#!/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.
#
##########################################################################
#######################################################################
#
#  Create a minimum Palladium configuration to test print.
#
#######################################################################
ME=$(basename $0)

if [[ "$PDBASE" = "" && -r /etc/rc.config.d/pd ]]
then
   . /etc/rc.config.d/pd
   export PATH=$PATH:/opt/pd/bin:/opt/pd/lbin
   export NLSPATH=/opt/pd/lib/nls/msg/%L/%N
   export PDBASE=/var/opt/pd

   # Default the HPDPS locale to C.

   if [[ -z "$LC_MESSAGES" && -z "$LC_ALL" && -z "$LANG" ]]
   then
      export LANG="C"
   fi
fi

usage()
{
   pddmsg 461 $ME $ME $ME $ME $ME
   exit 1
}

function errp
{
   if [ $1 -ne 0 ]
   then
      pddmsg $*
   fi
   pddmsg 463
   read QA
}

#----------------------------------------------------------------------
# Parse out the command line options
#----------------------------------------------------------------------
AUTO_START=0
set -- `getopt sh $*`
if [ $? != 0 ]
then
   usage
fi
while [ $1 != -- ]
do
        case $1 in
                -s)     AUTO_START=1
                        shift
                        ;;
                -h)     usage
                        ;;
                -\?)     usage
                        ;;
        esac
done

shift  # shift past "--" from getopt

if [ "$PDBASE" = "" ] ; then
   pddmsg 529
   exit 1
fi

#----------------------------------------------------------------------
# Set up the output shell script
#----------------------------------------------------------------------
OUT_FILE=/tmp/${ME}.sh
echo "#!/usr/bin/sh" >$OUT_FILE


#----------------------------------------------------------------------
# Get the spooler and supervisor names
#----------------------------------------------------------------------

SPLR_NAME=$1
SPVR_NAME=$2

if [ "$SPLR_NAME" = "" ]
then
   pddmsg 464
   read SPLR_NAME
fi

if [ "$SPVR_NAME" = "" ]
then
   pddmsg 465
   read SPVR_NAME
fi


#----------------------------------------------------------------------
# If needed, start the client daemon
#----------------------------------------------------------------------
CLID=`ps -ef|grep pdclientd|grep -v grep`
if [ "$CLID" = "" ]
then
   pdclientd
   if [ $? -ne 0 ]
   then
      errp 173
   fi
fi

#----------------------------------------------------------------------
# If requested, start the spooler and supervisor
#----------------------------------------------------------------------
if [ "$AUTO_START" = 1 ]
then
   SPLPDB=${PDBASE}/${SPLR_NAME}
   SUVPDB=${PDBASE}/${SPVR_NAME}
   pddmsg 466 $SPLPDB $SUVPDB
   read QA

   pddmsg 467 $SPLR_NAME
   startspl $SPLR_NAME
   if [ $? -ne 0 ]
   then
      # Assume spooler has put out meaningful message(s)
      exit 1
   fi

   pddmsg 469 $SPVR_NAME
   startsuv $SPVR_NAME
   if [ $? -ne 0 ]
   then
      # Assume supervisor has put out meaningful message(s)
      exit 1
   fi
   # Ensure that the spooler is fully operational
   until pdls -c server $SPVR_NAME >/dev/null 2>&1
   do
      sleep 10

      #  Ensure that the spooler process is still active
      CHK=`ps -ef|grep "supervisor $SPVR_NAME"|grep -v grep`
      if [ "$CHK" = "" ]
      then
         # Assume supervisor has put out meaningful message(s)
         exit 1
      fi
   done
fi

#----------------------------------------------------------------------
# Create the spooler objects
#----------------------------------------------------------------------
LP_NAME=${SPLR_NAME}-lp
Q_NAME=${SPLR_NAME}-q

pddmsg 470 $LP_NAME
CMD="pdcreate -c printer -x\"-associated-queue=$Q_NAME\" $SPLR_NAME:$LP_NAME"
echo $CMD >>$OUT_FILE
pdcreate -c printer -x"-associated-queue=$Q_NAME" $SPLR_NAME:$LP_NAME
if [ $? -ne 0 ]
then
   errp 0
fi

pddmsg 471 $Q_NAME
CMD="pdcreate -c queue $SPLR_NAME:$Q_NAME"
echo $CMD >>$OUT_FILE
pdcreate -c queue $SPLR_NAME:$Q_NAME
if [ $? -ne 0 ]
then
   errp 0
fi

pddmsg 472 $LP_NAME
CMD="pdenable -c printer $LP_NAME"
echo $CMD >>$OUT_FILE
pdenable -c printer $LP_NAME
if [ $? -ne 0 ]
then
   errp 0
fi

#----------------------------------------------------------------------
# Create the supervisor objects
#----------------------------------------------------------------------
PP_NAME=${SPVR_NAME}-pp
TEMP_FILE=/tmp/${PP_NAME}

pddmsg 473
read ANS

case $ANS in
1) touch $TEMP_FILE
   PP_ATTRS="-printer-model=4019 -device-name=$TEMP_FILE -associated-queue=$Q_NAME"
   ;;
2) pddmsg 474
   read ATTR_FILE
   ;;
*) errp 475
   touch $TEMP_FILE
   PP_ATTRS="-printer-model=4019 -device-name=$TEMP_FILE -associated-queue=$Q_NAME"
   ANS=1
   ;;
esac

pddmsg 476 $PP_NAME
if [[ $ANS = 1 ]]
then
   CMD="pdcreate -c printer -x\"$PP_ATTRS\" $SPVR_NAME:$PP_NAME"
   echo $CMD >>$OUT_FILE
   pdcreate -c printer -x"$PP_ATTRS" $SPVR_NAME:$PP_NAME
else
   CMD="pdcreate -c printer -X $ATTR_FILE -x\"-associated-queue=$Q_NAME\" $SPVR_NAME:$PP_NAME"
   echo $CMD >>$OUT_FILE
   pdcreate -c printer -X $ATTR_FILE -x"-associated-queue=$Q_NAME" $SPVR_NAME:$PP_NAME
fi
if [ $? -ne 0 ]
then
   errp 0
fi

pddmsg 477 $PP_NAME
CMD="pdenable -c printer $PP_NAME"
echo $CMD >>$OUT_FILE
pdenable -c printer $PP_NAME
if [ $? -ne 0 ]
then
   errp 0
fi

#----------------------------------------------------------------------
# Finish up and return
#----------------------------------------------------------------------
pddmsg 462 $LP_NAME $Q_NAME $PP_NAME $LP_NAME $OUT_FILE
exit 0
