#!/bin/sh 
###############################################################################
# Copyright 1997-1998 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.1.42.2 1998/10/14 14:26:47 cricard Exp $
#
# functions:
#
#	check_errstat
#	rexit_stat
#	read_config
#	root_check
#	check_os
#	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"
#echo "	-r recover configuration from file (not implemented yet)"
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";;
	-r) miRecover=0; miOptions="$miOptions -r";;
	-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
INSTDIR=${INSTDIR:=/usr/local/mi}
RAYFROM=${RAYFROM:=../bin}
INCFROM=${INCFROM:=../include}
SHADFROM=${SHADFROM:=../shaders}
INSTFROM=${INSTFROM:=../install}
TMP=${TMP:=/tmp}
miCustom=${miCustom:=1}
miSilent=${miSilent:=0}
miRecover=${miRecover:=0}
miDeinst=${miDeinst:=0}
FROM=$RAYFROM
OS=`uname`
HOST=`hostname`
miProc=default
miHost=$HOST
miProgname="$0"
miExt=none
miGenExt=none
miDNS=0
miNIS=0
miNISmaster=$HOST
miNISservice=0
miNIShosts=0
miDNSserver=$HOST
miSupported=1
miRayOwner=nobody
miPrinted=0
miConfig=miconfig
miOverwrite=false
miChangeInetd=false
miSpmPortNo=7050
miSpmPort=mi-spm
miInetFile=/etc/inetd.conf
miServiceFile=/etc/services
miRayPort=mi-ray

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


# read configuration file from former installations
read_config()
{
eval `grep INSTDIR ${INSTFROM}/$miConfig`
eval `grep miRayOwner ${INSTFROM}/$miConfig`
eval `grep miRayMaster ${INSTFROM}/$miConfig`
eval `grep miOverwrite ${INSTFROM}/$miConfig`
eval `grep miChangeInetd ${INSTFROM}/$miConfig`
eval `grep miSlaveHosts ${INSTFROM}/$miConfig`
eval `grep miSpmPortNo ${INSTFROM}/$miConfig`
}


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
	}
}

# 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`
[ $OK ] || 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
}


# check for supported operating system versions
# set system dependend system calls 
check_os()
{
[ $miDeinst = 1 ] && return
miSupported=1
[ $miHost != $HOST ] && UNAME="$RSH $miHost  $UNAME"
case $OS in 
	SunOS)
	        OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`
	        [ $OSversion = 5 ] && {
	                miExt=sunos5
			miGenExt=sunos				
	                return
	                }
		;;
	AIX)
		OSversion=`$UNAME -v`
		[ $OSversion = 4 ] && {
			miExt=aix4
			miGenExt=aix
			return
			}
		;;
	OSF1)
		OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`
		[ $OSversion = V4 ] && {
			miExt=decunix4
			miGenExt=decunix
			return
			}
		;;
	HP-UX)
		OSversion=`$UNAME -r | awk -F. '{print $2}' 2>/dev/null`
		miProc=`$UNAME -m | awk -F/ '{if ($2 <= 800) print "PA1.0"; 
			else print "PA2.0"}'`
		[ $OSversion = 09 ] && {
			miExt=hpux9
			miGenExt=hpux
			return
			}
		[ $OSversion = 10 ] && {
			miExt=hpux10
			miGenExt=hpux
			[ $miProc = PA1.0 ] && miExt=hpux9
			[ -f /bin/sysinfo ] && miExt=sppux5
			return
			}
		[ $OSversion = 11 ] && {
			miExt=hpux11
			miGenExt=hpux
			[ $miProc = PA1.0 ] && miExt=hpux9
			return
			}
		;;
	IRIX)
		OSversion=`$UNAME -r  2>/dev/null`
		[ $OSversion = 5.2 -o $OSversion = 5.3 ] && {
			miExt=irix5-32
			miGenExt=irix
			return
			}
		[ $OSversion = 6.2 -o $OSversion = 6.3 ] && {
			miExt=irix6-n32
			miGenExt=irix
			return
			}
		;;
	IRIX64)
		OSversion=`$UNAME -r | awk -F. '{print $1}' 2>/dev/null`
		miExt=irix6-n32
		miGenExt=irix
		return
		;;
	 Linux)
		OSversion=`$UNAME -m 2>/dev/null`
		[ $OSversion = i586 -o $OSversion = i686 ] && {
			miExt=linux-x86
			miGenExt=linux-x86
			return
			}
		[ $OSversion = alpha ] && {
			miExt=linux-alpha
			miGenExt=linux-alpha
			return
			}
		;;
esac

echo "Sorry, platform $OS $OSversion is not supported"
[ $miSilent = 0 -a $miPrinted = 0 ] && {
	miPrinted=1
	echo "... supported platforms are: ..."
	echo "  IRIX5.2"
	echo "  IRIX5.3"
	echo "  IRIX6.2"
	echo "  IRIX6.3"
	echo "  IRIX64 6.2"
	echo "  IRIX64 6.3"
	echo "  IRIX64 6.4"
	echo "  AIX4.x"
	echo "  HP-UX9.x"
#	echo "  HP-UX10.x"
	echo "  SPPUX5.x"
	echo "  SunOS5.x"
	echo "  OSF1 V4.x"
	echo "  Linux x86"
	echo "  Linux Alpha"
	}

echo
echo "NOTICE: $miHost will be excluded from installation"
[ $miProgname = "${TMP}/remote_install_ray" ] && { 
		rm -f ${TMP}/OK
		exit 1
		}
miSupported=0
}

#*******************************************************************************
# 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 -a $miRecover = 0 ]; then
		echo
		echo "Overwrite ray in ${INSTDIR}/bin [y]?"
		read tmp
		[ ${tmp:-y} = n ] && return
	fi
	[ $miRecover = 1 -a $miOverwrite = false ] && return
fi

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

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

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

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

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

for miInc in ${INCFROM}/*
do
	cp $miInc ${INSTDIR}/inc 1>/dev/null 2>&1
	errcode=$?
	message="cannot copy $miInc to ${INSTDIR}/inc"
	check_errstat
	chmod 644 ${INSTDIR}/inc/* 1>/dev/null 2>&1
done
 
[ -h /usr/include/mi ] && rm -f /usr/include/mi
ln -s ${INSTDIR}/inc /usr/include/mi 1>/dev/null 2>&1
errcode=$?
message="linking ${INSTDIR}/inc 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}/$miExt to ${INSTDIR}/shaders"
	[ $rayrc = 1 ]  && echo "... creating ${INSTDIR}/rayrc ..."
	}

[ $rayrc = 1 ] && {
	[ -f ${INSTDIR}/rayrc -a ! -f ${INSTDIR}/rayrc.old ] && 
		mv ${INSTDIR}/rayrc ${INSTDIR}/rayrc.old 1>/dev/null 2>&1
	rm -f ${INSTDIR}/rayrc
	touch ${INSTDIR}/rayrc 1>/dev/null 2>&1
	chmod 644 ${INSTDIR}/rayrc	 1>/dev/null 2>&1
}

for miShader in ${SHADFROM}/${miExt}/*
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 \"${INSTDIR}/shaders/`basename $miShader`\"" >> ${INSTDIR}/rayrc
	}
done
}

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

mk_softimage_links()

{

[ $miDeinst = 1 ] && return 

if [ $miCustom = 1 ] ;then
	echo
	echo "Do you want to use mental ray from within Softimage|3D [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

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

if [ $miCustom = 1 ] ;then
	echo
	echo "Where is the Softimage directory [$SOFTDIR]?"
	read tmp
	SOFTDIR=${tmp:-$SOFTDIR}
	SI_LOCATION=$SOFTDIR; export SI_LOCATION
fi

SOFTSHADIR=${SOFTDIR}/mental_ray/MR_Shaders/Shader_Lib/lib

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

for miInc in ${INSTDIR}/inc/*
do
	miInc=`basename $miInc`
	if [ -f ${SOFTDIR}/mental_ray/inc/$miInc ]; then
		[ ! -f ${SOFTDIR}/mental_ray/inc/$miInc.orig ] && 
			mv ${SOFTDIR}/mental_ray/inc/$miInc                                                ${SOFTDIR}/mental_ray/inc/$miInc.orig
		rm -f ${SOFTDIR}/mental_ray/inc/$miInc
	fi
	ln -s ${INSTDIR}/inc/$miInc ${SOFTDIR}/mental_ray/inc/$miInc
	errcode=$?
	message="cannot link $miInc to ${SOFTDIR}/mental_ray/inc"
	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}/inc/softimage.mi ${SOFTDIR}/3D/rsrc 1>/dev/null 2>&1
errcode=$?
message="cannot link ${INSTDIR}/inc/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 "... 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 ] && {
                echo "... you are using DNS for host name resolution ..."
                echo "... your nameserver has the address: $miDNSserver ..."
        }
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 "... port $miSpmPort has to be added to $miServiceFile ..."
	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 "Add the following line to $miNISmaster's $miServiceFile" 
	echo "$miSpmPort	$miSpmPortNo/tcp # SW protection"
	echo "WARNING:"
	echo "Without this entry mental ray will not find licenses."

	if [ $miCustom = 1 -a $miRecover = 0 ] ; then
		echo
		echo "Type RETURN when finished"
		read tmp
	fi
	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 -a $miRecover = 0 ]; 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 ..."
	echo "... host database has to be changed ..."
	}

spmhost=`grep spmhost0 $hostfile| awk '{print $2}'`
if [ x$spmhost = x ]; then
	spmhost=$HOST
	[ $miCustom = 1 ] && {
		echo
		echo "Which host is your license server [$HOST]?"
		read tmp		
		spmhost=${tmp:-$HOST}
	}
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 nickname spmhost0 to 
	      $spmhosts's entry in ${miNISmaster}'s $hostfile"
	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 "Add nickname spmhost0 to $spmhost's entry in 
	      $miNISmasters $hostfile"

        if [ $miCustom = 1 ] ; then
		echo
                echo "Type RETURN when finished"
                read tmp
        fi
        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: $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: 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=7001
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 "... $miRayPort has to be added to $miServiceFile ..."
	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 "${miRayPort}	${RayPortNumber}/tcp # MR ray tracer"
	echo "NOTICE:"
	echo "Without this entry mental ray cannot run in network mode"
	}

	if [ $miCustom = 1 ] ; then
		echo
		echo "Type RETURN when finished"
		read tmp
	fi
	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 -a $miRecover = 0 ] && {
	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 -a $miRecover = 0 ] ;then
	echo
	echo "Change $miInetFile for you [y]?"
	read tmp
	[ ${tmp:-y} = n ] && return
fi

[ $miRecover = 1 -a $miChangeInetd = false ] && return

[ $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
	init_var
	OS=`$RSH $miHost  $UNAME 2>/dev/null`
	[ $? != 0 ] && {
		echo "cannot login on $miHost"
		echo "NOTICE: $miHost will be excluded"
		return
		}
 	check_os
 	[ $miSupported = 0 ] && continue
 	trap "$RSH $miHost  'rm -f ${TMP}/remote_install_ray ;                                               rm -f ${TMP}/ray.${miExt}; rm -rf ${TMP}/inc;                                   rm -rf ${TMP}/${miExt}'" 2 15
	[ miRecover = 1 ] && rcp ${INSTFROM}/$miConfig $miHost:${TMP}/$miConfig
if [ $miDeinst = 0 ]; then
	rcp ${FROM}/ray.$miExt $miHost:${TMP}/ray.$miExt
	errcode=$?;message="cannot find ${FROM}/ray.$miExt";check_errstat
	[ -f ${FROM}/iray.$miExt ] && {
		rcp ${FROM}/iray.$miExt $miHost:${TMP}/iray.$miExt
		errcode=$?
		message="cannot find ${FROM}/iray.$miExt"
		check_errstat
		}
	[ -f ${FROM}/imf_disp.$miGenExt ] && {
		rcp ${FROM}/imf_disp.$miGenExt $miHost:${TMP}/ray.$miGenExt
		errcode=$?
		message="cannot find ${FROM}/imf_disp.$miGenExt"
		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}/$miExt ]; then
		$RSH $miHost  "mkdir ${TMP}/$miExt" 2>/dev/null
		for miShader in ${SHADFROM}/${miExt}/*
		do
			if [ -f $miShader ]; then
				rcp $miShader $miHost:${TMP}/${miExt}
				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}/remote_install_ray;
		rm -f ${TMP}/ray.$miExt;
 		rm -rf ${TMP}/${miExt};
 		rm -rf ${TMP}/inc" 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

check_os
[ $miSupported = 0 ] && return

if [ $miCustom = 1 -a $miRecover = 0 ];then
	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/' ] && {
		echo "WARNING: Set environment variable MI_ROOT to $INSTDIR"	
		echo
		}
fi

root_check
install_ray
[ -d ${SHADFROM}/$miExt ] && install_shaderlib
mk_softimage_links
get_network_information
add_spm_port
set_spmhost
add_ray_port
check_rayowner
change_inetd
}

#****************************************************************************
# 
install_slaves()
{
if [ $miCustom = 1 -a $miRecover = 0 ]; 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

[ $miRecover = 1 ] && miRemoteHosts=$miSlaveHosts

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

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

init_var
[ $miRecover = 1 ] && read_config

if [ $miProgname = "${TMP}/remote_install_ray" ]; then
	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 "**************************************************************"
fi

if [ $miCustom = 0 ] ;then
	install_main
	exit 0
fi

if [ $miRecover = 0 ];then
	echo "mental ray master is $HOST[y]?"
		read tmp
		[ ${tmp:-y} = y ] && miRayMaster=$HOST

	[ $miRayMaster ] || {
		echo "Which host? []"
		read tmp
		miRayMaster=${tmp:-$miRayMaster}
		}
fi

if [ x$miRayMaster != x ] ;then
	[ $miRayMaster = $HOST ] && install_main
	[ $miRayMaster != $HOST ] && {
		miRemoteHosts=$miRayMaster
		[ $miSilent = 0 ] && 
			 echo "... working ..."
		 start_remote_install
	}
fi

install_slaves
exit 0
