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

# Starts enstore after a machine boots

# setup enstore first
if [ -f /usr/local/etc/setups.sh ]; then
    # this check is required by current rpm build process, do not remove!!!!
    . /usr/local/etc/setups.sh
    setup enstore
fi

# set ENSTORE_HOME properly
if [ "${ENSTORE_HOME:-}" = "" ]; then
	ENSTORE_HOME=/home/enstore
fi

timeout=4
output=$ENSTORE_HOME/`hostname`.startup
touch $output
chown enstore.enstore $output
estat=0
if [ -r /dev/changer ]; then
    chmod a+rw /dev/changer
fi

case "$1" in

  start)
        echo "`date`: Starting enstore" 2>&1 | tee /dev/console >>$output

        n1=`echo $ENSTORE_CONFIG_HOST | sed -e 's/\([^\.]\)\..*/\1/'`
        n2=`uname -n | sed -e 's/\([^\.]\)\..*/\1/'`
	runs_on=0
	echo "`date`: Configuring service IPs" 2>&1 | tee /dev/console >>$output
	$ENSTORE_DIR/tools/service_ips 2>&1 | tee /dev/console >>$output

	validate_host.py $ENSTORE_CONFIG_HOST
	if [ $? -eq 1 ]; then
		runs_on=1
		echo "config server runs on this host"
	else
	        if [ "$n1" = "$n2" ]; then
			runs_on=1
		else
			runs_on=0
		fi
	fi


	# if running not on config node
        if [ $runs_on -eq 0 ]; then
        #if [ "$n1" == "$n2" ]; then
            for server in config log alarm; do
	      echo "`date`: checking if $server is alive"  2>&1 | tee /dev/console >>$output
              enstore $server --alive --timeout=$timeout --retries=2 > /dev/null 2>&1
              estat=$?
	      echo "`date`:  ... status returned=$estat" 2>&1 | tee /dev/console >>$output
              if [ $estat -ne 0 ] ; then
                echo "`date`: $server not responding, can not continue..." 2>&1 | tee /dev/console >>$output
                break
              fi
            done
	else
	    # start config server
	    echo "`date`: su-ing to enstore and doing an enstore start --just config" 2>&1 | tee /dev/console >>$output
            su enstore -c ". /usr/local/etc/setups.sh ; setup enstore; enstore start --just config"  
            estat=$?
            if [ $estat -ne 0 ] ; then
                echo "`date`: can not start config server ... can not continue..." 2>&1 | tee /dev/console >>$output
                break
            fi
        fi

	# start database servers if it is necessary

	$ENSTORE_DIR/bin/enstore_database_server.py start

        if [ $estat -eq 0 ]; then
          # we should be enstore when we start this up (at least not root) if we are a server
          if [ "`whoami`" = "root" ]; then
	    echo "`date`: su-ing to enstore and doing an enstore Estart $n2" 2>&1 | tee /dev/console >>$output
            # su enstore -c ". /usr/local/etc/setups.sh ; setup enstore; enstore Estart $n2"  
            su enstore -c ". /usr/local/etc/setups.sh ; setup enstore; enstore start"  
          else
            echo "`date`: doing enstore Estart $n2" 2>&1 | tee /dev/console >>$output
            # enstore Estart $n2 
            enstore start
          fi
       
        fi
        ;;

  stop)
        echo Stopping enstore 2>&1 | tee /dev/console >>$output

	# check if configure server is running
	if [ "`enstore config --alive --timeout=30`" == "BAD STATUS ('TIMEDOUT', 'configuration_server')" ]
	then
		echo "Can not talk to config server ..."
		echo "Stopping everything by default ..."
		# Just call "enstore stop" to stop everything
		enstore stop
		exit 0
	fi
		
	enstore stop

	# should we shut down database servers?

	$ENSTORE_DIR/bin/enstore_database_server.py stop

        ;;

  install)
        x=/etc/rc.d/init.d/enstore-boot
        rm -f $x; cp $ENSTORE_DIR/bin/enstore-boot $x
        cd /etc/rc.d/rc3.d
        x=S99yyyenstore-boot
        rm -f $x; ln -s ../init.d/enstore-boot $x
        cd /etc/rc.d/rc6.d
        x=K01aaaenstore-boot
        rm -f $x; ln -s ../init.d/enstore-boot $x
        ;;
  *)
        echo "Usage: /etc/rc.d/init.d/enstore-boot {start|stop}"
        estat=1
        ;;
esac

exit $estat
