#!/usr/bin/sh
##########################################################################
# COPYRIGHT:
#    5765-273 (C) COPYRIGHT IBM CORPORATION 1995.
#    All Rights Reserved.
#    Licenced Materials - Property of IBM
#
#    US Government Users Restricted Rights - Use, duplication, or
#    disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#######################################################################
#
# Starts a Palladium spooler
#
# Usage: pdstartspl [-F]
#
#                  -F - Force creation of spooler dir. HP,JJB,29Aug95.
#                       Note: SAM does not want prompts upon start-up.
#
#######################################################################
typeset bn=$(basename $0)
test_env=1

if [[ "$PDBASE" = "" && -r /etc/rc.config.d/pd ]]
then
   . /etc/rc.config.d/pd
   export PATH=$PATH:/opt/pd/bin:/opt/pd/lbin
   export PDBASE=/var/opt/pd
   export PD_ENV
   export PDPRNPATH

   # Default the HPDPS locale to C.

   temp_lang=""

   if [[ -n "$LC_MESSAGES" ]]
   then
      temp_lang="$LC_MESSAGES"

   elif [[ -n "$LC_ALL" ]]
   then
      temp_lang="$LC_ALL"

   elif [[ -n "$LANG" ]]
   then
      temp_lang="$LANG"
   fi

   if [[ -z "$temp_lang" ]]
   then
      temp_lang="C"
      export LANG="C"
   fi

   if [[ "$temp_lang" = "C" ]]
   then
      export NLSPATH=/opt/pd/lib/nls/msg/C/%N
   else
      export NLSPATH=/opt/pd/lib/nls/msg/%L/%N:/opt/pd/lib/nls/msg/C/%N
   fi

   test_env=0
fi

#----------------------------------------------------------------------
# Check if user has root permissions
#----------------------------------------------------------------------
if [ $(whoami) != root ]; then	#HP, amcgowen, 29Nov95
      pddmsg 1001
      exit 1
fi

if [ $# -ne 1 ]; then
   if [ $# -ne 2 ]; then
      pddmsg 584 "pdstartspl"
      exit 1
   fi
fi

#----------------------------------------------------------------------
# Parse command line options
#----------------------------------------------------------------------
PROMPT_ON_CREATE=1
if [ $# -eq 1 ] ; then
   SERVER=$1
else
   if [ "$1" = "-F" ] ; then
      SERVER=$2
      PROMPT_ON_CREATE=0
   else
      if [ "$2" = "-F" ] ; then
         SERVER=$1
         PROMPT_ON_CREATE=0
      else
         pddmsg 584 "pdstartspl"
         exit 1
      fi
   fi
fi

#----------------------------------------------------------------------
# Check if this server is already active
#----------------------------------------------------------------------
if [ ${IUT_REMOVE_SERVERS} -a $(uname) = HP-UX ]; then	#HP, ssabat/jbralley, 13Sep95
    TAB="\011";SPC=" " 
    while ps -ef | grep -v grep | \
          grep "spooler[$SPC$TAB]\{1,\}$SERVER[$SPC]\{0,\}$"
    do
        print "$bn: Shutting down existing spooler $SERVER"
        ps -ef | grep -v grep | \
           grep "spooler[$SPC$TAB]\{1,\}$SERVER[$SPC]\{0,\}$" | \
           awk '{ print $2 }' | xargs -n1 kill -17
        sleep 10
    done
else
    pdls -c server $SERVER >/dev/null 2>&1
    CLI_RC=$?
    if [ $CLI_RC -eq 0 ]; then
        pddmsg 618 $SERVER
        exit 1
    fi
fi
#----------------------------------------------------------------------
# See if this is a brand-new spooler (never started before)
#----------------------------------------------------------------------
if [ "$PDBASE" = "" ] ; then
    pddmsg 529
    exit 1
fi
# Set name of spooler's directory
SPL_DIR=$PDBASE/$SERVER

if [ $PROMPT_ON_CREATE -eq 0 ]; then
   [ ! -d $SPL_DIR ] && mkdir -p $SPL_DIR	# HP,JJB,28Aug95.
fi

# Check if that directory already exists
#
if [ -d "$SPL_DIR" ]; then
     NEW_SERVER=0
else NEW_SERVER=1
   if [ $PROMPT_ON_CREATE -eq 1 ] ; then
      pddmsg 583 $SERVER
      read ANS
      if [ "$ANS" != "Y" ] ; then
         if [ "$ANS" != "y" ] ; then
            exit 1
         fi
      fi
   fi
fi

if [[ -n ${PD_MEMLIMIT} ]]
then
   #----------------------------------------------------------------------
   # Set the limits of system resource the spooler uses.
   #----------------------------------------------------------------------
   ulimit -d ${PD_MEMLIMIT}      # data space in kbytes
fi

if [[ $test_env -eq 0 ]]
then
   #----------------------------------------------------------------------
   # Setup the startup.log file.
   #----------------------------------------------------------------------

   spooldir="$PDBASE/$SERVER"

   mkdir $spooldir > /dev/null 2>&1

   if [[ -f ${spooldir}/startup.log ]]
   then
      mv ${spooldir}/startup.log ${spooldir}/startup.log.old
   fi

   #----------------------------------------------------------------------
   # Start the spooler process
   #----------------------------------------------------------------------
   spooler $SERVER > ${spooldir}/startup.log 2>&1

else
   spooler $SERVER
fi

SPL_RC=$?

#  See if failed to start spooler daemon
if [ $SPL_RC -ne 0 ]; then
   pddmsg 403 $SERVER $SPL_RC
   exit $SPL_RC
fi

sleep 10

#-------------------------------------------------------------------
# Ensure client code installed and client daemon running before
# trying to issue any commands
#-------------------------------------------------------------------
# Check if the client code is installed

whence pdls > /dev/null

if [[ $? -ne 0 ]]; then
   pddmsg 409
   exit 0
fi

# Check if the client daemon is running
pdls -c server $SERVER >/dev/null 2>&1
CLI_RC=$?
if [ $CLI_RC -eq 2 ]; then
   pddmsg 410
   exit $CLI_RC
fi

#-------------------------------------------------------------------
# Wait until the spooler is *really* listening
#-------------------------------------------------------------------
integer rem num
((num=1))
until pdls -c server $SERVER >/dev/null 2>&1
do
   sleep 10

   #  Ensure that the spooler process is still active
   CHK=`ps -ef|grep "spooler $SERVER"|grep -v grep`
   if [ "$CHK" = "" ]; then
      pddmsg 404 $SERVER
      exit 1
   fi

   # Display in-progress msg every other time through loop
   ((rem=num%2))
   if ((rem==0)) ; then
      pddmsg 611 $SERVER
   fi
   ((num+=1))
done

#----------------------------------------
# Server started successfully message
#----------------------------------------
pddmsg 405 $SERVER

exit 0
