#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# bin/$RCSfile$   $Revision$
# stop enstore processes on the current node

# Make sure we are running as enstore on the production or test systems.
node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
ensudo=""
case $node in
  cdfen*|stken*|d0en*|rip[0-9]*)
     ensudo="sudo"
     if [ `whoami` = "root" ]; then
	# if running as root, rerun as enstore
	exec su enstore -c "/bin/sh $0 $*"
     elif [ `whoami` = "enstore" ]; then
	# running as enstore
	: # Do nothing
     else
	# not root or enstore
	echo "You should run this as user enstore" >&2
	exit 1
     fi
     ;;
esac

PID_DIR=`$ENSTORE_DIR/bin/en_get_pid_dir`

# NOTE: some options (w/ or w/o args) are currently provided just for restart
# (compatible with start)
opts_w_args='xterm|just'
USAGE="`basename $0`: [--{$opts_w_args} <arg>]"
while opt=`expr "${1-}" : '--\(.*\)'`;do
    shift
    eval "case \$opt in
    \\?) echo \"$USAGE\"; exit 0;;
    $opts_w_args)
        if [ $# = 0 ];then echo option $opt requires argument; exit 1; fi
        eval opt_\$opt=\"'\$1'\";shift ;;
    *)  echo \"invalid option: \$opt\"; exit 1;;
    esac"
done

# this works for the case where opt_just is null
opt_just_ere=`echo ${opt_just-} | sed -e 's/ /|/g' -e 's/,/|/g'`
if [ "$opt_just_ere" = "" ];then opt_just_ere=".*";fi

local_host=`hostname`
if expr "$local_host" : '.*\.' >/dev/null; then 
    local_host=`nslookup $local_host | sed -e '/Name.*/ !d' -e 's/Name: *//'`
fi

timeout=1
tries=10

echo "`date` Calling configuration server to get list of servers to stop"
nodeplus_servers=`conf.sh $local_host 3 2`
# check if Configuration Server is up
if [ $? -ne 0 ] ; then
    config_running=0
    echo "`date` ...configuration server does not seem to be running"
else
    config_running=1
    echo "`date` ...configuration server returned"
fi
echo "`date` ...configuration server returned"

# now just isolate the servers for this node
node_servers=`echo "$nodeplus_servers" | grep :$local_host: | sed -e "s/:$local_host//"`

if [ ! "$node_servers" ];then
    #echo try alternate host specification
    if  hh=`expr "$local_host" : '\([^.]*\)\.'`;then
        :
    else
        hh=`nslookup $local_host | sed -e '/Name.*/ !d' -e 's/Name: *//'`
    fi
    echo "`date` Calling configuration server to get list of servers to stop (alternate host specification)"
    nodeplus_servers=`conf.sh $hh 3 2`
    # check if Configuration Server is up
    if [ $? -ne 0 ] ; then
       config_running=0
       echo "`date` ...configuration server does not seem to be running"
    else
       config_running=1
       echo "`date` ...configuration server returned"
    fi

    node_servers=`echo "$nodeplus_servers" | grep :$hh: | sed -e "s/:$hh//"`
fi


turnoff() { for pid in $1; do
		rm -f $PID_DIR/mtpid; echo "-1">$PID_DIR/mtpid
                if [ "$pid" = "$1" ] ; then echo "No $2 running"; $ensudo pidkill $PID_DIR/mtpid $2; break; fi
		#note the search and kill feature of pidkill only works for single instance servers (like file_clerk, not movers) because name won't match
                $ensudo pidkill $pid $2
            done; }

turnoff_s() { for pid in $1; do
		rm -f $PID_DIR/mtpid; echo "-1">$PID_DIR/mtpid
                if [ "$pid" = "$1" ] ; then echo "No $2 running"; $ensudo pidkill_s $PID_DIR/mtpid $2; break; fi
		#note the search and kill feature of pidkill only works for single instance servers (like file_clerk, not movers) because name won't match
                $ensudo pidkill_s $pid $2
            done; }

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep inq`" ];then
    for serv in $PID_DIR/`uname -n`-inquisitor_pid; do
	if [ `echo "$serv" | grep -c "\-\*\-"` -eq 1 ] ; then continue; fi
        do_server=`echo $serv | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi
        if [ $config_running -eq 1 ] ; then
	  serv1=`echo $serv | cut -f2 -d-`
  	  udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep "$serv1" |sed -e 's/.*://'` $timeout --work quit >/dev/null
        fi

        # note: turnoff function assumes filename globbing, so we must add an *
        turnoff_s "$serv*"   "inquisitor"
    done

#    if [ $config_running -eq 1 ] ; then
#      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep inq |sed -e 's/.*://'` $timeout --work quit >/dev/null
#    fi
#    turnoff "$PID_DIR/`uname -n`-inquisitor*pids"      "inquisitor"
fi

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep event`" ];then
    turnoff "$PID_DIR/`uname -n`-event_relay*pids"      "event_relay"
fi

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep fil`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep fil |sed -e 's/.*://'` $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-file_clerk*pids"      "file_clerk"
fi

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep vol`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep vol |sed -e 's/.*://'` $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-volume_clerk*pids"    "volume_clerk"
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep mov`" ];then
    for serv in $PID_DIR/`uname -n`-*-mover_pid; do
	if [ `echo "$serv" | grep -c "\-\*\-"` -eq 1 ] ; then continue; fi
        do_server=`echo $serv | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi
        #echo "serv is <$serv>  opt_just_ere is <$opt_just_ere> do_server is <$do_server>"
        if [ $config_running -eq 1 ] ; then
	  serv1=`echo $serv | cut -f2 -d-`
  	  udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep "$serv1" |sed -e 's/.*://'` $timeout --work quit >/dev/null
        fi

        # note: turnoff function assumes filename globbing, so we must add an *
        turnoff_s "$serv*"           "movers"
    done
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep med`" ];then
    for serv in $PID_DIR/`uname -n`-*-media_changer_pid; do
	if [ `echo "$serv" | grep -c "\-\*\-"` -eq 1 ] ; then continue; fi
        do_server=`echo $serv | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi
        if [ $config_running -eq 1 ] ; then
	  serv1=`echo $serv | cut -f2 -d-`
  	  udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep "$serv1" |sed -e 's/.*://'` $timeout --work quit >/dev/null
        fi

        # note: turnoff function assumes filename globbing, so we must add an *
        turnoff "$serv*"   "media_changers"
    done
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep lib`" ];then
    for serv in $PID_DIR/`uname -n`-*-library_manager_pid; do
	if [ `echo "$serv" | grep -c "\-\*\-"` -eq 1 ] ; then continue; fi
        do_server=`echo $serv | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi
        if [ $config_running -eq 1 ] ; then
	  serv1=`echo $serv | cut -f2 -d-`
  	  udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep "$serv1" |sed -e 's/.*://'` $timeout --work quit >/dev/null
	fi

        # note: turnoff function assumes filename globbing, so we must add an *
        turnoff_s "$serv*" "library_managers"
    done
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep conf`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host $ENSTORE_CONFIG_PORT $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-configuration*pids"   "configuration_server"
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep alarm`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep alarm |sed -e 's/.*://'` $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-alarm_server*pids"      "alarm_server"
fi
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep log`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep log |sed -e 's/.*://'` $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-log_server*pids"      "log_server"
fi

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep data`" ];then
    turnoff_s "$PID_DIR/`uname -n`-db_checkpoint_pids*" "db_checkpoint"
    turnoff   "$PID_DIR/`uname -n`-db_deadlock_pids*"   "db_deadlock"
    /bin/rm -f $PID_DIR/db_checkpoint # nothing in this file - delete or else db_checkpoint won't start again
    /bin/rm -f $PID_DIR/db_deadlock   # nothing in this file - delete or else db_deadlock   won't start again
fi

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep rate`" ];then
    if [ $config_running -eq 1 ] ; then
      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep rate |sed -e 's/.*://'` $timeout --work quit >/dev/null
    fi
    turnoff "$PID_DIR/`uname -n`-ratekeeper*pids"      "ratekeeper"
fi

#if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep mon`" ];then
#    if [ $config_running -eq 1 ] ; then
#      udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep mon |sed -e 's/.*://'` $timeout --work quit >/dev/null
#    fi
#    turnoff "$PID_DIR/`uname -n`-monitor*pids"         "monitor"
#fi

#   turnoff "$PID_DIR/`uname -n`-*pids"                "what else"

# let's do a PS just to show user what is still running
EPS

exit 0
