#!/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_kill.csh.sgi,v $
#       $Author: psantan $ $Date: 93/06/23 16:05:39 $
#
#
#
#   This script kills a drender daemon on a given machine
#

#
#   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/dkill.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'` )

    foreach NT_HOST_ITEM ( ${NT_HOST_LIST} ) 

        if ( ${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

        #
        # Maker 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

        echo "Removing network renderer from host ${NT_HOST}."

        #
        # Here we check to see if there is a drender daemon recorded as
        # running on the 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}`)
            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

                    if ( `aliasWarning  -s"Kill distributed renderer on ${NT_HOST}, PID=${NT_OLD_PID}?" -b"Yes" -b"No"` == "Yes" ) then

                        ${NT_RCMD} csh -f ${NT_MASTER_DIR}/bin/${NT_HOST}.kill 
                        /bin/rm -f ${NT_MASTER_DIR}/bin/${NT_HOST}.kill
                        /bin/rm -f ${NT_MASTER_DIR}/bin/${NT_HOST}.pid
                        /bin/rm -f ${NT_MASTER_DIR}/bin/${NT_HOST}.check
                        /bin/rm -f ${NT_MASTER_DIR}/config/${NT_HOST}
                        /bin/rm -f ${NT_MASTER_DIR}/log/${NT_HOST}
                    endif
                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

            /bin/rm -f ${NT_TMP2}
        endif 
    end
endif
