#!/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$
# start enstore processes on the current node

# the default coresize limit is 0.  This is set to unlimited in /etc/profile, 
# but /etc/profile is only executed if you login.  Since we start most of
# the enstore movers and servers from rsh or ssh without logging in, the coresize
# remains set at the default 0 and hence, we do not get get core files!
# Just do the same thing that /etc/profile does in this script.
ulimit -c unlimited


# 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

# function to redirect input/output when there is not a controlling terminal
output() { tty -s
           if [ $? -ne 0 ] ; then
             # no controlling tty
             if [ -z "${1-}" ] ; then
               # no file to direct output to
	       if test -w /dev/console; then
                 echo " < /dev/null > /dev/console 2>&1"
               else
                 echo "< /dev/null > /dev/null 2>&1"
               fi
             else
               # use file in $1 to direct output to
	       if test -w /dev/console; then
                 echo " < /dev/null |tee /dev/console > $1.out 2>&1"
               else
                 echo " < /dev/null > $1.out 2>&1"
	       fi
             fi
	   else
             # have a controlling tty
             if [ -z "${1-}" ] ; then
	       # no file to direct output to
	       echo " < /dev/null > /dev/tty  2>&1"
             else
               echo " < /dev/null |tee /dev/tty > $1.out 2>&1"
             fi
           fi
         }

# function to check if server is up, exits if it is not
check_server()
{
    if [ `echo $1 | grep -c config` -eq 0 ]; then
      xnode=`echo "$nodeplus_servers" | grep $1 | cut -d: -f2`
      xport=`echo "$nodeplus_servers" | grep $1 | cut -d: -f3`
    else
      xnode=$ENSTORE_CONFIG_HOST
      xport=$ENSTORE_CONFIG_PORT
    fi
    if [ -n "${2-}" ] ; then
       echo "`date` Checking $1 on $xnode $xport $local_host"
    fi  
    #echo udp_sendWaitReply-withTimeout.sh $xnode $xport $timeout --tries $tries
    alv=`udp_sendWaitReply-withTimeout.sh $xnode $xport $timeout --tries $tries`
    if [ $? -ne 0 ] ; then
        echo "`date`   ...$1 on $xnode $xport $local_host NOT REACHABLE..."
        exit 1
    elif [ -n "${2-}" -a `echo $local_host | grep -ic $xnode` -ne 0 ]; then 
       # put the pid (maybe more than once, doesn't matter) into pid file
       echo $alv | sed -e 's/.*pid.: //' -e 's/,.*//'  >> $2
    fi
    if [ -n "${2-}" ] ; then
       echo "`date` $1 = $alv"
    fi  
}

check_event_relay()
{
    if [ `echo $1 | grep -c config` -eq 0 ]; then
      xnode=`echo "$nodeplus_servers" | grep $1 | cut -d: -f2`
      xport=`echo "$nodeplus_servers" | grep $1 | cut -d: -f3`
    else
      xnode=$ENSTORE_CONFIG_HOST
      xport=$ENSTORE_CONFIG_PORT
    fi
    if [ -n "${2-}" ] ; then
       echo "`date` Checking $1 on $xnode $xport $local_host"
    fi  
    #echo udp_sendWaitReply-withTimeout.sh $xnode $xport $timeout --tries $tries
    alv=`enstore EPS ${xnode}|grep event_relay.py`
    if [ "${alv}" = "" ]; then
        echo "`date`   ...$1 on $xnode $xport $local_host NOT REACHABLE..."
        exit 1
    elif [ -n "${2-}" -a `echo $local_host | grep -ic $xnode` -ne 0 ]; then 
       # put the pid (maybe more than once, doesn't matter) into pid file
       echo $alv | cut -f2 -d " "  >> $2
    fi
    if [ -n "${2-}" ] ; then
       echo "`date` $1 = $alv"
    fi  
}


start_db_daemons()
{
    out=$PID_DIR/db_checkpoint
    if [ ! -r $out ] ; then
        if [ -z "${enstore_db-}" ]; then
          enstore_db=`udp_sendWaitReply-withTimeout.sh $ENSTORE_CONFIG_HOST $ENSTORE_CONFIG_PORT 4 --work lookup --additional_ticket '"lookup":"database"' | sed -e "s/.*'db_dir'/'db_dir'/" |  cut -d: -f2 | sed -e "s/.*'\(.*\)'.*/\1/" `
	fi
        echo "`date` Starting database checkpoint daemon  on `hostname`"
	cmd="db_checkpoint -h $enstore_db  -p 5 `output $out`"
	eval "$cmd &"
        echo $! >> $PID_DIR/`uname -n`-db_checkpoint_pids	
	echo "`EPS returnpid db_checkpoint`" >> $PID_DIR/`uname -n`-db_checkpoint_pids
	date >> $out  # db_checkpoint doesn't usually write to this file. send something so we don't start twice on same node
    fi

    out=$PID_DIR/db_deadlock
    if [ ! -r $out ] ; then
        if [ -z "${enstore_db-}" ]; then
          enstore_db=`udp_sendWaitReply-withTimeout.sh $ENSTORE_CONFIG_HOST $ENSTORE_CONFIG_PORT 4 --work lookup --additional_ticket '"lookup":"database"' | cut -d: -f2 | cut -d, -f1 | cut -d\' -f2`
	fi
        echo "`date` Starting database deadlock daemon  on `hostname`"
        cmd="db_deadlock -h $enstore_db  -t 1 `output $out`"
        eval "$cmd &"
        echo $! >> $PID_DIR/`uname -n`-db_deadlock_pids	
        echo "`EPS returnpid db_deadlock`"  >> $PID_DIR/`uname -n`-db_deadlock_pids	
	date >> $out  # db_deadlock doesn't usually write to this file. send something so we don't start twice on same node
     fi
}

PID_DIR=`$ENSTORE_DIR/bin/en_get_pid_dir`

opts_wo_args='ping|nocheck|async'
opts_w_args='xterm|just'
USAGE="`basename $0`: [--{$opts_w_args} <arg>] [--{$opts_wo_args}]"
while opt=`expr "${1-}" : '--\(.*\)'`;do
    shift
    eval "case \$opt in
    \\?) echo \"$USAGE\"; exit 0;;
    $opts_wo_args)
        eval opt_\$opt=1;;
    $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

place=`pwd`
cd $PID_DIR

# we need both local_host and ENSTORE_CONFIG_HOST to be fully qualified or not qualified at all
# can not be mixed for a meaningful comparison
local_host=`hostname`
enstore_start_config=0     # new default
eful=0 lful=0
if expr "$ENSTORE_CONFIG_HOST" : '.*\.' >/dev/null;then eful=1; fi
if expr "$local_host" : '.*\.' >/dev/null;         then lful=1; fi

if   [ $eful = 1 -a $lful = 0 ];then
    echo "`date` Warning $ENSTORE_CONFIG_HOST $local_host eful=$eful lful=$lful...calling nslookup"
    local_host=`nslookup $local_host | sed -e '/Name.*/ !d' -e 's/Name: *//'`
    echo "`date` ...nslookup returned"
elif [ $eful = 0 -a $lful = 1 ];then
    echo "`date` Warning $ENSTORE_CONFIG_HOST $local_host eful=$eful lful=$lful...calling nslookup"
    ENSTORE_CONFIG_HOST=`nslookup $local_host | sed -e '/Name.*/ !d' -e 's/Name: *//'`
    echo "`date` ...nslookup returned"
fi

if [ $local_host = $ENSTORE_CONFIG_HOST ];then
    enstore_start_config=1
fi

timeout=5
save_timeout=$timeout
tries=5
save_tries=$tries

###################################################################################################################################

# the configuration server needs to be the 1st thing started
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep conf`" != "" \) -a $enstore_start_config -eq 1 ];then
    if [ "${opt_ping-}" != 1 ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $ENSTORE_CONFIG_HOST $ENSTORE_CONFIG_PORT $timeout >/dev/null
        then
        echo "Configuration Server on $ENSTORE_CONFIG_HOST already started"
      else
        echo "`date` Starting Configuration Server on $ENSTORE_CONFIG_HOST, port $ENSTORE_CONFIG_PORT, using file $ENSTORE_CONFIG_FILE"
        out=$PID_DIR/configuration_server
        rm -f $out
        cmd="python $ENSTORE_DIR/src/configuration_server.py --config_file=$ENSTORE_CONFIG_FILE `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|conf' >/dev/null; then
            title="Enstore Config Server"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-configuration_pids
        echo "`EPS returnpid configuration_server`" >> $PID_DIR/`uname -n`-configuration_pids
      fi
    fi

    # Don't check configuration startup in background - need it working before anything else can start
    echo "`date` Waiting for configuration server to start"
    check_server configuration $PID_DIR/`uname -n`-configuration_pids
    echo "`date` ...configuration server returned"

fi

###################################################################################################################################

# new defaults
enstore_libraries=
enstore_media_changers=
enstore_movers=
enstore_start_fileclerk=0
enstore_start_volumeclerk=0
enstore_start_alarm=0
enstore_start_log=0
enstore_start_inquisitor=0
enstore_start_event=0
enstore_start_monitor=0
enstore_start_ratekeeper=0

# get all the servers for this port and other important servers (file_clerk, volume_clerk, ...)
#note that conf.sh will reset the above vars.
echo "`date` Calling configuration server to get list of servers to start"
nodeplus_servers=`conf.sh $local_host 5 10`
# check if Configuration Server is up
if [ $? -ne 0 ] ; then
    echo "   ...Configuration Server not reachable..."
    exit 1
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 start (alternate host specification)"
    nodeplus_servers=`conf.sh $hh 5 10`
    # check if Configuration Server is up
    if [ $? -ne 0 ] ; then
	echo "   ...Configuration Server not reachable..."
	exit 1
    fi
    echo "`date` ...configuration server returned"

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

for server in $node_servers;do
    if t=`expr "$server" : '\(.*\)[: ]'`;then   #strip off port if it exists
        server=$t
    fi
    case $server in
    *library_manager)
	#echo "Will be checking/starting library manager $server on $local_host"
        enstore_libraries="$enstore_libraries $server";;
    *media_changer)
	#echo "Will be checking/starting media changer $server on $local_host"
        enstore_media_changers="$enstore_media_changers $server";;
    *mover)
	#echo "Will be checking/starting mover $server on $local_host"
        enstore_movers="$enstore_movers $server";;
    log_server)
	#echo "Will be checking/starting log server on $local_host"
        enstore_start_log=1;;
    file_clerk)
	#echo "Will be checking/starting file clerk on $local_host"
        enstore_start_fileclerk=1;;
    volume_clerk)
	#echo "Will be checking/starting volume clerk on $local_host"
        enstore_start_volumeclerk=1;;
    inquisitor)
	#echo "Will be checking/starting inquisitor on $local_host"
        enstore_start_inquisitor=1;;
    alarm_server)
	#echo "Will be checking/starting alarm server on $local_host"
        enstore_start_alarm=1;;
    event_relay)
	#echo "Will be checking/starting event relay on $local_host"
        enstore_start_event=1;;
    monitor)
	#echo "Will be checking/starting monitor on $local_host"
        enstore_start_monitor=1;;
    ratekeeper)
	#echo "Will be checking/starting ratekeeper on $local_host"
        enstore_start_ratekeeper=1;;
    esac
done

###################################################################################################################################
# if user doesn't specify the --async option, then check servers as we start them up
if   [ ! "${opt_async-}" ] ; then 
    passes=startem
# if user does specify --async , then start up in 2 passes - 1st pass - just start, 2nd pass - just check
else
    passes="startem checkem"
fi
for pass in $passes; do
###################################################################################################################################

# the event_relay needs to be next
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep event`" != "" \) -a $enstore_start_event -eq 1 ];then
    thisNode=`uname -n`
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ] && enstore EPS $thisNode|grep event_relay.py >/dev/null; then
        echo "Event Relay already started"
      else
        echo "`date` Starting Event Relay"
        out=$PID_DIR/event_relay
        rm -f $out
        cmd="python $ENSTORE_DIR/src/event_relay.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|event' >/dev/null; then
            title="Enstore Event Relay"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-event_relay_pids
        echo "`EPS returnpid event_relay`" >> $PID_DIR/`uname -n`-event_relay_pids
      fi


      if [ ! "${opt_async-}" ];
      then
	check_event_relay event_relay $PID_DIR/`uname -n`-event_relay_pids
      fi
    
    else
      check_event_relay event_relay $PID_DIR/`uname -n`-event_relay_pids &
    fi

fi

###################################################################################################################################

# the logger needs to be next
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep log`" != "" \) -a $enstore_start_log -eq 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep log_server |sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "Log Server already started"
      else
        echo "`date` Starting Log Server"
        out=$PID_DIR/log_server
        rm -f $out
        cmd="python $ENSTORE_DIR/src/log_server.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|log' >/dev/null; then
            title="Enstore Log Server"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-log_server_pids
        echo "`EPS returnpid log_server`" >> $PID_DIR/`uname -n`-log_server_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server log_server $PID_DIR/`uname -n`-log_server_pids; fi
    
    else
      check_server log_server $PID_DIR/`uname -n`-log_server_pids &
    fi

fi

###################################################################################################################################

# the alarm server is next
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep alarm`" != "" \) -a $enstore_start_alarm -eq 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep alarm_server |sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "Alarm Server already started"
      else
        echo "`date` Starting Alarm Server"
        out=$PID_DIR/alarm_server
        rm -f $out
        cmd="python $ENSTORE_DIR/src/alarm_server.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|alarm' >/dev/null; then
            title="Enstore Alarm Server"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-alarm_server_pids
        echo "`EPS returnpid alarm_server`" >> $PID_DIR/`uname -n`-alarm_server_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server alarm_server $PID_DIR/`uname -n`-alarm_server_pids; fi

    else
      check_server alarm_server $PID_DIR/`uname -n`-alarm_server_pids &
    fi

fi
 
###################################################################################################################################

# dealing with db_recover
# just to be paranoid ... if db_recover is running, do not start
# file_clerk nor volume clerk

db_recover=0

case `uname` in
  Linux*)
        x=`ps ax`
        ;;
  OSF1*)
        x=`ps  ax`
        ;;
  SunOS*)
        x=`ps -ef`
        ;;
  IRIX*)
        x=`ps -ef`
        ;;
      *)
        x=`ps -ef`
        ;;
esac

if `echo $x | grep db_recover > /dev/null`
then
	db_recover=1
else
	db_recover=0
fi

# volume clerk
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep vol`" != "" \) -a $enstore_start_volumeclerk -eq 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep volume_clerk|sed -e 's/.*://'` $timeout>/dev/null
        then
        echo "Volume Clerk already started"
      elif [ $db_recover -eq 1 ]; then
	echo "Volume Clerk does not start due to suspicious db_recover"
      else
        echo "`date` Starting Volume Clerk"
        out=$PID_DIR/volume_clerk
        rm -f $out
        cmd="python $ENSTORE_DIR/src/volume_clerk.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|vol' >/dev/null; then
            title="Enstore Volume Clerk"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-volume_clerk_pids
        echo "`EPS returnpid volume_clerk`" >> $PID_DIR/`uname -n`-volume_clerk_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server volume_clerk $PID_DIR/`uname -n`-volume_clerk_pids; fi

    else
      check_server volume_clerk $PID_DIR/`uname -n`-volume_clerk_pids  &
    fi

    start_db_daemons

fi

###################################################################################################################################

# file clerk
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep fil`" != "" \) -a $enstore_start_fileclerk -eq 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem"  ];then

      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep file_clerk|sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "File Clerk already started"
      elif [ $db_recover -eq 1 ]; then
	echo "File Clerk does not start due to suspicious db_recover"
      else
        echo "`date` Starting File Clerk"
        out=$PID_DIR/file_clerk
        rm -f $out
        cmd="python $ENSTORE_DIR/src/file_clerk.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|fil' >/dev/null; then
            title="Enstore File Clerk"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-file_clerk_pids
        echo "`EPS returnpid file_clerk`" >> $PID_DIR/`uname -n`-file_clerk_pids
      fi
     if [ ! "${opt_async-}" ]; then check_server file_clerk $PID_DIR/`uname -n`-file_clerk_pids; fi

    else
     check_server file_clerk $PID_DIR/`uname -n`-file_clerk_pids &
    fi

    start_db_daemons

fi

###################################################################################################################################

# library manager

if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep lib`" != "" ];then
    for i in $enstore_libraries; do
        do_server=`echo $i | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi

        if [ ! "${opt_ping-}" -a $pass = "startem" ];then
            if     [ ! "${opt_nocheck-}" ]\
                && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep $i|sed -e 's/.*://'` $timeout >/dev/null
                then
                echo "Library Manager already started"
            else
                echo "`date` Starting $i Library Manager"
                out=$PID_DIR/library_manager_$i
                rm -f $out
                cmd="python $ENSTORE_DIR/src/library_manager.py $i `output $out`"
                if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|lib' >/dev/null; then
                    title="Enstore Library Manager $i"
                    eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
                else
                    eval "$cmd &"
                fi
                echo $! > $PID_DIR/`uname -n`-$i-library_manager_pid
                echo "`EPS returnpid $i.library_manager`" > $PID_DIR/`uname -n`-$i-library_manager_pid
            fi
  	   if [ ! "${opt_async-}" ]; then check_server $i $PID_DIR/`uname -n`-$i-library_manager_pid; fi

	else
  	   check_server $i $PID_DIR/`uname -n`-$i-library_manager_pid &
        fi

    done
fi

###################################################################################################################################

# media changer
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep med`" != "" ];then
    for i in $enstore_media_changers; do
        do_server=`echo $i | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi

        if [ ! "${opt_ping-}" -a $pass = "startem" ];then
            if     [ ! "${opt_nocheck-}" ]\
                && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep $i|sed -e 's/.*://'` $timeout >/dev/null
                then
                echo "Media Changer already started"
            else
                echo "`date` Starting $i Media Changer"
                out=$PID_DIR/media_changer_$i
                rm -f $out
                cmd="python $ENSTORE_DIR/src/media_changer.py $i `output $out`"
                if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|med' >/dev/null; then
                    title="Enstore Media Changer $i"
                    eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
                else
                    eval "$cmd &"
                fi
                echo $! >> $PID_DIR/`uname -n`-$i-media_changer_pid
                echo "`EPS returnpid $i.media_changer`" >> $PID_DIR/`uname -n`-$i-media_changer_pid
            fi
  	    if [ ! "${opt_async-}" ]; then check_server $i $PID_DIR/`uname -n`-$i-media_changer_pid; fi

        else
  	  check_server $i $PID_DIR/`uname -n`-$i-media_changer_pid &
        fi

    done

fi

###################################################################################################################################

# mover
if [ "${opt_just-}" = "" -o "`echo ${opt_just-}|grep mov`" != "" ];then
    for i in $enstore_movers; do
        do_server=`echo $i | egrep "$opt_just_ere"`
        if [ ! "$do_server" ];then continue; fi

        if [ ! "${opt_ping-}" -a $pass = "startem" ];then
            if     [ ! "${opt_nocheck-}" ]\
                && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep $i|sed -e 's/.*://'` $timeout >/dev/null
                then
                echo "$i Mover already started"
            else
	        # on a reboot, an mt offline command is issued to each drive from rc.local
                # a file /dev/rmt/tps*.offline is created before the offline and rm'd when it completes (successful or not)
                # if the file still exists, there is clearly something wrong with the drive and the mover should not start
                if [ `ls /dev/rmt/tps*.offline 2>/dev/null | wc | awk '{print $1}'` != "0" ]; then 
		  echo "`date`  ERROR: Founding drive in reboot offline condition, sleeping 60"
		  sleep 60
                  if [ `ls /dev/rmt/tps*.offline 2>/dev/null | wc | awk '{print $1}'` != "0" ]; then 
		    echo "`date`  ERROR: Founding drive in reboot offline condition, sleeping 120"
		    sleep 120
                    if [ `ls /dev/rmt/tps*.offline 2>/dev/null | wc | awk '{print $1}'` != "0" ]; then 
  	 	      echo "`date`  ERROR: Founding drive in reboot offline condition, ABORTING"
		      exit 1
		    fi
		  fi
                fi  
                echo "`date` Starting $i Mover"
                out=$PID_DIR/mover_$i
                rm -f $out
                cmd="$ensudo python $ENSTORE_DIR/src/mover.py $i `output $out`"
                if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|mov' >/dev/null; then
                    title="Enstore Mover $i"
                    eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
                else
                    eval "$cmd &"
                fi
                echo $! >> $PID_DIR/`uname -n`-$i-mover_pid
                echo "`EPS returnpid $i.mover`" >> $PID_DIR/`uname -n`-$i-mover_pid
            fi
   	    if [ ! "${opt_async-}" ]; then 
               timeout=10
               tries=12
	       check_server $i $PID_DIR/`uname -n`-$i-mover_pid
               timeout=$save_timeout
               tries=$save_tries
            fi

        else         
#         if the mover has to dismount a tape, it could take some time to respond to us
          timeout=10
          tries=12
 	  check_server $i $PID_DIR/`uname -n`-$i-mover_pid &
          timeout=$save_timeout
          tries=$save_tries
        fi

    done
fi

###################################################################################################################################

# inquisitor
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep inq`" != "" \) -a $enstore_start_inquisitor = 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep inquisitor|sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "Inquisitor already started"
      else
        echo "`date` Starting Inquisitor"
        out=$PID_DIR/inquisitor
        rm -f $out
        cmd="python $ENSTORE_DIR/src/inquisitor.py `output $out`"
	echo $cmd
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|inq' >/dev/null; then
            title="Enstore Inquisitor"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-inquisitor_pids
        echo "`EPS returnpid inquisitor`" >> $PID_DIR/`uname -n`-inquisitor_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server inquisitor $PID_DIR/`uname -n`-inquisitor_pids; fi

    else
      check_server inquisitor $PID_DIR/`uname -n`-inquisitor_pids &
    fi

fi

###################################################################################################################################

# ratekeeper
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep rate`" != "" \) -a $enstore_start_ratekeeper = 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep ratekeeper|sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "Ratekeeper already started"
      else
        echo "`date` Starting Ratekeeper"
        out=$PID_DIR/ratekeeper
        rm -f $out
        cmd="python $ENSTORE_DIR/src/ratekeeper.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|rate' >/dev/null; then
            title="Enstore Ratekeeper"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-ratekeeper_pids
        echo "`EPS returnpid ratekeeper`" >> $PID_DIR/`uname -n`-ratekeeper_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server ratekeeper $PID_DIR/`uname -n`-ratekeeper_pids; fi

    else
      check_server ratekeeper $PID_DIR/`uname -n`-ratekeeper_pids &
    fi

fi

###################################################################################################################################

# monitor server
if [ \( "${opt_just-}" = "" -o "`echo ${opt_just-}|grep mon`" != "" \) -a $enstore_start_monitor = 1 ];then
    if [ "${opt_ping-}" != 1 -a $pass = "startem" ];then
      if   [ ! "${opt_nocheck-}" ]\
        && udp_sendWaitReply-withTimeout.sh $local_host `echo "$node_servers"|grep monitor|sed -e 's/.*://'` $timeout >/dev/null
        then
        echo "Monitor already started"
      else
        echo "`date` Starting Monitor"
        out=$PID_DIR/monitor
        rm -f $out
        cmd="python $ENSTORE_DIR/src/monitor_server.py `output $out`"
        if [ "${opt_xterm-}" ] && echo ${opt_xterm-} | egrep 'all|mon' >/dev/null; then
            title="Enstore Monitor"
            eval "xterm -sl 2500 -T \"$title\" -e $cmd &"
        else
            eval "$cmd &"
        fi
        echo $! >> $PID_DIR/`uname -n`-monitor_pids
        echo "`EPS returnpid monitor`" >> $PID_DIR/`uname -n`-monitor_pids
      fi
      if [ ! "${opt_async-}" ]; then check_server monitor $PID_DIR/`uname -n`-monitor_pids; fi

    else
      check_server monitor $PID_DIR/`uname -n`-monitor_pids &
    fi

fi

###################################################################################################################################
# start the rest up in 2 passes - 1st pass - just start, 2nd pass - just check
done
###################################################################################################################################


###################################################################################################################################

cd $place >/dev/null 2>&1

exit 0
