#!/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/srcChanges/CD2/Gifts/bin/RCS/drender_submit.csh.sgi,v $
#       $Author: edirks $ $Date: 93/09/23 14:29:01 $
#
#

#
# Program to submit rendering to the simple network distributer
#


#
# Check to see if ALIAS_WORKENV and ALIAS_LOCATION are set
# Note that we assume that both ALIAS_WORKENV and ALIAS_LOCATION are
# nfs paths, and not relative to this machine. This is IMPORTANT!
#
onintr -
nohup

if (! $?ALIAS_WORKENV ) then
    echo "Environment variable ALIAS_WORKENV must be set for proper execution"
    exit -1
endif

if (! $?ALIAS_LOCATION ) then
    echo "Environment variable ALIAS_LOCATION must be set for proper execution"
    exit -1
endif

set NT_DR=${ALIAS_WORKENV}/user_data/demo/misc_data/dsubmit.pst

pst ${NT_DR} 
if ( $status == "1" ) then
    exit 1
endif

set NTsplit=`pstget ${NT_DR} '*.skip' '1'`
set NTfile=`pstget ${NT_DR} '*.sdlfile' ''`
set NTproject=`pstget ${NT_DR} '*.project' 'demo'`
set NTapp=`pstget ${NT_DR} '*.application' 'renderer'`
set NTdest=`pstget ${NT_DR} '*.dest' '/usr/tmp'`
set NToptions=`pstget ${NT_DR} '*.options' ''`
set NTprops=`pstget ${NT_DR} '*.props' ''`
set NTproperties=( Alias ${NTprops} )
set NTorder=`pstget ${NT_DR} '*.order' ''`

set NT_QUEUE_DIR=${NT_MASTER_DIR}/queue

if ( -e ${NTfile} ) then
    set NTrendfile=${NTfile}
    set NTfile=${NTfullfile}:t
    set NTfullfile=${NTrendfile}
else
    set NTrendfile=sdl/${NTfile}
    set NTfullfile=${ALIAS_WORKENV}/user_data/${NTproject}/${NTrendfile}
endif

if ( -e ${NTfullfile} ) then
    echo "Submitting: ${NTfullfile}"
else
    aliasWarning -s"Cannot find file ${NTfile}" -b"OK" >  /dev/null
    exit -1
endif

if ( "`pstget ${NT_DR} '*.source' 'from options'`" == "from options" ) then

    set NTstart=`pstget ${NT_DR} '*.start' '1'`
    set NTend=`pstget ${NT_DR} 'Start/End/By options.end' '1'`
    set NTby=`pstget ${NT_DR} '*.by' '1'`

else
    #
    # Find the start,end,by limits from the SDL file
    #
    set NTlimits=`NTgetlimits ${NTfullfile}`

    #
    # Check to see if user has overridden start/end/by. If not, use the
    # ones we found in the SDL file
    #
    set NTstart=`NTint ${NTlimits[1]}`

    set NTend=`NTint ${NTlimits[2]}`

    set NTby=`NTint ${NTlimits[3]}`

endif


umask 000

#
# Now create the jobs
#
@   NTskip = ${NTsplit} * ${NTby}
@   NTcurrent = $NTstart
@   NTseq     = 0

#
# We will loop throught the required frames, splitting them
# up into individual csh jobs. These are stored in the queue
# directory.
#
# The csh job is called <job>.job. There is also created
# <job>.lock which is a "delete to use" lock file. We also
# create <job>.prop that holds any properties *required*
# to run the job, and <job>.post that contains will move
# the files after they are generated (if this was requested).
#
while ( ${NTcurrent} <= ${NTend} )

    #
    # drender_order takes in the start, end, by arguements along
    # with the "frames per job" and generated-job-number and
    # prints out and job number that takes into account the
    # desired generation order
    #
    set NTcurrentseq=`NTorder "${NTorder}" ${NTstart} ${NTend} ${NTby} ${NTskip} ${NTseq}`

@   NTseq = ${NTseq} + 1
@   NTendcurrent = ${NTcurrent} + ${NTsplit} - 1
    if ( ${NTendcurrent} > ${NTend} ) then
        set NTendcurrent=${NTend}
    endif
 
    set NTjobname=${NT_QUEUE_DIR}/${NTcurrentseq}+${NTfile}+${NTcurrent}.job
    set NTpropname=${NT_QUEUE_DIR}/${NTcurrentseq}+${NTfile}+${NTcurrent}.prop
    set NTlockname=${NT_QUEUE_DIR}/${NTcurrentseq}+${NTfile}+${NTcurrent}.lock
    set NTpost=${NT_QUEUE_DIR}/${NTcurrentseq}+${NTfile}+${NTcurrent}.post

    #
    # If the user wanted the files moved after generation, then
    # create the post process script to do that here.
    #
    if ( "${NTdest}" != "" ) then
        echo '#\!/bin/csh' > ${NTpost}
        echo '#' >> ${NTpost}
        echo '/bin/cp $3' " ${NTdest}" >> ${NTpost} 
        echo '/bin/rm -f $3'       >> ${NTpost}
        chmod 777 ${NTpost}
        set  NT_POST_CMD=" -f ${NTpost} "
    else
        set  NT_POST_CMD=""
    endif


    #
    # Here we create the job that will actually do the rendering.
    # It does the following:
    #   1.  sets up any required environment variables.
    #   2.  changes (pushes) to the saved workenv
    #   3.  Runs the requested rendering job
    #   4.  returns to the initial directory
    #   5.  exits with the renderer status.
    #
    echo '#\!/bin/sh'                                 >  ${NTjobname}
    echo "ALIAS_WORKENV=${ALIAS_WORKENV}"       >> ${NTjobname}
    echo "ALIAS_LOCATION=${ALIAS_LOCATION}"     >> ${NTjobname}
    echo "NT_MASTER_DIR=${NT_MASTER_DIR}"       >> ${NTjobname}
    echo "export ALIAS_WORKENV"                 >> ${NTjobname}
    echo "export ALIAS_LOCATION"                >> ${NTjobname}
    echo 'opwd=`pwd`'                           >> ${NTjobname}
    echo "cd ${ALIAS_WORKENV}/user_data/${NTproject}" >> ${NTjobname}

    set NTrunapp=${ALIAS_LOCATION}/Gifts/bin/drender_run
    echo "${NTrunapp} ${ALIAS_LOCATION}/bin/${NTapp} -q1 ${NToptions} ${NT_POST_CMD}  -s ${NTcurrent} -e ${NTendcurrent} -b ${NTby} ${NTrendfile} &" >> ${NTjobname} 
    echo 'sleep 3'                                     >> ${NTjobname}
    echo 'RPID=`cat /usr/tmp/drender_pid`'             >> ${NTjobname}
    echo 'NTrunning="TRUE"'                            >> ${NTjobname}
    echo 'while [ "$NTrunning" = "TRUE" ] '            >> ${NTjobname}
    echo '    do'                                      >> ${NTjobname}
    echo '    if [ "`ps -p $RPID | grep $RPID`" = "" ];then ' >> ${NTjobname}
    echo '        NTrunning="FALSE"'                   >> ${NTjobname}
    echo '    fi'                                      >> ${NTjobname}
    echo '    if [ -f ${NT_MASTER_DIR}/config/`hostname`.locked ];then ' >> ${NTjobname}
    echo '        kill -9 $RPID'                       >> ${NTjobname}
    echo '        NTrunning="KILLED"'                  >> ${NTjobname}
    echo '    fi'                                      >> ${NTjobname}
    echo '    sleep 30'                                >> ${NTjobname}
    echo 'done'                                        >> ${NTjobname}
    echo 'if [ $NTrunning = "FALSE" ];then'            >> ${NTjobname}
    echo '    sleep 3'                                 >> ${NTjobname}
    echo '    NTstat=`cat /usr/tmp/drender_status`'    >> ${NTjobname}
    echo 'else'                                        >> ${NTjobname}
    echo '    NTstat="255"'                            >> ${NTjobname}
    echo 'fi'                                          >> ${NTjobname}
    echo '/bin/rm /usr/tmp/drender_pid'                >> ${NTjobname}
    echo '/bin/rm /usr/tmp/drender_status'             >> ${NTjobname}
    echo 'cd ${opwd}'                                  >> ${NTjobname}
    echo 'exit ${NTstat}'                              >> ${NTjobname}

    #
    # Here we create the file containing the machine properties we 
    # require to render the file. This file will later be "sourced"
    # to retrieve the value of the property variable.
    #    
    echo "set  RENDER_PROPERTIES=(${NTproperties})" > ${NTpropname}
    
    #
    # Now we create the lock file, and thus make the file available for
    # rendering.
    #
    echo "`date`" > ${NTlockname}

@   NTcurrent = ${NTcurrent} + ${NTskip}
end

exit








