#! /usr/bin/ksh
############################################################################
# This startpd script initializes the pd envrionment to execute the pdS
# command test scripts.  The following environment variables
# (shown with sample values) are also required:
#
#      export PD_PATH="/pd_dev/bin"
#      export WORKDIR="$HOME"
#      export QUEUE="gp_q1"
#      export SPOOLER="gp_splr"
#      export SUPERVR="gp_spvr"
#      export LPRINTER="gp_lp"
#      export PPRINTER="gp_pp"
#      export LPDEV="null"
#
#  A related script, cleanpd, removes the objects created by startpd and
# cleans up the DCE Name Space.
#
############################################################################

#  Echo the execution path for user verification.
echo "The execution path is set to" $PD_PATH

if [[ -z $PD_PATH ]]   # path to executables
then
        echo "Environment variable PD_PATH not found!"
        exit 1
fi

############################################################################
# Check to see if we have a dce login
############################################################################

/opt/dce/bin/klist > /dev/null
if  [ $? -ne 0 ]   # if not logged in
then
        echo "STARTPD :klist error "
        echo "*** Not logged into DCE ***"
        exit 1
else
        echo "DCE login verified"
fi

############################################################################
# Kill the client daemon if it is running then restart it using the correct
# executable.
# Kill all pdclientd if PDCLIENTD environment variable is not true
############################################################################

if [[ $PDCLIENTD != true ]]
then
        ps -t ? | awk '/pdclientd/ { system ( "kill -9 " $1) ; print "Removing pdclientd " $1  }'
        ps -t ? | awk '/basicdsd/ { system ( "kill -9 " $1) ; print "Removing basicdsd " $1  }'

        if  [ -f /client.db ]   # 1st check for existence of /client.db file.
        then
                          su pdroot "-c /bin/rm /client.db "
        echo "The file "/client.db" has been removed."
        fi
        if  [ -f /var/opt/pd/basicdsd.db ]   # 1st check for existence of /client.db file.
        then
                          su pdroot "-c /bin/rm /var/opt/pd/basicdsd.db "
        echo "The file "/var/opt/pd/basicdsd.db" has been removed."
        fi
fi


# Restart pdclientd if none are running
typeset -i count=0
## find out how many pdclient daemons are running
count=`ps -t ? | grep 'pdclientd' | wc -l | awk '{ print $1 }'`
echo "count=" $count
if [[ count -eq 0 ]]
then
        $PD_LBIN/pdclientd
        ret_code=$?

        # Assume couldn't start because message queue exists
        if [[ $ret_code -ne 0 ]]
        then
                echo "pdclientd failed to start - issuing ipcrm -Q 5ac command"
                echo "STARTPD : pdclientd failed to start - issuing ipcrm -Q 5ac command"
                su pdroot "-c ipcrm -Q 5ac"
                echo "Restarting pdclientd"
                $PD_LBIN/pdclientd   # try again
                ret_code=$?
                if [[ $ret_code -ne 0 ]]
                then
                        print "Error starting pdclient daemon, return code $ret_code."
                        exit 1
                fi
        fi
fi

############################################################################

# Echo the variables back to the user
echo "queue name = $QUEUE"
echo "spooler name = $SPOOLER"
echo "supervisor name = $SUPERVR"
echo "logical printer name = $LPRINTER"
echo "physical printer name = $PPRINTER"
echo "Execution path = $PD_PATH"

############################################################################
# Start the spooler(s) and supervisor(s).
############################################################################


# Echo the server names back to the user
echo "spooler name = $SPOOLER"
echo "supervisor name = $SUPERVR"

# Create working directories for servers

if [ -d "$WORKDIR/work" ]
then
        su pdroot "-c /bin/rm -rf $WORKDIR/work"
        echo "The old directory $WORKDIR/work has been removed."
fi
echo "Creating new directories under $WORKDIR/work"

# Create all the subdirectories

echo "creating directory $WORKDIR/work"
mkdir $WORKDIR/work
if  [[ $? -ne 0 ]] then
        echo "STARTPD : Error creating directory $WORKDIR/work"
        exit 1
fi

echo "creating directory $WORKDIR/work/splr"
mkdir $WORKDIR/work/splr
if  [[ $? -ne 0 ]] then
        echo "STARTPD : Error creating directory $WORKDIR/work/splr"
        exit 1
fi

echo "creating directory $WORKDIR/work/spvr"
mkdir $WORKDIR/work/spvr
if  [[ $? -ne 0 ]] then
        echo "STARTPD : Error creating directory $WORKDIR/work/spvr"
        exit 1
fi

echo "creating directory $WORKDIR/work/spvr/pdb"
mkdir $WORKDIR/work/spvr/pdb
if  [[ $? -ne 0 ]] then
        echo "Error creating directory $WORKDIR/work/spvr/pdb"
        exit 1
fi

echo "creating directory $WORKDIR/work/splr/pdb"
mkdir $WORKDIR/work/splr/pdb
if  [[ $? -ne 0 ]] then
        echo "Error creating directory $WORKDIR/work/splr/pdb"
        exit 1
fi

echo '
*
*  Trace Configuration File
*
traceon = 0
size = 100000
path = .
wrap = 1
*groupmap = ???? -1
groupmap = P??? -1
groupmap = S??? -1
groupmap = MOBJ -1
groupmap = K??? -1
default = 1
' > $WORKDIR/work/spvr/suv.cfg

echo '
*
*  Trace Configuration File
*
traceon = 0
size = 100000
path = .
wrap = 1
*groupmap = ???? -1
groupmap = P??? -1
groupmap = S??? -1
groupmap = MOBJ -1
groupmap = K??? -1
default = 1
' > $WORKDIR/work/splr/spooler.cfg

########################## START SPOOLER ###############################
########################## START SUPERVISOR ############################

(
cd $WORKDIR/work/splr
echo "About to start spooler"
waitfor "spooler is listening" 120 "$PD_LBIN/spooler $SPOOLER"
) &
splr_pid=$!

(
cd $WORKDIR/work/spvr
echo "About to start supervisor"
waitfor "supervisor is listening" 120 "$PD_LBIN/supervisor $SUPERVR"
)
wait $splr_pid

############################################################################
#  Verify spooler and server are running.
############################################################################
echo "List the spooler status - should be READY"
$PD_PATH/pdls -c server "$SPOOLER"
ret_code=$?
if  [[ $ret_code -ne 0 ]]
then
        echo "STARTPD : Error $ret_code executing pdls -c server $SPOOLER ***"
        exit 1
fi



############################################################################
#  Create queue and verify status.
############################################################################
$PD_PATH/pdcreate -c queue -x "-scheduler-ready=scheduler-fifo" $SPOOLER:$QUEUE
if [ $? -ne 0 ]
then
        echo "STARTPD : pdcreate ERROR for queue object"
        exit 1
fi

#sleep 5

echo "List the queue status - should be FIFO"
$PD_PATH/pdls -c queue $SPOOLER:$QUEUE
if  [[ $? -ne 0 ]]
then
        echo "STARTPD : *** Error executing pdls -c server $QUEUE ***"
        exit 1
fi

############################################################################
#  Create logical printer, verify status, then enable and verify..
############################################################################
## Removed from create

$PD_PATH/pdcreate -x "-class=printer \
-printer-model=4019 \
-document-formats-supported=d-f-asc d-f-ps d-f-hp-pcl \
-printer-realization=logical \
-printer-backend-operation-mode-ready=o-m-standalone \
-device-name=$LPDEV \
-sides-supported=1 2 \
-sides-ready=1 \
-scheduler-ready=scheduler-fifo \
-queue-supported=$QUEUE " $SPOOLER:$LPRINTER

#-qdaemon-backend-cmd piobe \  # pulled out for now

if [ $? -ne 0 ] ; then
        echo "STARTPD : pdcreate ERROR for logical printer"
        exit 1
fi

#sleep 5

echo "List the logical printer status"
echo " "
$PD_PATH/pdls -c printer $LPRINTER
if  [[ $? -ne 0 ]]
then
        echo "*** Error executing pdls -c printer $LPRINTER ***"
        echo "STARTPD : Error executing pdls -c printer $LPRINTER ***"
        exit 1
fi

$PD_PATH/pdenable -c printer $LPRINTER


echo " "
echo "List the logical printer status"
echo " "
$PD_PATH/pdls -c printer $LPRINTER
if  [[ $? -ne 0 ]]
then
        echo "*** Error executing pdls -c printer $LPRINTER ***"
        echo "STARTPD : Error executing pdls -c printer $LPRINTER ***"
        exit 1
fi

echo " "
echo "List the supervisor status - should be READY"
echo " "
$PD_PATH/pdls -c server "$SUPERVR"
ret_code=$?
if  [[ $ret_code -ne 0 ]]
then
        echo "*** Error executing pdls -c server $SUPERVR ***"
        echo "STARTPD : *** Error $ret_code executing pdls -c server $SUPERVR ***"
        exit 1
fi


############################################################################
#  Create physical printer and verify status.
############################################################################

$PD_PATH/pdcreate -x "-class=printer \
-printer-model=4019 \
-document-formats-supported=d-f-asc d-f-ps d-f-hp-pcl \
-printer-realization=physical \
-device-support-system=device-support-system-piobe \
-device-name=$LPDEV \
-sides-supported=1 \
-sides-ready=1 \
-maximum-copies-supported=50 \
-scheduler-ready=scheduler-fifo \
-queue-supported=$QUEUE " $SUPERVR:$PPRINTER

#-qdaemon-backend-cmd piobe \

if [ $? -ne 0 ] ; then
        echo "pdcreate ERROR for physical printer"
        exit 1
fi

#sleep 20

echo "\nList the physical printer status\n"
$PD_PATH/pdls -c printer $PPRINTER
if  [[ $? -ne 0 ]]
then
        echo "*** Error executing pdls -c printer $PPRINTER  ***"
        exit 1
fi

echo " "
$PD_PATH/pdenable -c printer $PPRINTER

echo " "
echo "List the physical printer status"
echo " "
$PD_PATH/pdls -c printer $PPRINTER
echo ""
if  [[ $? -ne 0 ]]
then
        echo "*** Error executing pdls -c printer $PPRINTER "
        exit 1
fi

exit 0
#
#    Version      Date     Time    Owner   Comment
# ------------- -------- -------- -------- ----------------------------
# V             03/10/94 14:22:02 longstaf New
# V             03/10/94 14:22:15 longstaf New
