#!/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.
#
##########################################################################
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

#######################################################################
#
#  Starts a Palladium supervisor and performs additional initialization
#  needed if this is a brand new supervisor.
#
# Usage: pdstartsuv [-F]
#
#                  -F - Force creation of supervisor dir. HP,JJB,29Aug95.
#                       Note: SAM does not want prompts upon start-up.
#
#######################################################################
if [ $# -ne 1 ]; then
   if [ $# -ne 2 ]; then
      pddmsg 584 "pdstartsuv"
      exit 1
   fi
fi

#----------------------------------------------------------------------
# Parse out the 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 "pdstartsuv"
         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 "supervisor[$SPC$TAB]\{1,\}$SERVER[$SPC]\{0,\}$"
    do
        print "$bn: Shutting down existing supervisor $SERVER."
        ps -ef | grep -v grep | \
           grep "supervisor[$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 supervisor (never started before)
#----------------------------------------------------------------------
if [ "$PDBASE" = "" ] ; then
    pddmsg 529
    exit 1
fi

# Set name of supervisor's directory
SUV_DIR=$PDBASE/$SERVER

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

# Check if that directory already exists
if [ -d "$SUV_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 this is a new server, anticipate pdcrdflt will be run and that
# # person starting server must be dce_logged in.
# # See if user is logged into DCE.  klist returns 0 if logged on.
# #----------------------------------------------------------------------
# if [ $NEW_SERVER -eq 1 ]; then
#    /opt/dce/bin/klist >/dev/null 2>&1
#    KL_RC=$?
#    if [ $KL_RC -ne 0 ]; then
#       if [ $PROMPT_ON_CREATE -eq 1 ] ; then
#          pddmsg 598
#          pddmsg 599
#          read ANS
#          if [ "$ANS" != "Y" ] ; then
#             if [ "$ANS" != "y" ] ; then
#                exit 1
#             fi
#          fi
#       else
#          pddmsg 290
#          exit 1
#       fi
#    fi
# fi

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

if [[ $test_env -eq 0 ]]
then
   superdir="$PDBASE/$SERVER"

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

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

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

else
   supervisor $SERVER
fi

SUV_RC=$?

#  See if failed to start supervisor daemon
if [ $SUV_RC -ne 0 ]; then
   pddmsg 403 $SERVER $SUV_RC
   exit $SUV_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
#    if [ $NEW_SERVER -eq 1 ]; then
#       pddmsg 561
#    fi
   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
   if [ $NEW_SERVER -eq 1 ]; then
      pddmsg 561
   fi
   exit $CLI_RC
fi

#-------------------------------------------------------------------
# Wait until the supervisor is *really* listening
#-------------------------------------------------------------------
integer rem num
((num=1))
until pdls -c server $SERVER >/dev/null 2>&1; do
   sleep 10
   #  Ensure that the supervisor process is still active
   CHK=`ps -ef|grep "supervisor $SERVER"|grep -v grep`
   if [ "$CHK" = "" ]; then
      pddmsg 404 $SERVER
      if [ $NEW_SERVER -eq 1 ]; then
         pddmsg 561
      fi
      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

# #----------------------------------------------------------------------
# # If we just started a new supervisor, create the default objects
# #----------------------------------------------------------------------
# if [ $NEW_SERVER -eq 1 ]; then
#    pddmsg 406
#    #-------------------------------------------------------------------
#    # Create default objects
#    #-------------------------------------------------------------------
#    pdcrdflt $SERVER
# fi

pddmsg 405 $SERVER

exit 0










