#!/usr/bin/ksh
#------------------------------------------------------------------------------
# File Name: DT_shutdown
# Descriptive File Name: Shutdown all running DT processes
#------------------------------------------------------------------------------
#
# Licensed Materials - Property of IBM
# 5765-B81
# (C) Copyright IBM Corp. 1990, 1997.
# All rights reserved.
# US Government Users Restricted Rights
# Use, duplication or disclosure restricted by GSA ADP Schedule
# Contract with IBM Corp.
#
#------------------------------------------------------------------------------
#
# @(#) tools/DT_shutdown, System_Management, Tiger SID=1.7 modified 97/07/23 16:21:52 extracted 97/08/14 13:47:28
#
# Change History:
#
# < where modifications to file contents are not described in
#   function prologue change histories>
#
# Date          Description
# ----------  -----------------------------------------------------------------
# 21-Mar-97     YukLun  19626   Initial version
# 26-Mar-97     yuklun  20842   Remove unnecessary su
# 16-Apr-97     yuklun  21271   Kill dtsnmpd and dtalarmd at the very end
# 02-Jun-97     NickB   22126   Remove background stats entries from crontab
#------------------------------------------------------------------------------

if [ -z "$VAE" ]; then
   export VAE=/usr/lpp/dirTalk
fi

export PATH=/usr/bin:/usr/sbin:/etc
unset FULL_CLEANUP
LANG=C

# 'cleanup' requires full dt6 environment to run, otherwise the wrong files
# may get deleted (it does lots of "rm -f" without first checking to see
# if the environment is correctly set up!)

if [ -f $VAE/sw/environ/.vaeprofile -a \
     -x $VAE/sw/environ/.vaeprofile ]
then
    . $VAE/sw/environ/.vaeprofile
    FULL_CLEANUP=1
else
    echo "Cannot find \$VAE/sw/environ/.vaeprofile - partial cleanup only"
    export PATH=$VAE/tools:$VAE/sw/bin:$PATH
fi

function zap
{
    # $1 = process to zap
    # $2 = signal number to use
    # $3 = how long to wait for process to exit

    typeset -i timeout
    typeset -i poll_freq=5

    ps -ef -ocomm,pid | fgrep "$1" | while read com pid; do

        if [ "$com"="$1" ]; then
            timeout=$3
            kill -$2 $pid
            while kill -0 $pid > /dev/null 2>&1 ; do
                timeout=timeout-poll_freq
                sleep $poll_freq
                if [ $timeout -le 0 ]; then
                    break
                fi
            done

            # if the process still exists, send it a SIGKILL
            if ps $pid > /dev/null ; then
                kill -9 $pid
                sleep 5
            fi
        fi
    done
}

# Remove background statistics reporting entries from crontab
DTschedule delete

zap NODEM 2 600
rm -rf $SYS_DIR/system_running

if [ -n "$FULL_CLEANUP" ]; then

    # If we are root, then su to dtuser to avoid network
    # file permission problems.

    USERF=$VAE/sw/environ/dTalk.user
    if [ "$(id -u)" = "0" -a -f "$USERF" ]; then
        cat $USERF | (
            read user group
            if [ -n "$user" ] && lsuser "$user" > /dev/null 2>&1 ; then
                su $user "-c cleanup"
            else
                cleanup
            fi
        )
    else
        cleanup
    fi

else

    # partial clean up - just destroy the io subsystem

    which_io_subsystem
    rc=$?
    if [ $rc -eq 1 ]; then
        dirtalk_run devices/dtqa_bin/dtdd_destroy_io_subsystem
    fi
fi

zap DToutlog 2 5
# use ps etc to kill DToutdsp as it is a script
ps -ef -opid,args | grep "/tools/DToutdsp" | grep aixterm | while read pid args; do
     kill -9 $pid
     done

# Remove DirectTalk SNMP daemon
zap dtsnmpd 2 20
# Remove DirectTalk alarm daemon
zap dtalarmd 2 20

exit 0
