#!/bin/sh 
###############################################################################
# Copyright 1997-2000 by mental images GmbH & Co.KG, Fasanenstr. 81, D-10623
# Berlin, Germany. All rights reserved.					       #
#
# installation of mental ray
#
# $Id: mi_install_ray,v 1.12 2000/06/19 14:05:22 runa Exp $
#
# functions:
#
#	check_errstat
#	rexit_stat
#	root_check
#	install_ray
#	add_spm_port
#	set_spmhost
#	add_ray_port
#	change_inetd
#	install_main
#	install_shaderlib
#	mk_softimage_links
#	install_slaves
#	start_remote_install
#	check_rayowner
#
###############################################################################

usage()
{
echo "usage:  mi_install_ray [ -sduh ]"
echo "	-h help"
echo "	-s silent installation"
echo "	-d default installation" 
echo "	-u deinstall mental ray"
exit 0
}


# parsing command line options
set -- `getopt sduh $@`
[ $? != 0 ] && usage

while :
do 
	case $1 in 
        -s) miSilent=1; miOptions="$miOptions -s";;
        -d) miCustom=0; miOptions="$miOptions -d";;
	-u) miDeinst=1; miOptions="$miOptions -u";;
	-h) usage;;
        --) break;;
	esac
	shift
done



# if this script is called as standalone it cannot run in default mode
ROOT=$TOP
[ x$TOP = x ] && ROOT=`dirname $0`/..
miProgname="$0"
TMP=${TMP:=/tmp/mi}
if [ $miProgname = ${TMP}/remote_install_ray ] ;then
        INSTFROM=$TMP
else
        INSTFROM=${INSTFROM:=${ROOT}/install}
fi
INSTFROM=${INSTFROM:=${ROOT}/install}
miExt=`${INSTFROM}/mi_checkos`
[ $miExt = none ] &&
	{
	echo "This platform is not supported to run mental ray"
	exit
	}
INSTDIR=${INSTDIR:=/usr/local/mi}
RAYFROM=${RAYFROM:=${ROOT}/${miExt}/bin}
INCFROM=${INCFROM:=${ROOT}/common/include}
SHADFROM=${SHADFROM:=${ROOT}/${miExt}/shaders}
miCustom=${miCustom:=1}
miSilent=${miSilent:=0}
miDeinst=${miDeinst:=0}
FROM=$RAYFROM
OS=`uname`
HOST=`hostname`
miProc=default
miHost=$HOST
miDNS=0
miNIS=0
miNISmaster=$HOST
miNISservice=0
miNIShosts=0
miDNSserver=$HOST
miRayOwner=nobody
miPrinted=0
miConfig=/tmp/mi-config
miSpmPortNo=7050
miSpmPort=mi-spm
miInetFile=/etc/inetd.conf
miServiceFile=/etc/services
miRayPort=mi-raySoft3D392

PATH=/bin
[ $OS = 'OSF1' ] && PATH=$PATH:/sbin
[ $OS = 'SunOS' ] && PATH=$PATH:/usr/ucb
[ $OS = 'HP-UX' ] && PATH=$PATH:/usr/bin:/usr/sbin
[ $OS = 'IRIX' -o $OS = 'IRIX64' ] && PATH=$PATH:/usr/sbin:/usr/bsd
[ $OS = 'Linux' ] && PATH=$PATH:/usr/bin


init_var()
{
# set system dependent system calls
UNAME=uname
RSH=rsh
[ -f /usr/bin/remsh ] && RSH=/usr/bin/remsh
[ -f /bin/remsh ] && RSH=/bin/remsh
PS='ps -ef' 
[ $OS = 'SunOS' -a "`$UNAME -r | awk -F. '{print $1}'`" = 4 ] && PS='ps uax'
[ $OS = 'Linux' ] && PS='ps uax'
[ $miProgname = "${TMP}/remote_install_ray" ] && {
	FROM=$TMP
	INSTFROM=$TMP
	INCFROM=$TMP/include
	SHADFROM=$TMP/shaders
	}
}

# utility functions check_errstat
check_errstat()
{
if [ $errcode != 0 ];then
	rm -f ${TMP}/OK
        echo "FATAL ERROR: $message"
	echo "installation procedure aborted"
	echo
        exit  $errcode
fi
errcode=0
}

# check remote exit status
rexit_stat()
{
OK=`$RSH $miHost  "ls ${TMP}/OK" 2>/dev/null`
[ x$OK = x ] && {
	$RSH $miHost "rm -rf $TMP"
	exit 1
	}
}



# most things does not work if you are not superuser
root_check()
{
if [ `whoami` != 'root' ]; then
 	errcode=1 
	message="Sorry, you must be root to continue with installation"	
	check_errstat
fi
}



#*******************************************************************************
# copy mental ray, include files to $INSTDIR
# make links to /usr/softimage/mental_ray 

install_ray()
{

[ $miSilent = 0 ] && echo "... installation directory is $INSTDIR ..."

if [ $miDeinst = 1 ]; then 
	[ $miSilent = 0 ] && echo "... removing $INSTDIR ..."
	[ -d $INSTDIR ] && {
		[ ! -f $INSTDIR/bin/ray ] && {
			echo "WARNING: $INSTDIR seems not to contain a"
			echo "mental ray installation and will not be removed"  
			exit 1
		}
		rm -rf $INSTDIR 1>/dev/null 2>&1
		errcode=$?;message="cannot remove $INSTDIR";check_errstat
	}
	return
fi

if [ ! -d $INSTDIR/bin ] ;then 
	[ $miSilent = 0 ] && 
		echo "... making directory ${INSTDIR}/bin on $HOST ..."
	mkdir -p ${INSTDIR}/bin 1>/dev/null 2>&1
	errcode=$?;message="cannot create ${INSTDIR}/bin";check_errstat
	chmod 755 ${INSTDIR}/bin 1>/dev/null 2>&1
fi

if [ -f ${INSTDIR}/bin/ray ] ;then
	[ $miSilent = 0 ] && 
		echo "... there is already a mental ray executable (ray) installed ..."
	if [ $miCustom = 1 ]; then
		echo
		echo "Overwrite ray in ${INSTDIR}/bin [y]?"
		read tmp
		[ ${tmp:-y} = n ] && return
	fi
fi


[ $miSilent = 0 ] && echo "... copying ray from ${FROM} to ${INSTDIR}/bin ..."
cp ${FROM}/ray 	${INSTDIR}/bin/ray 1>/dev/null 2>&1
errcode=$?
message="cannot copy ${FROM}/ray to ${INSTDIR}/bin"
check_errstat
chmod 755 ${INSTDIR}/bin/ray 1>/dev/null 2>&1

if [ -f ${FROM}/imf_disp ] ;then
	[ $miSilent = 0 ] && 
		echo "... copying imf_disp from ${FROM} to ${INSTDIR}/bin ..."
	cp ${FROM}/imf_disp 	${INSTDIR}/bin/imf_disp 1>/dev/null 2>&1
	errcode=$?
	message="cannot copy ${FROM}/imf_disp to ${INSTDIR}/bin"
	check_errstat
	chmod 755 ${INSTDIR}/bin/imf_disp 1>/dev/null 2>&1
fi

if [ -f ${FROM}/ivray ] ;then
	[ $miSilent = 0 ] && 
		echo "... copying ivray from ${FROM} to ${INSTDIR}/bin ..."
	cp ${FROM}/ivray	${INSTDIR}/bin/ivray 1>/dev/null 2>&1
	errcode=$?
	message="cannot copy ${FROM}/ivray to ${INSTDIR}/bin"
	check_errstat
	chmod 755 ${INSTDIR}/bin/ivray 1>/dev/null 2>&1
fi

if [ -f ${FROM}/spmstat ] ;then
	[ $miSilent = 0 ] && 
		echo "... copying spmstat from ${FROM} to ${INSTDIR}/bin ..."
	cp ${FROM}/spmstat 	${INSTDIR}/bin/spmstat 1>/dev/null 2>&1
	errcode=$?
	message="cannot copy ${FROM}/spmstat to ${INSTDIR}/bin"
	check_errstat
	chmod 755 ${INSTDIR}/bin/spmstat 1>/dev/null 2>&1
fi


if [ -f ${INCFROM}/rayrc ] ; then
	incinst=rayinc
else
	incinst=inc
fi

if [ ! -d ${INSTDIR}/$incinst ] ;then 
	[ $miSilent = 0 ] && 
		echo "... making directory ${INSTDIR}/$incinst on $HOST ..."
	mkdir ${INSTDIR}/$incinst 1>/dev/null 2>&1
	errcode=$?;message="cannot create ${INSTDIR}/$incinst";check_errstat
	chmod 755 ${INSTDIR}/$incinst 1>/dev/null 2>&1
fi

[ $miSilent = 0 ] && 
	echo "... copying include files from ${INCFROM} to ${INSTDIR}/$incinst ..."

for miInc in ${INCFROM}/*
do
	cp $miInc ${INSTDIR}/$incinst 1>/dev/null 2>&1
	errcode=$?
	message="cannot copy $miInc to ${INSTDIR}/$incinst"
	check_errstat
done
chmod 644 ${INSTDIR}/${incinst}/* 1>/dev/null 2>&1
 
[ -h /usr/include/mi ] && rm -f /usr/include/mi
ln -s ${INSTDIR}/${incinst} /usr/include/mi 1>/dev/null 2>&1
errcode=$?
message="linking ${INSTDIR}/${incinst} to /usr/include/mi failed"
check_errstat

if [ $OS = 'AIX' ]; then
	mkdir ${INSTDIR}/lib 1>/dev/null 2>&1
	[ -f ${INSTDIR}/lib/keep.exp ] &&
		mv ${INSTDIR}/lib/keep.exp ${INSTDIR}/lib/keep.exp.orig
	[ -f ${INSTDIR}/lib/keepsym.ibm ] &&
		mv ${INSTDIR}/lib/keepsym.ibm ${INSTDIR}/lib/keepsym.ibm.orig
	[ -f ${INCFROM}/keep.exp ] && cp ${INCFROM}/keep.exp ${INSTDIR}/lib 
	[ -f ${INCFROM}/keepsym.ibm ] && cp ${INCFROM}/keepsym.ibm /usr/local/mi
fi
}

#*******************************************************************************
# installation of shader to $INSTDIR

install_shaderlib()
{

rayrc=0


[ $miDeinst = 1 ] && return

[ $miSilent = 0 ] && 
	echo "... installing shader library ..."

if [ ! -d ${INSTDIR}/shaders ]; then
	[ $miSilent = 0 ] &&
		echo "... make directory ${INSTDIR}/shaders ..."
	mkdir ${INSTDIR}/shaders
	errcode=$?;message="cannot create ${INSTDIR}/shaders";check_errstat
	chmod 755 ${INSTDIR}/shaders 1>/dev/null 2>&1
else
	[ $miCustom = 1 ] && {
		[ $miSilent = 0 ] &&
			echo "... shader are already installed ..."
		echo
		echo "Overwrite shader in ${INSTDIR}/shaders [y]?"
		read tmp
		[ ${tmp:-y} = n ] && return
	}
fi

[ -f ${INCFROM}/rayrc ] && rayrc=1

[ $miSilent = 0 ] && { 
	echo "... copying shader from ${SHADFROM} to ${INSTDIR}/shaders"
	[ $rayrc = 1 ]  && echo "... creating ${INSTDIR}/rayrc ..."
	}

if [ $rayrc = 1 ]; then
	rayrc_ext=""
	[ -f ${INSTDIR}/rayrc ] && rayrc_ext=.inst
	rm -f ${INSTDIR}/rayrc$rayrc_ext
	touch ${INSTDIR}/rayrc$rayrc_ext 1>/dev/null 2>&1
	chmod 644 ${INSTDIR}/rayrc$rayrc_ext 1>/dev/null 2>&1
	echo registry \"{_MI_REG_INCLUDE}\" value \"${INSTDIR}/${incinst}\" end registry >> ${INSTDIR}/rayrc$rayrc_ext
	echo registry \"{_MI_REG_LIBRARY}\" value \"${INSTDIR}/shaders\" end registry >> ${INSTDIR}/rayrc$rayrc_ext
fi

for miShader in ${SHADFROM}/*
do
	cp $miShader ${INSTDIR}/shaders
	errcode=$?
	message="cannot copy $miShader to ${INSTDIR}/shaders"
	check_errstat
	chmod 755 $miShader 1>/dev/null 2>&1
	[ $rayrc = 1 ] && {
		echo "link \"`basename $miShader`\"" >> ${INSTDIR}/rayrc$rayrc_ext
	}
done
}

#*******************************************************************************
# making links to Softimage

mk_softimage_links()

{

[ $miDeinst = 1 ] && return 

if [ $miCustom = 1 ] ;then
	OSBool=`grep SBool $miConfig | awk '{print $2}' 2>/dev/null`
        SBool=${OSBool:-y}
	echo
	echo "Do you want to use mental ray from within Softimage|3D [$SBool]?"
	read tmp
	SBool=${tmp:=$SBool}
	if [ x$OSBool = x ];then
		echo "SBool $SBool" >> $miConfig
	else
		sed -e "/SBool/ s/$OSBool/$SBool/" < $miConfig > ${miConfig}.tmp
		mv ${miConfig}.tmp $miConfig
	fi
	[ $SBool = n ] && return
fi

if [ $SI_LOCATION ]; then
	SOFTDIR=$SI_LOCATION
else
	SOFTDIR=/usr/Softimage
fi 

if [ $miCustom = 1 ] ;then
	OSOFTDIR=`grep SOFTDIR $miConfig | awk '{print $2}' 2>/dev/null`
	OSOFTDIR=`echo $OSOFTDIR | tr -s ! /`
        SOFTDIR=${OSOFTDIR:-$SOFTDIR}
	echo
	echo "Where is the Softimage directory [$SOFTDIR]?"
	read tmp
	SOFTDIR=${tmp:-$SOFTDIR}
	SI_LOCATION=$SOFTDIR; export SI_LOCATION
	OSOFTDIR=`echo $OSOFTDIR | tr -s / !`
        SOFTDIR=`echo $SOFTDIR | tr -s / !`
	if [ x$OSOFTDIR = x ];then
		echo "SOFTDIR $SOFTDIR" >> $miConfig
	else
		sed -e "/SOFTDIR/ s/${OSOFTDIR}/${SOFTDIR}/" < $miConfig > ${miConfig}.tmp
		mv ${miConfig}.tmp $miConfig
	fi
	SOFTDIR=`echo $SOFTDIR | tr -s ! /`
fi

if [ -f ${INCFROM}/rayrc ] ; then
	SOFTSHADIR=${SOFTDIR}/mental_ray/MR_Shaders/Shader_Lib/lib20
	SOFTINC=${SOFTDIR}/mental_ray/inc/mr2.0
else
	SOFTSHADIR=${SOFTDIR}/mental_ray/MR_Shaders/Shader_Lib/lib
	SOFTINC=${SOFTDIR}/mental_ray/inc/mr1.9
fi


[ $miSilent = 0 ] &&
	echo "... making softlinks to Softimage directory ..."

if [ -d $SOFTDIR -o -h $SOFTDIR ]; then
	[ $miSilent = 0 ] && 
		echo "... old files will be saved ..."
fi

mkdir -p $SOFTDIR/mental_ray 1>/dev/null 2>&1
mkdir ${SOFTDIR}/mental_ray/bin 1>/dev/null 2>&1
mkdir -p ${SOFTINC} 1>/dev/null 2>&1
mkdir -p ${SOFTSHADIR} 1>/dev/null 2>&1
mkdir -p ${SOFTDIR}/3D/rsrc 1>/dev/null 2>&1

if [ -f ${SOFTDIR}/mental_ray/bin/ray ];then
	[ ! -f ${SOFTDIR}/mental_ray/bin/ray.orig ] &&
		mv ${SOFTDIR}/mental_ray/bin/ray                                            ${SOFTDIR}/mental_ray/bin/ray.orig
	rm -f ${SOFTDIR}/mental_ray/bin/ray
fi
ln -s ${INSTDIR}/bin/ray ${SOFTDIR}/mental_ray/bin/ray 1>/dev/null 2>&1
errcode=$?
message="cannot link ${INSTDIR}/bin/ray to ${SOFTDIR}/mental_ray/bin/ray"
check_errstat

for miInc in ${INSTDIR}/${incinst}/*
do
	miInc=`basename $miInc`
	if [ -f ${SOFTINC}/$miInc ]; then
		[ ! -f ${SOFTINC}/inc/$miInc.orig ] && 
			mv ${SOFTINC}/$miInc ${SOFTINC}/$miInc.orig
		rm -f ${SOFTINC}/$miInc
	fi
	ln -s ${INSTDIR}/${incinst}/$miInc ${SOFTINC}
	errcode=$?
	message="cannot link $miInc to ${SOFTINC}"
	check_errstat
done

if [ -f ${SOFTDIR}/3D/rsrc/softimage.mi ]; then
	[ ! -f ${SOFTDIR}/3D/rsrc/softimage.mi.orig ] && 
		mv ${SOFTDIR}/3D/rsrc/softimage.mi                                                 ${SOFTDIR}/3D/rsrc/softimage.mi.orig
	rm -f ${SOFTDIR}/3D/rsrc/softimage.mi
fi
ln -s ${INSTDIR}/${incinst}/softimage.mi ${SOFTDIR}/3D/rsrc 1>/dev/null 2>&1
errcode=$?
message="cannot link ${INSTDIR}/${incinst}/softimage.mi to ${SOFTDIR}/3D/rsrc"
check_errstat

[ -d ${INSTDIR}/shaders ] || return
for miShader in ${INSTDIR}/shaders/*
do
	miShader=`basename $miShader`
	if [ -f ${SOFTSHADIR}/$miShader ];then
		[ ! -f ${SOFTSHADIR}/miShader.orig ] && 
			mv ${SOFTSHADIR}/$miShader ${SOFTSHADIR}/$miShader.orig
		rm -f ${SOFTSHADIR}/$miShader
	fi
	ln -s ${INSTDIR}/shaders/$miShader ${SOFTSHADIR}/$miShader
	errcode=$?
	message="cannot link $miShader to $SOFTSHADIR"
	check_errstat
done
}

#*******************************************************************************
# get network information where to add mi-ray port (DNS, NIS or local services)
get_network_information()
{
[ $miSilent = 0 ] && {
	echo
	echo "*** setting up communication with the license server ***"
	echo
	echo "... gathering network information ..."
	}

[ -f /etc/resolv.conf ] &&  miDNS=1
$PS | grep ypbind | grep -v grep 1>/dev/null 2>&1
[ $? = 0 ] &&  miNIS=1

if [ $miNIS = 1 ] ;then
        miNISmaster=`ypwhich -m passwd 2>/dev/null`
        if [ $miSilent = 0 ] ;then
                echo "... it seems you are running NIS ..."
                echo "... your NIS master is $miNISmaster ..."
        fi
        ypcat services 2>&1 | grep 'no such map'
        [ $? != 0 ] && miNISservice=1
        ypcat hosts 2>&1 | grep 'no such map'
        [ $? != 0 ] && miNIShosts=1
fi

if [ $miDNS = 1 ]; then
        miDNSserver=`grep nameserver /etc/resolv.conf | awk '{print $2}'`
        [ $miSilent = 0 ] && {
		if [ $miNIS = 1 ] ;then
		    echo "... hhm, probably you are running NIS and DNS ..."
		else
		    echo "... you are using DNS for host name resolution ..."
		    echo "... your nameserver has the address: $miDNSserver ..."
		fi
        }
fi
if [ $miNIS = 0 -a $miDNS = 0 ] ; then
        [ $miSilent = 0 ] && {
	echo "... your system does not seem to use NIS or DNS ..."
        echo "... local files /etc/services and /etc/hosts will be changed ..."
	}
fi
}

#*******************************************************************************
# add spm port mi-spm to /etc/services
# check if the port number 7050 is already in use
# check for existing ports of mi-spm 
add_spm_port()
{

[ $miDeinst = 1 ] && return

[ $miSilent = 0 ] && {
	echo
	echo "... mental ray needs port $miSpmPort to communicate with the license server ..."
	echo "... default port number is $miSpmPortNo ..."
	}

if [ $miNISservice = 1 -a $miNISmaster != $HOST ];then
        `ypcat services | grep $miSpmPort 1>/dev/null 2>&1`
        [ $? = 0 ] && {
                [ $miSilent = 0 ] && {
                        echo "... the spm port is already installed ..."
			echo "... nothing to be done ..."
			}
                return
                }

	[ $miSilent = 0 ] && 
		echo "... ${miNISmaster}'s $miServiceFile has to be changed ..."

	echo
	echo "WARNING:"
	echo "Without this entry mental ray will not find licenses."
	echo "Add the following line to $miNISmaster's ${miServiceFile}:" 
	echo 
	echo "$miSpmPort	$miSpmPortNo/tcp # SW protection"
	echo
	return
fi

miSpmPort=`grep $miSpmPortNo $miServiceFile|awk '{print $1}' 2>/dev/null`
if [ x$miSpmPort = 'xmi-spm' ]; then
	[ $miSilent = 0 ] && {
		echo "... spm port is already installed ..."
		echo "... nothing to be done ..."
		}
	return
fi

if [ $miCustom = 1 ]; then
	echo
	echo "Change $miServiceFile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

if [ x$miSpmPort != 'x' ];then 
	[ $miSilent = 0 ] && 
		echo "... spm port is used by another service ..."
	while true
	do
   		miSpmPortNo=`expr $miSpmPortNo + 1`
		grep $miSpmPortNo $miServiceFile 1>/dev/null 2>&1
		[ $? != 0 ] && break
	done
	if [ $miCustom = 1 ]; then
		echo
		echo "Port to be used by SPM[$miSpmPortNo]?"
		read tmp
		miSpmPortNo=${tmp:-$miSpmPortNo}
	fi			
fi

[ $miSilent = 0 ] &&
	echo "... original $miServiceFile will be saved in ${miServiceFile}.orig ..."
[ ! -f ${miServiceFile}.orig ] &&
	cp $miServiceFile ${miServiceFile}.orig 1>/dev/null 2>&1

[ $miSilent = 0 ] && 
	echo "... adding mi-spm to $miServiceFile with port number $miSpmPortNo ..."

(echo "mi-spm          $miSpmPortNo/tcp" >> $miServiceFile) 2>/dev/null
errcode=$?
[ $errcode != 0 ] && rm -f ${miServiceFile}.orig
message="cannot add spm port to $miServiceFile";check_errstat
}


#*******************************************************************************
# add spmhostX nicknames to the suitable hosts information file
# for NIS and DNS ask the system adminstrator
# in any other case add it to local hosts file
set_spmhost()
{
spmhost=$HOST
hostfile=/etc/hosts
i=0

if [ $miDeinst = 1 ]; then
# done in deinstall spm
#	[ $miDNS = 1 -a $miSilent = 0 ] &&
#		echo "Ask your administrator to delete spmhost0 from DNS database" 
#	[ $miNIShosts = 1 -a $miSilent = 0 ] && {
#		echo " Delete spmhost0 from $miNISmaster's $hostfile"
#		}
	[ -f ${hostfile}.orig ] && {
		[ $miSilent = 0 ] && 
			echo "... restoring original $hostfile ..."
		mv ${hostfile}.orig $hostfile
		}
	return
fi

[ $miSilent = 0 ] && {
	echo 
	echo "... mental ray will look for host spmhost0 to find the license server ..."
	}

spmhost=`grep spmhost0 $hostfile| awk '{print $2}'`
spmhost=${spmhost:-$HOST}
ospmhost=`grep miSpmhost $miConfig | awk '{print $2}' 2>/dev/null`
spmhost=${ospmhost:-$spmhost}

if [ $miCustom = 1 ]; then
	echo
	echo "Which host will be your SPM license server [$spmhost]?"
	read tmp		
	spmhost=${tmp:-$spmhost}
        if [ x$ospmhost != x ] ;then
                sed -e "/miSpmhost /s/$ospmhost/$spmhost/" < $miConfig > $miConfig.tmp
                mv $miConfig.tmp $miConfig 1>/dev/null 2>&1
        else
                echo "miSpmhost $spmhost" >> $miConfig 2>/dev/null
        fi
fi

if [ $miDNS = 1 -a $miNIShosts = 1 ] ;then
	[ $miSilent = 0 ] &&
	    echo "... you are running NIS and DNS for host name resolution ..."
	echo "Ask your system administrator to add nickname spmhost0 to 
	      your license servers entry."
	return
fi
	
if [ $miDNS = 1 ]; then
	[ $miSilent = 0 ] && 
		echo "... DNS' hostname base has to be changed ..."
	echo "Ask your system administrator to add spmhost0 to DNS database"
	return
fi

if [ $miNIShosts = 1 -a $miNISmaster != $spmhost ]; then
	ypcat hosts |  grep $spmhost | grep spmhost  1>/dev/null 2>&1
        [ $? = 0 ] && {
                [ $miSilent = 0 ] && {
                        echo "... $spmhost already registered as spmhost ..."
			echo "... nothing to be done ..."
			}
                return
                }

	[ $miSilent = 0 ] && 
		echo "... ${miNISmaster}'s $hostfile has to be changed ..."
	echo "WARNING:"
        echo "Add nickname spmhost0 to $spmhost's entry in ${miNISmaster}'s $hostfile"
	echo "Without this entry mental ray will not find licenses."
	echo
        return

fi

grep $spmhost $hostfile | grep spmhost 1>/dev/null 2>&1
	[ $? = 0 ] && {
		[ $miSilent = 0 ] && {
			echo "... $spmhost already registered as spmhost ..."
			echo "... nothing to be done ..."
			}
	return
	}

if [ $miCustom = 1 ]; then
	echo
	echo "Change local $hostfile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && {
		return
		}
fi

grep $spmhost $hostfile 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	echo "WARNING:"
	echo "$spmhost is not in $hostfile"
	echo "Add this entry and rerun the installation procedure"
	errcode=1;message="";check_errstat
fi

grep spmhost $hostfile 1>/dev/null 2>&1
if [ $? = 0 ] ;then
	[ $miSilent = 0 ] && 
		echo "... there already exist spmhosts in the network ..."
	if [ $miCustom = 1 ]; then
		echo
		echo "Register $spmhost as an additional spmhost[y]?"
		read tmp
		[ ${tmp:-y} = n ] && {
			return
		}
	fi
fi
while true
do 
	grep spmhost$i $hostfile 1>/dev/null 2>&1
	[ $? != 0 ] && break
	i=`expr $i + 1`
done
(sed -e "/$spmhost	/ s/$/ spmhost${i}/" < $hostfile > /tmp/hosts.tmp) 2>/dev/null
errcode=$?;message="cannot write in this directory";check_errstat
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	(sed -e "/$spmhost / s/$/ spmhost${i}/" <$hostfile > /tmp/hosts.tmp) 2>/dev/null
fi
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ] ;then
	(sed -e "/$spmhost$/ s/$/ spmhost${i}/" <$hostfile > /tmp/hosts.tmp) 2>/dev/null
fi
grep spmhost${i} /tmp/hosts.tmp 1>/dev/null 2>&1
if [ $? != 0 ];then
	echo "WARNING:"
	echo "changing $hostfile failed, please do it manually"
	return
fi
[ $miSilent = 0 ] &&
	echo "... original $hostfile will be saved in ${hostfile}.orig ..." 

[ ! -f ${hostfile}.orig ] && 
	cp $hostfile ${hostfile}.orig 1>/dev/null 2>&1

mv /tmp/hosts.tmp $hostfile 2>/dev/null
errcode=$?;message="no permission to overwrite $hostfile";check_errstat
[ $miSilent = 0 ] && echo "... $spmhost registered as spmhost$i ..."
[ $miCustom = 1 ] && echo "WARNING: $hostfile was changed, please verify"
}

#*******************************************************************************
# add ray port and spm port to /etc/services
add_ray_port()
{
RayPortNumber=7002
if [ $miDeinst = 1 ]; then
	[ -f ${miServiceFile}.orig ] && {
		[ $miSilent = 0 ] && 
			echo "... restoring original $miServiceFile ..."
		mv ${miServiceFile}.orig $miServiceFile 1>/dev/null 2>&1
		errcode=$?
		message="cannot restore original $miServiceFile"
		check_errstat
	}
	[ $miNISservice = 1 -a $miSilent = 0 ] && {
		echo "... ${miNISmaster}'s $miServiceFile has to be changed ..."
		echo "Delete $miRayPort from ${miNISmaster}'s $miServiceFile!"
		}
	return
fi

[ $miSilent = 0 ] && {
	echo
	echo "*** preparing the system to run mental ray network parallel ***"
	echo
	echo "... mental ray needs port $miRayPort to communicate with slaves ..."
	echo "... default port number is $RayPortNumber ..."
	}

if [ $miNISservice = 1 ];then
	`ypcat services | grep $miRayPort 1>/dev/null 2>&1`
	[ $? = 0 ] && {
		[ $miSilent = 0 ] && {
			echo "... the ray port is already installed ..."
			echo "... nothing to be done ..."
			}
		return
		}

	[ $miSilent = 0 ] && {
		echo "... ${miNISmaster}'s $miServiceFile has to be changed ..."
 	echo "Add the following line to $miNISmaster's $miServiceFile" 
	echo
	echo "${miRayPort}	${RayPortNumber}/tcp # MR ray tracer"
	echo
	echo "NOTICE: Without this entry mental ray cannot run in network mode."
	}
	return
fi

RayPort=`grep $RayPortNumber $miServiceFile|awk '{print $1}' 2>/dev/null`
if [ x$RayPort != x -a x$RayPort = x$miRayPort ]; then
	[ $miSilent = 0 ] && {
		echo "... ray port is already installed ..."
		echo "... nothing to be done ..."
		}
	return
fi

if [ $miCustom = 1 ]; then
	echo
	echo "Change $miServiceFile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

#for future use #crl 1997
#if [ $miRayPort ];then 
#	[ $miSilent = 0 ] && 
#		echo "... ray port is used by another service ..."
#	while true
#	do
#   		RayPortNumber=`expr $RayPortNumber + 1`
#		grep $RayPortNumber $miServiceFile 1>/dev/null 2>&1
#		[ $? != 0 ] && break
#	done
#	if [ $miCustom = 1 ]; then
#		echo
#		echo "Port to be used by ray[$RayPortNumber]?"
#		read tmp
#		RayPortNumber=${tmp:-$RayPortNumber}
#	fi			
#fi

[ $miSilent = 0 ] && 
	echo "... adding $miRayPort to $miServiceFile ..."

(echo "$miRayPort          $RayPortNumber/tcp" >> $miServiceFile) 2>/dev/null
errcode=$?
message="cannot add ray port to $miServiceFile";check_errstat
[ $miCustom = 1 ] && echo "WARNING: $miServiceFile was changed, please verify"
}

#*******************************************************************************
# check nobodies entry
check_rayowner()
{

[ $miDeinst = 1 ] && return
[ $miSilent = 0 ] && {
	echo
	echo "... ray servers will be started from inetd ..."
	}

grep $miRayPort $miInetFile 1>/dev/null 2>&1
[ $? = 0 ] && return

[ $miCustom = 1 ] && {
	echo
	echo "Enter the name of the owner of ray servers [$miRayOwner]?"
	read tmp
	[ $miRayOwner = root ] && {
		echo "WARNING:"
		echo "Running ray servers as root is a serious security hole"	
		}
	miRayOwner=${tmp:-$miRayOwner}
	}

[ $miSilent = 0 ] && {
	echo "... ray servers will run as $miRayOwner ..."
	echo "... checking $miRayOwner's password entry ..."
	}

if [ $miNIS = 1 ];then 
	id=`ypcat passwd | grep $miRayOwner 2>/dev/null`
	[ $? = 1 ] && {
		id=`grep $miRayOwner /etc/passwd 2>/dev/null`
		errcode=$?;message="$miRayOwner does not exist";check_errstat
		}
else
	id=`grep $miRayOwner /etc/passwd 2>/dev/null`
	errcode=$?;message="$miRayOwner does not exist";check_errstat
fi
uid=`echo $id | awk -F: '{print $3}' 2>/dev/null`
gid=`echo $id | awk -F: '{print $4}' 2>/dev/null`

[ `expr "$uid" \> \0` = 1 -a `expr "$uid" != 4294967294` ]  || 
		echo "WARNING: change ${miRayOwner}'s uid to a positive value"
[ `expr "$gid" \> \0` = 1 -a `expr "$gid" != 4294967294` ]  ||
		echo "WARNING: change ${miRayOwner}'s gid to a positive value"
}

#*******************************************************************************
# changes inetd.conf on any slave host

change_inetd()
{

if [ $miDeinst = 1 ]; then
	[ -f ${miInetFile}.orig ] && {
		[ $miSilent = 0 ] && 
			echo "... restoring original $miInetFile ..."
		mv ${miInetFile}.orig $miInetFile 1>/dev/null 2>&1
		errcode=$?;message="cannot restore original $miInetFile ..."
	}
	return
fi

[ $miSilent = 0 ] && echo "... ray entry has to be added to $miInetFile ..."

grep "$miRayPort " $miInetFile 1>/dev/null 2>&1
[ $? = 0 ] && {
	[ $miSilent = 0 ] && {
		echo "... $miInetFile is already updated ..."
		echo "... nothing to be done ..."
		}
	return
	}

if [ $miCustom = 1 ] ;then
	echo
	echo "Change $miInetFile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

[ $miSilent = 0 ] && 
	echo "... original $miInetFile will be saved in ${miInetFile}.orig ..."

[ ! -f ${miInetFile}.orig ] &&
	cp $miInetFile ${miInetFile}.orig 1>/dev/null 2>&1

echo "$miRayPort          stream  tcp    nowait  $miRayOwner  ${INSTDIR}/bin/ray ray -server" 2>/dev/null >> $miInetFile
errcode=$?
[ $errcode != 0 ] && rm -f ${miInetFile}.orig
message="cannot change $miInetFile";check_errstat

pid=`$PS | grep inetd | grep root | grep -v grep | awk '{print $2}'`
[ $miSilent = 0 ] && echo "... sending kill -1 to process $pid (inetd) ..."

kill -1 $pid 2>/dev/null
[ $? = 0 ] || echo "WARNING: cannot send kill -1 to inetd, do it manually"

}
#*******************************************************************************
# main for remote installation
start_remote_install()
{

for miHost in $miRemoteHosts
do
	[ $miHost ] || exit 0
	OS=`$RSH $miHost  $UNAME 2>/dev/null`
	[ $? != 0 ] && {
		echo
		echo
		echo "cannot login on $miHost"
		echo "NOTICE: $miHost will be excluded"
		continue
		}
	init_var
	$RSH $miHost rm -rf ${TMP} 1>/dev/null 2>&1
	$RSH $miHost mkdir ${TMP} 1>/dev/null 2>&1
	errcode=$?
	message="mkdir ${TMP} on $miHost failed"
	check_errstat

	rcp ${INSTFROM}/mi_checkos ${miHost}:${TMP}
	miExt=`$RSH $miHost ${TMP}/mi_checkos 2>/dev/null`
 	[ $miExt = none ] && {
		echo "This version of ${OS} is not supported"
		continue
		}
        [ $miExt = "irix6-n32" -a $miDeinst = 0 ] && {
		[ $miCustom = 1 ] && {
			echo
			echo Choose 64 bits if you have many threads or 
			echo very large scenes or textures, and 32 otherwise.
			echo Note that 32 and 64 bit executables and 
			echo shader libraries cannot be mixed.
		}
		echo "The 32bit version is the default on IRIX6."
		echo "Do you want to use the 64bit version instead[n]?"
		read tmp
		[ ${tmp:-n} = y ] && miExt=irix6-64
		RAYFROM=${ROOT}/${miExt}/bin
		SHADFROM=${ROOT}/${miExt}/shaders
		}
 	trap "$RSH $miHost  'rm -rf ${TMP}'" 2 15
	FROM=${ROOT}/${miExt}/bin
	SHADFROM=${ROOT}/${miExt}/shaders
if [ $miDeinst = 0 ]; then
	rcp ${FROM}/ray $miHost:${TMP}/ray
	errcode=$?;message="cannot find ${FROM}/ray";check_errstat
	[ -f ${FROM}/ivray ] && {
		rcp ${FROM}/ivray $miHost:${TMP}/ivray
		errcode=$?
		message="cannot find ${FROM}/ivray"
		check_errstat
		}
	[ -f ${FROM}/imf_disp ] && {
		rcp ${FROM}/imf_disp $miHost:${TMP}/imf_disp
		errcode=$?
		message="cannot find ${FROM}/imf_disp"
		check_errstat
		}
	$RSH $miHost  "mkdir ${TMP}/include" 2>/dev/null
	for miInc in ${INCFROM}/*
	do
		if [ -f $miInc ]; then
			rcp $miInc $miHost:${TMP}/include 2>/dev/null
			errcode=$?
			message="cannot find $miInc"
			check_errstat 
		fi
	done

	if [ -d ${SHADFROM} ]; then
		$RSH $miHost  "mkdir ${TMP}/shaders" 2>/dev/null
		for miShader in ${SHADFROM}/*
		do
			if [ -f $miShader ]; then
				rcp $miShader $miHost:${TMP}/shaders
				errcode=$?
				message="cannot find $miShader"
				check_errstat 
			fi
		done
	fi
fi
	rcp $miProgname $miHost:${TMP}/remote_install_ray
	$RSH $miHost  "touch ${TMP}/OK;
		chmod 700 ${TMP}/remote_install_ray;
   		${TMP}/remote_install_ray $miOptions ;
    		rm -f ${TMP}" 2>/dev/null
  	rexit_stat
	[ $miSilent = 0 ] &&
		echo "... Installation of $miHost finished ..."
done
}

#*******************************************************************************
# main for local installation
install_main()
{
if [ $miSilent = 0 ]; then
	[ $miDeinst = 0 ] && {
		echo
		echo "*** starting installation of mental ray on $HOST ***"
		echo
	}
	[ $miDeinst = 1 ] && {
		echo
		echo "*** starting deinstallation on $HOST ***"
		echo
	}
fi


if [ $miCustom = 1 ];then
	OINSTDIR=`grep INSTDIR $miConfig | awk '{print $2}' 2>/dev/null` 
	OINSTDIR=`echo $OINSTDIR | tr -s ! /`
	INSTDIR=${OINSTDIR:-$INSTDIR}
	[ $miDeinst = 0 ] && {
		echo
       		echo "Where is the mental ray installation directory ?[${INSTDIR}]"
       		read tmp
       		INSTDIR=${tmp:-${INSTDIR}}
		}
	[ `dirname $INSTDIR` = . ] && INSTDIR=${PWD}/`basename $INSTDIR`
	[ $INSTDIR != '/usr/local/mi' -a $miDeinst = 0 ] && {
		echo "WARNING: Set environment variable MI_ROOT to $INSTDIR"	
		echo
		}

	OINSTDIR=`echo $OINSTDIR | tr -s / !`
	INSTDIR=`echo $INSTDIR | tr -s / !`
	if [ x$OINSTDIR = x ];then
		echo "INSTDIR $INSTDIR" >> $miConfig
	else
		sed -e "/INSTDIR/ s/${OINSTDIR}/${INSTDIR}/" < $miConfig > ${miConfig}.tmp
		mv ${miConfig}.tmp $miConfig
	fi
fi
	INSTDIR=`echo $INSTDIR | tr -s ! /`

#root_check
install_ray
[ -d ${SHADFROM} ] && install_shaderlib
[ -f ${INSTDIR}/shaders/softimage.so ] && mk_softimage_links
get_network_information
add_spm_port
set_spmhost
add_ray_port
check_rayowner
change_inetd
}

#****************************************************************************
# 
install_slaves()
{
if [ $miCustom = 1 ]; then
	echo
	if [ $miDeinst = 1 ] ;then
		echo "deinstall mental ray on any other host [n]?"
	else
		echo "install mental ray on any other host [n]?"
	fi
	read tmp
	[ ${tmp:-n} = n ] && return
	echo "Enter the names of the designated hosts[]?"
	read tmp
       	miRemoteHosts=${tmp:-$miRemoteHosts}
fi


[ $miSilent = 0 ] && echo "... working ..."
start_remote_install
}

#****************************************************************************
# installation main of mi_install_ray
#****************************************************************************

init_var
if [ ! -f $miConfig ] ;then
	touch $miConfig
	errcode=$?
	message="Cannot touch $miConfig"
	check_errstat
else
	[ $miSilent = 0 ] && {
		echo
		echo ... using configuration file $miConfig ...	
		echo
	}
fi

if [ $miProgname = "${TMP}/remote_install_ray" ]; then
	miExt=`${TMP}/mi_checkos`
	[ $miExt = none ] && return
	install_main
 	exit 0 
fi
	
if [ $miSilent = 0 ]; then
	echo "**************************************************************"
	echo "NOTICE:"
	echo "If you intend to make any kind of remote installation"

	echo "root must be able to login there without any password questions"
	echo "Otherwise the installation script will be interrupted!"
	echo "**************************************************************"
	echo
fi

miExt=`${INSTFROM}/mi_checkos`
[ $miExt = none ] && return
	
if [ $miCustom = 0 ] ;then
	install_main
	exit 0
fi

miOldMaster=`grep miRayMaster $miConfig | awk '{print $2}' 2>/dev/null` 
miRayMaster=${miOldMaster:-$HOST}

[ $miDeinst = 1 ] && {
	echo "Where shall I deinstall mental ray [$miRayMaster]?"
	read tmp
	miRayMaster=${tmp:-$HOST} 
	}
if [ $miDeinst = 0 ]; then
	echo "mental ray master is $miRayMaster[y]?"
	read tmp
	[ ${tmp:-y} = n ] && { 
		echo "Which host?[$HOST]"
		read tmp
		miRayMaster=${tmp:-$HOST}
		}
	if [ x$miRayMaster != x ] ;then
		if [ x$miOldMaster != x ] ;then
			sed -e "/miRayMaster /s/$miOldMaster/$miRayMaster/" < $miConfig > $miConfig.tmp
			mv $miConfig.tmp $miConfig 1>/dev/null 2>&1
		else
			echo "miRayMaster $miRayMaster" >> $miConfig 2>/dev/null
		fi
	fi
fi
	[ $miRayMaster = $HOST ] && {
		[ $miExt = "irix6-n32" -a $miDeinst = 0 ] && {
			[ $miCustom = 1 ] && {
				echo
				echo Choose 64 bits if you have many threads or 
				echo very large scenes or textures, and 32 otherwise.
				echo Note that 32 and 64 bit executables and 
				echo shader libraries cannot be mixed.
			}
			echo "The 32bit version is the default on IRIX6."
			echo "Do you want to use the 64bit version instead[n]?"
			read tmp
			[ ${tmp:-n} = y ] && miExt=irix6-64
			RAYFROM=${ROOT}/${miExt}/bin
			SHADFROM=${ROOT}/${miExt}/shaders
			FROM=$RAYFROM
			}
		install_main
		}
	[ $miRayMaster != $HOST ] && {
		miRemoteHosts=$miRayMaster
		[ $miSilent = 0 ] && 
			 echo "... working ..."
		 start_remote_install
	}

install_slaves
exit 0
