#!/bin/csh -f 
#*****************************************************************************
#                                                                            *
#            Copyright (C) 1993, Alias Research, Inc.                        *
#                                                                            *
#    These coded instructions,  statements and  computer programs contain    *
#    unpublished information proprietary to Alias Research, Inc.  and are    *
#    protected by the Canadian and US Federal copyright law. They may not    *
#    be disclosed to third parties  or copied  or duplicated, in whole or    *
#    in part,  without the prior written consent of Alias Research,  Inc.    *
#                                                                            *
# Unpublished-rights reserved under the Copyright Laws of the United States. *
#                                                                            *
#****************************************************************************/
#
#       $Source: /tmp_mnt/repo/root/v/alias3.2.2/src/Gifts/bin/RCS/drender_install.csh.sgi,v $
#       $Author: psantan $ $Date: 93/06/30 17:55:13 $
#
#
#
#   This script installs the drender deamon on a machine.
#   Note that it is a graphical app, and thus must be run from the
#   SGI console.
#
#   
#
#   Ensure that all of the required environment variables are set
#
if (! $?ALIAS_LOCATION ) then
    echo "ALIAS_LOCATION environment variable must be set to install drender"
    exit -1
else
    #
    # Here we set NT_DR to be the file that contains the dialogue and
    # settings information for the installation control panel.
    #
    set NT_DR=${ALIAS_WORKENV}/user_data/demo/misc_data/dsetup.pst

    #
    # Display the installation control panel, and allow the user to
    # interact with it.
    #
    pst ${NT_DR}

    #
    # If the user pressed the "cancel" button, then the control panel
    # program will have exited with a 1 status.
    #
    if ( $status == "1" ) then
        exit 1
    endif

    #
    # Here we retrieve all of the control panel settings into
    # csh variables.
    #
    set NT_HOST_LIST=( `pstget ${NT_DR} '*.host' 'localhost'` )
    set NT_RETRY=`pstget ${NT_DR} '*.retry' '1'`
    set NT_BOOT=`pstget ${NT_DR} '*.boot' '0'`
    set NT_PROPS=`pstget ${NT_DR} '*.props' 'none'`
    set NT_LOG_TRUNC=`pstget ${NT_DR} '*.log_truncate' '1'`
    set NT_LOG_LINES=`pstget ${NT_DR} '*.log_lines' '100'`
    set NT_TEMP_EXE=/usr/tmp/DR.$$
    set NT_LOG_VERBOSE="0"

    foreach NT_HOST_ITEM ( ${NT_HOST_LIST} ) 

        if ( ${NT_HOST_ITEM} == "`hostname`" || ${NT_HOST_ITEM} == "localhost" || ${NT_HOST_ITEM} == "" || ${NT_HOST_ITEM} == " " ) \
            then

            set NT_HOST=`hostname`
            set NT_RCMD=""
        else
            
            set NT_HOST=${NT_HOST_ITEM}
            set NT_RCMD="rsh ${NT_HOST} "
        endif

        #
        # Make sure that the user has the appropriate permissions on
        # the control area. Note that no attempt is made to protect
        # the control area.
        #
        if (! -w ${NT_MASTER_DIR} ) then

            echo "You must have write permission on ${NT_MASTER_DIR} to install"
            exit -2
        endif

        #
        # We set a umask to 000 so that other users can read/write 
        # from the control area. This is pretty "liberal", but it is
        # assumed that this will not be run in a "hostile" environment.
        #
        umask 000
        echo "Installing network renderer on host ${NT_HOST}."

        #
        # The install program will now BUILD a csh deamon to periodically
        # check for jobs. The deamon is being created into the file
        # ${NT_TEMP_EXE}
        #

        #
        # The first thing generated script does is echo its process ID to
        # stdout. We capture this to allow us to kill the process later
        # if desired.
        #
	    echo '#\!/bin/csh -f '				      >   ${NT_TEMP_EXE}
        echo 'nohup'                              >>  ${NT_TEMP_EXE}
        echo 'onintr -'                           >>  ${NT_TEMP_EXE}
        #
        # The first thing we do is output the PID, which we will capture
        # when we run the exe.
        #
        echo 'echo $$'                            >>  ${NT_TEMP_EXE}
        #
        # Record the control area in the deamon
        #
        echo "setenv NT_MASTER_DIR ${NT_MASTER_DIR}"          >> ${NT_TEMP_EXE}

        #
        # We will now create 3 csh lists, one corresponding to each day
        # in the render schedule. They are indexed (1-7) from Sunday to
        # Saturday, and the lists contain the active status, start hour
        # and end hours respectively.
        #
        set NT_ACTIVE=""
        set NT_START_HOUR=""
        set NT_END_HOUR=""

        foreach day ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday )

            set NT_ACTIVE=(${NT_ACTIVE} `pstget ${NT_DR} ${day}.Active '1'`)
            set NT_START_HOUR=(${NT_START_HOUR} `pstget ${NT_DR} ${day}.starthour '1'`)
            set NT_END_HOUR=(${NT_END_HOUR} `pstget ${NT_DR} ${day}.endhour '1'`)
        end

        #
        # Here we record the generated lists in the deamon script
        #
        echo "set NT_START_HOUR=(${NT_START_HOUR})"               >> ${NT_TEMP_EXE}
        echo "set NT_END_HOUR=(${NT_END_HOUR})"                   >> ${NT_TEMP_EXE}
        echo "set NT_ACTIVE=(${NT_ACTIVE})"                       >> ${NT_TEMP_EXE}

        #
        # Record other variables the script will need to see. These
        # variables are stored in the environment (setenv) so that
        # the job checking script will inherit them when run by 
        # the daemon.
        #
        echo "setenv NT_RETRY ${NT_RETRY}"                        >> ${NT_TEMP_EXE}
        echo "setenv NT_BIN_DIR ${ALIAS_LOCATION}/Gifts/bin"      >> ${NT_TEMP_EXE}
        echo "setenv NT_LOG_TRUNC ${NT_LOG_TRUNC}"                >> ${NT_TEMP_EXE}
        echo "setenv NT_LOG_LINES ${NT_LOG_LINES}"                >> ${NT_TEMP_EXE}
        echo "setenv NT_LOG_VERBOSE ${NT_LOG_VERBOSE}"            >> ${NT_TEMP_EXE}

        #
        # Output the main checking loop in the script. We check to
        # see if we are active, whether we are scheduled to render,
        # and then if so, we run the render check/run script.
        #
        echo "while ( 1 )"                                        >> ${NT_TEMP_EXE}
        echo '@     day=`date +%w` + 1'                           >> ${NT_TEMP_EXE}
        echo '@     hour=`date +%H`'                              >> ${NT_TEMP_EXE}
        echo '  if ( ${NT_ACTIVE[$day]} == "1" ) then'            >> ${NT_TEMP_EXE}
        echo '     if ( ($hour <  ${NT_END_HOUR[$day]}  ||  \'    >> ${NT_TEMP_EXE}
        echo '           $hour >= ${NT_START_HOUR[$day]}) && \'   >> ${NT_TEMP_EXE}
        echo '          (! -e ${NT_MASTER_DIR}/config/`hostname`.locked) \' >> ${NT_TEMP_EXE}
        echo '                                          ) then'   >> ${NT_TEMP_EXE}
        echo "          ${ALIAS_LOCATION}/Gifts/bin/drender_check >> ${NT_MASTER_DIR}/log/${NT_HOST} ">> ${NT_TEMP_EXE}
    
        if ( "${NT_LOG_TRUNC}" == "1" ) then

            echo "          tail -${NT_LOG_LINES} ${NT_MASTER_DIR}/log/${NT_HOST}"' > /usr/tmp/LG.$$' >> ${NT_TEMP_EXE}
            echo '          cat /usr/tmp/LG.$$ >'" ${NT_MASTER_DIR}/log/${NT_HOST}" >> ${NT_TEMP_EXE}
            echo '          /bin/rm /usr/tmp/LG.$$' >> ${NT_TEMP_EXE}
        endif

        echo "     endif"                                         >> ${NT_TEMP_EXE}
        echo "  endif"                                            >> ${NT_TEMP_EXE}

        #
        # Here output the sleep period command to the deamon
        # script.
        #
        echo "  sleep 60"                                         >> ${NT_TEMP_EXE}

        #
        # Close the check loop
        #
        echo "end"                                              >> ${NT_TEMP_EXE}

        #
        # Make sure that the control location exists, if not
        # then offer to create it.
        #
        if (! -w ${NT_MASTER_DIR}/config ) then

            if ( `aliasWarning -s"Control Location does not exist. Create it?"  -b"Yes" -b"No"` == "Yes" ) then
                echo "Creating drender control area"
                mkdir ${NT_MASTER_DIR}/config
                mkdir ${NT_MASTER_DIR}/log
                mkdir ${NT_MASTER_DIR}/bin
                mkdir ${NT_MASTER_DIR}/queue
                mkdir ${NT_MASTER_DIR}/failed
                mkdir ${NT_MASTER_DIR}/completed
                mkdir ${NT_MASTER_DIR}/running

                chmod 777 ${NT_MASTER_DIR}/config
                chmod 777 ${NT_MASTER_DIR}/log
                chmod 777 ${NT_MASTER_DIR}/bin
                chmod 777 ${NT_MASTER_DIR}/queue
                chmod 777 ${NT_MASTER_DIR}/failed
                chmod 777 ${NT_MASTER_DIR}/completed
                chmod 777 ${NT_MASTER_DIR}/running
            else
                echo "Installation aborted"
                exit 1
            endif

        endif

        #
        # Here we set up any user requested machine properties.
        # At render submit time, a user can request processing by machines
        # that have these properties only.
        #
        echo "set MACHINE_PROPERTIES=( Alias ${NT_PROPS} )" >          \
                ${NT_MASTER_DIR}/config/${NT_HOST}


        #
        # Here we copy the daemon script we just created into the 
        # control location bin directory.
        #
        /bin/cp ${NT_TEMP_EXE} ${NT_MASTER_DIR}/bin/${NT_HOST}.check

        /bin/rm -f ${NT_TEMP_EXE}
        #
        # Here we check to see if there is a drender daemon recorded as
        # running on this host. If so, then we offer to kill it (as this
        # will replace it)
        #
        if (-e ${NT_MASTER_DIR}/bin/${NT_HOST}.kill) then

	        set NT_OLD_PID=`csh -f ${NT_MASTER_DIR}/bin/${NT_HOST}.pid`

            #
            # now do a ps to see if such a process exists, and whether it
            # is *likely* a distributed renderer
            #
            set NT_TMP2=/usr/tmp/NT_TMP2.$$
            ${NT_RCMD} ps -p ${NT_OLD_PID} -f > ${NT_TMP2}
   
            set NT_WC_ARRAY=(`wc ${NT_TMP2}`)

            # skip the header and read the process line
            set NT_PS_RET=(`tail -1 ${NT_TMP2}`)

            /bin/rm -f ${NT_TMP2}

            set NT_PS_IND=$#NT_PS_RET


            if ( $NT_WC_ARRAY[1] == "2" ) then

                set NT_COMSTR=$NT_PS_RET[$NT_PS_IND]

                if ( $NT_COMSTR:t == "${NT_HOST}.check" ) then

                    echo "Killing previous distributed renderer on ${NT_HOST}, PID=${NT_OLD_PID}."
                    ${NT_RCMD} csh -f ${NT_MASTER_DIR}/bin/${NT_HOST}.kill 

                else
                    aliasWarning -s"Distributed renderer on ${NT_HOST}, PID=${NT_OLD_PID} is no longer running" -b "Ok" > /dev/null
                endif

            else
                aliasWarning -s"Distributed renderer on ${NT_HOST}, PID=${NT_OLD_PID} is no longer running" -b "Ok" > /dev/null
            endif
        endif 

        set NT_TMP=/usr/tmp/DRT.$$

        #
        # Here we run the drender daemon script, capturing the resulting PID
        #
        set NT_TMP_PID=/usr/tmp/DRPID.$$
        ${NT_RCMD} /bin/csh -f ${NT_MASTER_DIR}/bin/${NT_HOST}.check > ${NT_TMP_PID} & 

@       timout=0
        while ( -z ${NT_TMP_PID} ) 
@           timeout++
            if ( $timeout > 120 )  then

                 echo "Could not start remote renderer on ${NT_HOST}"
                 /bin/rm ${NT_TMP_PID}
                 exit 1
            endif
            sleep 1
        end
        set NT_PID=`head -1 ${NT_TMP_PID}`

        /bin/rm ${NT_TMP_PID}

        echo "Process ID for Distributed Renderer on ${NT_HOST} is ${NT_PID}"

        #
        # Create a kill file for killing the process later
        #
        set NT_KILL_FILE=${NT_MASTER_DIR}/bin/${NT_HOST}.kill
        echo "${NT_RCMD} kill -9 ${NT_PID}" 			> ${NT_KILL_FILE}

        #
        # Create a pid file to retrieve the pid of the process later
        #
        set NT_ECHO_PID=${NT_MASTER_DIR}/bin/${NT_HOST}.pid
        echo "echo ${NT_PID}"			> ${NT_ECHO_PID}

        if ( ${NT_BOOT} == '1' ) then

            #
            # The user has requested auto-restart on boot. Thus we have
            # to stick a script in /etc/rc2.d to restart the net renderer
            # when we come up.
            #
            set NT_TMP3="/usr/tmp/S99drender_auto.${NT_HOST}"

            echo '#\!/bin/sh'    				      >   ${NT_TMP3}
            echo '#'                                  >>  ${NT_TMP3}
            echo '# Script to start up Alias distributed renderer' >> ${NT_TMP3}
            echo "# Created on `date`"                >>  ${NT_TMP3}
            echo '#'                                  >>  ${NT_TMP3}
            echo 'if $IS_ON verbose ; then'           >>  ${NT_TMP3}
            echo '    ECHO=echo'                      >>  ${NT_TMP3}
            echo '    VERBOSE=-v'                     >>  ${NT_TMP3}
            echo 'else'                               >>  ${NT_TMP3}
            echo '    ECHO=:'                         >>  ${NT_TMP3}
            echo '    VERBOSE='                       >>  ${NT_TMP3}
            echo 'fi'                                 >>  ${NT_TMP3}
            echo 'case $1 in'                         >>  ${NT_TMP3}
            echo "'start')"                           >>  ${NT_TMP3}
    
            echo "NT_MASTER_DIR=${NT_MASTER_DIR}"     >>  ${NT_TMP3}
            echo "/bin/csh -f ${NT_MASTER_DIR}/bin/${NT_HOST}.check &" >> ${NT_TMP3}
            echo 'echo "rsh `hostname` kill -9 $\!" > ${NT_MASTER_DIR}/bin/`hostname`.kill ' >> ${NT_TMP3}

            echo ';;'                                 >>  ${NT_TMP3}

            echo "'stop')"                            >>  ${NT_TMP3}
    
            echo ';;'                                 >>  ${NT_TMP3}
            echo 'esac'                               >>  ${NT_TMP3}    

            aliasWarning -s "To enable auto-startup, copy the file ${NT_TMP3}" \
                         -s "to the /etc/rc2.d directory on ${NT_HOST}" \
                         -s "NOTE: this will likely require root priviledges"\
                         -b "OK" > /dev/null

        else
            #
            # Auto-start has not been requested. Ensure that any previous auto
            # start scripts are removed.
            #
            if ( "`rsh ${NT_HOST} /bin/ls /etc/rc2.d | /bin/grep S99drender_auto`" != "" ) \
                then

                aliasWarning -s "Please ensure that the file"\
                             -s "/etc/rc2.d/S99drender_auto.${NT_HOST}"\
                             -s "on ${NT_HOST} is removed" \
                             -s "NOTE: this will likely require root priviledges" \
                             -b "OK" > /dev/null
            endif

            /bin/rm -f /usr/tmp/check.$$

        endif

    end

endif




