#! /bin/sh
#
# ctrluniboot -- Script used to control aspects of the UniBoot facilities
#
# @(#)ctrluniboot	1.6	LPS_UNX_COM	6/16/95
#
# Copyright 1995   Digital Equipment Corporation, Westford, MA
#
# DESCRIPTION:
#
# This script is used to perform various management tasks surrounding
# the UniBoot facilities.
#
# Usage:  ctrluniboot cmd
#
# where "cmd" is one of the valid control commands:
#
#    start   - Startup the lpsbootd daemon
#    stop    - Shutdown the lpsbootd daemon
#    restart - Stop the daemon, then start it
#    showlog - Display the lpsbootd log file using PAGER
#    change  - Change the booting options for the lpsbootd daemon
#
# This script MUST be executed from the "lpssetup" shell script to ensure
# proper privileges are available, key environment variables are set, and
# the current working directory is properly established.
#
# The current working directory is expected to be LPSROOT directory.
#
# AUTHOR:   JK Martin, Underscore, Inc.
#
###

# Set all script-specific initial variables

SCRIPTNAME=`basename $0`
CMD="$1"

: ${PAGER:=more}
: ${TMPDIR:=/tmp}

DAEMONNAME=lpsbootd

MSGPREFIX="${SCRIPTNAME}:"

# Attempt to validate our environment

if [ -z "$FUNCS" -o -z "$LPSODBLIST" -o -z "$LPSODB" ]
then
    echo
    echo "$MSGPREFIX this script must be run by the lpssetup script!"
    echo
    pause
    exit 1
fi


# Initialize our internal data

. $FUNCS
getobject false ENV


# Internal functions

daemon_is_running ()
{
    PID="-1"			# "PID" is usable by the caller
    pidfile="$ENV_LPSROOT/${DAEMONNAME}.pid"
    rv=1

    if [ -f $pidfile ]
    then
	PID="`cat $pidfile`"

	if kill -0 $PID	> /dev/null 2>&1	# Ping the little devil
	then
	    rv=0				# Daemon found to be running
	fi
    fi

    return $rv
}

start_daemon ()
{
    program="$ENV_LPSBIN/$DAEMONNAME"

    if [ -x $program ]
    then
	if $program
	then
	    echo "Booting daemon launched: $program"
	    retval=0
	else
	    echo
	    echo "Failure to launch booting daemon: $program"
	    retval=1
	fi
    else
	echo "$MSGPREFIX daemon program not found: $program"
	retval=1
    fi

    return $retval
}

stop_daemon ()
{
    retval=0

    if daemon_is_running
    then
	if kill -2 $PID
	then
	    echo "Booting daemon stopped (pid = $PID)"
	else
	    echo "Failure to stop booting daemon (pid = $PID)"
	    retval=1
	fi
    else
	echo "Booting daemon not running (or pid file removed)"
	retval=0
    fi

    return $retval
}

update_env ()
{
    ODBDEF="$TMPDIR/odbdef.$$"
    OLDLPSODB="$TMPDIR/oldodb.$$"

    expobjvars ENV

    if mkodbdef ENV Alist.ENV $ODBDEF
    then
	if updateLPSODB $LPSODB replace ENV LPSENV LPSENV $ODBDEF $OLDLPSODB
	then
	    rm -f $ODBDEF $OLDLPSODB    # No longer need these files
	    
	    return 0	# Success
	fi
    fi

    return 1		# Error
}

# Perform the requested command

exitval=0

echo

case "$CMD" in

    start) if start_daemon
	   then
	       :
	   else
	       exitval=1
	   fi
	   ;;

    stop ) if stop_daemon
	   then
	       :
	   else
	       exitval=1
	   fi
	   ;;

    restart) if stop_daemon && start_daemon
	     then
	     :
	     else
	         exitval=1
	     fi
	     ;;

    showlog ) logfile="$ENV_LPSLOG/${DAEMONNAME}.log"
	      if [ -f $logfile ]
	      then
	          $PAGER $logfile
	      else
	          echo "Log file not found for booting daemon: $logfile"
	      fi
	      ;;

    change ) # Must stop the daemon (if running) in order to perform
	     # the change procedure.

	     echo
	     echo "Checking to see if the booting daemon is running..."
	     echo

	     if daemon_is_running
	     then
	         echo "The booting daemon is currently running."
		 echo
		 echo "To properly determine the available services,"
		 echo "the booting daemon must first be stopped."
		 echo
		 pause "Press RETURN to stop the booting daemon..."
		 echo
		 stop_daemon
	     else
	         echo "The booting daemon is not currently running."
	     fi

	     echo
	     pause

	     outfile=$TMPDIR/ctrlub.$$

	     success=false

	     if setBOOTOPTIONS ENV ANY ANY ANY check $outfile
	     then
	         bootoptions=`cat $outfile`
		 if [ "$bootoptions" = "$ENV_BOOTOPTIONS" ]   # No change?
		 then
		     success=true   # No different than the current setting
		 else
		     # Must update the LPSODB with this new value

		     ENV_BOOTOPTIONS=$bootoptions

		     if update_env
		     then
		         success=true
		     fi
		 fi

		 rm -f $outfile
	     fi

	     if $success
	     then
	         :
	     else
	         echo
	         echo "Booting options were not changed due to errors."
		 echo
	         exitval=1
	     fi

	     # Restart the daemon, even if the new option is "none",
	     # since booting on port 170 still must be performed for
	     # compatible PrintServer printers.

	     echo
	     start_daemon
	     ;;

    *) if [ -z "$CMD" ]
       then
           CMD="<null>"
       fi
       echo "$MSGPREFIX invalid command line paramter: $CMD"
       echo
       echo "usage: $SCRIPTNAME <command>"
       exitval=1
esac

if [ $exitval -ne 0 ]
then
    echo "$MSGPREFIX terminating unsuccessfully."
fi

exit $exitval
