#!/usr/bin/sh
##########################################################################
#
#######################################################################
#
# Starts a Palladium client daemon
#
# Usage: pdstartclient 
#
#
#######################################################################
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

# See if pdstartclient was passed the '-q' option
# If so, then report status.
if [ "$1" = "-q" ] 
then
   CHK=` ps -ef | grep -v grep | grep "pdclientd$"`
   if [ "$CHK" = "" ]
   then
     #echo pdclientd is not running
     exit 1
   else
     #HEAD=`ps -ef | head -1`
     # echo $HEAD | \
  # awk '{ printf("%s\t\t%s\t%s\t%s\t%s\t%s\t\t%s\t%s\n",$8,$1,$2,$3,$4,$5,$6,$7) }'
     # echo $CHK | \
  # awk '{ printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",$8,$1,$2,$3,$4,$5,$6,$7) }'
     #echo The HPDPS daemon is already running
     pddmsg 315
     exit 0
   fi
fi


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

#----------------------------------------------------------------------
# Parse command line options
#----------------------------------------------------------------------
CLIENT=$1

#----------------------------------------------------------------------
# Check if this daemon is already active
#----------------------------------------------------------------------
if [ ${IUT_REMOVE_SERVERS} -a $(uname) = HP-UX ]; then	#HP, amcgowen, 29Nov95
    TAB="\011";SPC=" " 
    while ps -ef | grep -v grep | grep "pdclientd$"; do 
        print "$bn: Shutting down existing pdclientd"
        ps -ef | grep -v grep | grep "pdclientd$" | \
           awk '{ print $2 }' | xargs -n1 kill -15
        sleep 10
    done
fi

#----------------------------------------------------------------------
# Set the limits of system resource the client daemon uses.
#----------------------------------------------------------------------
ulimit -d 50000      # data space in kbytes

if [[ $test_env -eq 0 ]]
then
   clientdir="$PDBASE/pdclientd"

   if [[ "${PD_SOCKET}" != "" ]]
   then
      clientdir="${clientdir}.${PD_SOCKET}"
   fi

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

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

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

else
   pdclientd
fi

SPL_RC=$?

#  See if failed to start client daemon
if [ $SPL_RC -ne 0 ]; then
   print "5010-403 Cannot start the client daemon. Return Code=$SPL_RC.  Check the error log for messages related to this failure."
   exit $SPL_RC
fi

pddmsg 396
exit 0
