#!/bin/sh
#-----------------------------------------------------
# setup
# install mental ray services
#
# Copyright (c) 2006 by Autodesk, Inc.
#
#-----------------------------------------------------
DIRNAME=`dirname "$0"`
MODSVC=${DIRNAME}/modsvc
BACKUPDIR=/tmp/mr-setup-backup

# Determine the installdir based on the location of this script.
# This script should be in the "bin" sub-directory, "DIRNAME" should be the bin directory.:
INSTALLDIR=`dirname "${DIRNAME}"`
LEADING_PATH_CHARACTER=${INSTALLDIR:0:1}  # This assumes that sh really is Bash!
if (test ${LEADING_PATH_CHARACTER} != "/") ; then
    echo "ERROR: Unable to determine the install directory!"
    echo "       Please use the full path when invoking this script."
    exit 0
fi
if (! test -d "${INSTALLDIR}") ; then
    echo "ERROR:  Unable to determine the install directory!"
    exit 0
fi
# Finally, test for a critical file to make sure this really is the install directory.
CRITICAL_FILE="${INSTALLDIR}/bin/raysatd"
if (! test -f "${CRITICAL_FILE}") ; then
    echo "ERROR:  The file \"${CRITICAL_FILE}\" could not be found!"
    echo "        Please only run this script ($0) from the install directory."
    exit 0
fi

BINDIR=${INSTALLDIR}/bin
SERVICEEXE=raysatd
RAYDSCRIPT=${BINDIR}/${SERVICEEXE}
RAYSERVMNAME=mi-raysat3651
RAYSERVHNAME="mental ray for Maya network rendering" 
RAYPORT='7108/tcp'
SPMPORT='7050/tcp'
SPMHOST=localhost
SPMSERVHNAME="mental images software protection manager"
SPMSERVMNAME="mi-spm"
PROG=`basename $0`
done=0
readstring()
{
	rsdef=$2
	rsmsg="$1 [$2]" 
	while true ; do
		echo -n "$rsmsg "
		answer=""
		read answer
		if [ "$answer" ] ; then
			return 
		else 
			answer=$2
			return
		fi
	done
}

backup() 
{
	# create a backup of sensitive files
	[ $verbose -eq 1 ] && echo "saving configuration files to ${BACKUPDIR}"
	mkdir ${BACKUPDIR} > /dev/null 2>&1
	cp /etc/services ${BACKUPDIR}/services >/dev/null 2>&1
	cp /etc/inetd.conf ${BACKUPDIR}/inetd.conf >/dev/null 2>&1
	cp /etc/xinetd.d/${RAYSERVMNAME} ${BACKUPDIR}/${RAYSERVMNAME} >/dev/null 2>&1
	cp ${RAYDSCRIPT} ${BACKUPDIR}/${RAYDSCRIPT} >/dev/null 2>&1
}

install()
{
	backup
	[ $spmonly == 0 ] && {
		# install the ray server service and configure the internet
		# services daemon to start it on the specified port
		${MODSVC} ${verbflag} ${nointflag} -i -h "${RAYSERVHNAME}" \
			-r ${BINDIR} \
			-e ${SERVICEEXE} \
			-p ${RAYPORT} \
			${RAYSERVMNAME}
		[ $? == 0 ] || {
			echo "!!! Setup encountered an error installing the mental ray"
			echo "!!! network rendering service. "
			exit 0
		}
		## make sure the spm service is also installed
		#${MODSVC} ${verbflag} ${nointflag} -i -h "${SPMSERVHNAME}" \
		#	-p ${SPMPORT} \
		#	${SPMSERVMNAME}
		#[ $? == 0 ] || {
		#	echo "!!! Setup encountered an error installing the SPM service"
		#	exit 0
		#}
	}

	# find out SPM_HOST and set up rayd to use it
	# we can't do this in quiet mode
	if [ $nopromptspm -eq 0 ] ; then
		if [ -f ${RAYDSCRIPT} ] ; then
			raydhost=`awk '/^setenv[ \t]+SPM_HOST/ { print $3 }' ${RAYDSCRIPT}`
			[ ".$raydhost" == "." ] || SPMHOST=$raydhost
		fi
	fi
	if [ $nointer == 0 ] ; then
		while [ ${done} -eq 0 ] ; do
			echo " "
			pres=0
			tmprd=/tmp/rayd$$.tmp
			[ $nopromptspm -eq 0 ] && {
				readstring "From which network host should mental ray request licenses ?" ${SPMHOST}
				SPMHOST=$answer
				[ ".${SPMHOST}" != ".localhost" ] && {
					[ $verbose -eq 1 ] && echo "looking for ${SPMHOST}..."
					ping -c 4 ${SPMHOST} > /dev/null 2>&1
					pres=$?
				}
			}
			if [ $pres -ne 0 ] ; then
				[ $verbose -eq 1 ] && echo "cannot contact ${SPMHOST} from this machine"
			else
				done=1
				awk "/setenv[ \t]+SPM_HOST/ { print \"setenv SPM_HOST ${SPMHOST}\"; next} {print}" \
					${RAYDSCRIPT} > $tmprd
				cp ${tmprd} ${RAYDSCRIPT}
				rm ${tmprd}
				[ $verbose -eq 1 ] && echo "${RAYDSCRIPT} now requests SPM licenses from ${SPMHOST}."
			fi
		done
	else 
		[ $verbose -eq 1 ] && {
			echo " "
			echo "${RAYDSCRIPT} requests licenses from server ${SPMHOST}"
			echo "To change the location of your license server run"
			echo "$0 in interactive mode"
		}
	fi
	[ $verbose -eq 1 ] && {
		echo " "
		echo "$PROG has successfully updated ${RAYSERVHNAME}"
		echo " "
		echo "To uninstall ${RAYSERVHNAME}"
		echo "run ${PROG} with the -u option."
	}
}

remove() 
{
	backup
	# uninstall the ray server service and reconfigure the internet
	# services daemon
	${MODSVC} ${verbflag} ${nointflag} -u -h "${RAYSERVHNAME}" \
		-r ${BINDIR} \
		-e ${SERVICEEXE} \
		-p ${SPMPORT} \
		${RAYSERVMNAME}
	[ $? == 0 ] || {
		echo "!!! Setup encountered an error removing the mental ray"
		echo "!!! network rendering service. "
		exit 0
	}
	[ $verbose -eq 1 ] && {
		echo " "
		echo "$PROG has successfully removed ${RAYSERVHNAME} "
		echo " "
	}
}

usage()
{
	cat <<!EOT!

Usage: $PROG [-hqunvs] [-o <host>]

where:
    -q         suppress messages
    -u         uninstall
    -n         non-interactive: fail if any 
               user-input is required
    -h         print this help message
    -s         spm-only. Do not update services
               just prompt for and change the
               license server. This option is
               incompatible with -n and -u
	-o <host>  spm server host name

install or optionally uninstall mental ray ray network
rendering service.

!EOT!
	exit 1
}

action=install
verbose=1
verbflag='-v'
nointer=0
spmonly=0
nopromptspm=0
while getopts o:hquns opt; do
	case $opt in
	\?)	exit 1
		;;
	u)	action='remove'
		;;
	q)	verbose=0
		verbflag=''
		;;
	n)	nointer=1
		nointflag='-n'
		;;
	s) 	spmonly=1
		;;
	h)  usage
		;;
	o)	SPMHOST=$OPTARG	
		nopromptspm=1
		;;
	esac
done
shift `expr $OPTIND - 1`

[ $spmonly -eq 1 ] && {
	[ $nointer -eq 1 ] && echo "-n flag ignored when updating SPM license server"
	nointer=0
	[ ".$action" == ".remove" ] && {
		echo "-u flag ignored when updating SPM license server"
		echo "Nothing to do..."
		exit 0
	}
}

MYUID=`id -u`
if test "$MYUID" != "0" ; then
	echo "$PROG must be run with root priveleges";
	exit 1;
fi

[ $nointer -eq 0 ] && {
	echo ""
	if [ ".$action" == ".remove" ] ; then 
		echo "netsetup will inspect and update your system to disable"
		echo "network rendering with mental ray"
	else
		echo "netsetup will inspect and update your system to enable"
		echo "network rendering with mental ray"
		echo 'This machine will be set up as a mental ray render server ("slave")'
		echo 'and will receive instructions from client machines ("master")'
	fi
	readstring "Do you want to continue [yes/no] ?" "n"
	case $answer in
	y|yes|Y|YES|Yes)
		echo ""
		;;
	*)
		echo ""
		usage
		;;
	esac;
}

[ ".$action" == ".remove" ] && remove
[ ".$action" == ".install" ] && install
exit 0
