#!/bin/sh
# @(#) $Header: option8solaris,v 1.9 93/02/22 15:07:47 ss2 Exp $
# log file $LOG is /tmp/hpnpcfg.log


LPINTF=/etc/lp/interfaces

if [ -d $LPINTF/model.orig ]
then
  echo "Currently configured network printers in ${LPINTF}/model.orig:"
  echo "-----------------------------------------------------------------"
  PERIPHS=`ls $LPINTF/model.orig`
  if [ "$PERIPHS" = "" ]
  then
    echo "(no printer has been configured)"
  else
    ls $LPINTF/model.orig | more
  fi
fi

while [ 0 ]
do
  getspoolname "option8solaris" "$1" ""
  if [ $? != 0 ]
  then
    exit
  fi

  SPOOLNAME=`cat $TMP/SPOOLNM`
  rm $TMP/SPOOLNM

  #  Check if this printer is in spooler.  If not give message
  #  and prompt to enter printer name.

  if [ -f $LPINTF/$SPOOLNAME ]
  then
    break
  else
    echo ""
    echo "printer $SPOOLNAME is not part of the spooler."
  fi
done

#
# This checks the queue for all hosts that can queue jobs to it.
#
if grep $SPOOLNAME /var/spool/lp/requests/*/*  >/dev/null 2>&1
then
  echo "\nCannot remove the printer queue $SPOOLNAME! There are jobs there" | tee -a $LOG
  exit 1
fi

echo  ""
echo "Ready to shut down the spooler and remove the spooler destination/queue."
echo "If the spooler is not currently running, it will remain shutdown."  
echo "Otherwise, it will be restarted."
echo "If there are jobs in the queue, this queue will NOT be removed."

while [ 0 ]
do
  echo ""
  $ECHO "OK to continue? (y/n/q default=y) $NL"
  read OK
  if [ "$OK" = "" -o "$OK" = 'y' ]
  then
    break
  fi
  if [ "$OK" = 'n' -o "$OK" = 'q' ]
  then
    exit 1
  fi
  if [ "$OK" = '?' ]
  then
    echo ""
    echo "Enter \"y\" to remove the spooler destination."
    echo ""
    echo "Enter \"q\" or \"n\" to return to the main menu."
  fi
done

echo "" >> $LOG
echo "Removing $SPOOLNAME from spooler" >> $LOG
echo "" | tee -a $LOG

# disable and reject queue
/usr/lib/reject $SPOOLNAME
disable $SPOOLNAME

# logging AFTER the operation, 
STARTLP=0
if /usr/lib/lpshut
then
  STARTLP=1
  echo /usr/lib/lpshut >> $LOG
else
  echo The spooler is not running | tee -a $LOG
fi

# check the result of lpadmin -x, if failed, abort 
ABORT="NO"
sleep 4
/usr/lib/lpadmin -x $SPOOLNAME
if [ $? -ne 0 ]
then 
     ABORT="YES"
     echo "Cannot delete this printer queue from the spooler," | tee -a $LOG
     echo "Check /tmp/hpnpcfg.log." | tee -a $LOG
else
     echo /usr/lib/lpadmin -x$SPOOLNAME >> $LOG
fi

if [ "$STARTLP" = "1" ]
then
      /usr/lib/lpsched
      echo /usr/lib/lpsched >> $LOG
else
      echo " " | tee -a $LOG
      echo The Spooler remains shutdown | tee -a $LOG
fi

if [ $ABORT != "YES" -a -f  $LPINTF/model.orig/$SPOOLNAME ]
then
  rm $LPINTF/model.orig/$SPOOLNAME
  echo rm $LPINTF/model.orig/$SPOOLNAME >> $LOG

  echo "" | tee -a $LOG
  echo "The spooler destination $SPOOLNAME has been removed from the spooler."
fi

$ECHO "Press the return key to continue ... $NL"
read RESP
exit 0

