Listing 7. Script to Activate Printcap Entries

#! /bin/sh
echo "      spool.chk   mod 11-15-99 mjp"
if [ $1. = debug. ]; then
   set -xv; shift 1
fi
# start/stop script
INIT=/sbin/init.d/bsd
LPC='/usr/sbin/lpc'
if [ ! -f $LPC ]; then
   LPC=`which lpc 2>/dev/null`
fi
if [ -z "$LPC" ]; then
   echo "BSD printing doesn't seem to be"
   echo " installed on this system because I"
   echo " I can't find 'lpc'."
   exit
fi
if [ -z "$1" ]; then
   echo "Script to reset a print queue & check"
   echo " & optionally create spool directories"
   echo " defined in /etc/printcap."
   echo " "
   echo "Usage: spool_chk [-c] [queue_name ...]"
   echo " "
fi
if [ "$1" = "-c" ]; then
   action=c
   reset=y
   shift 1
else
   echo -n "Create missing directories & log"
   echo " files, or List only (c/l) [c]? "
   read action
   if [ -z "$action" ]; then action=c; fi
fi
if [ -n "$1" ]; then
   if [ "$1". = all. ]; then
      ques=`awk -F: 'NF<1{next}
         $1~/^#/ || $2~/#|=/{next}
         $1~/\|/{char=index($1,"|") - 1
         printf "%s ", substr($1,1,char)}
         $1!~/\|/ {printf "%s ",$1}'\
         /etc/printcap`
   else
      ques="$*"
   fi
   if [ -z "$reset" ]; then
      echo -n 'Abort & Restart the queue (y/n)\
 [y]? '
      read reset
   fi
else
   #   get list of spool directories:
   qu=`awk -F: 'NF<1{next}
   $1~/^#/ {next} $0~/\|/{print}' \
   /etc/printcap | awk -F\
| '{printf "%s ",$1}'`
   echo $qu
   echo " "
   echo -n "Which of these queues do you want to\
 check [all]?
"
   read ques
   if [ "$ques". = . ] || \
      [ "$ques". = all. ]; then
      ques="$qu"
   fi
   echo " "
   if [ -z "$reset" ]; then
      echo -n 'Abort & Restart the queues (y/n)\
 [y]? '
      read reset
   fi
fi
if [ $reset. = . ]; then reset=y; fi
if [ ! -d /var/spool/lpd ]; then
   mkdir /var/spool/lpd
fi
for que in $ques; do
   echo -n "${que}: "
   # grab the full printcap entry for this queue:
   sed -n /$que/,'/|/p' /etc/printcap |
      sed -n '1,/:$/p' >/tmp/prtr$$
   # get the spool directory, stripping off colons
   # & whitespace:
   dir=`sed -n s/sd=//p /tmp/prtr$$ | \
      head -1 | tr -d ':\011\040\134'`
   if [ -d $dir ]; then
      echo "$dir exists."
      if [ $reset. = y. ]; then
         if [ -x $INIT ]; then
            $INIT stop
            $INIT start
         else
            $LPC restart $que
         fi
      fi
   else
      if [ $action. = c. ];then
         mkdir $dir
         if [ $? -eq 0 ]; then
            chown daemon.daemon $dir
            $LPC start $que
            if [ $? -eq 0 ]; then
               echo "$que started."
            else
               echo "Couldn't start $que"
            fi
         else
            echo "mkdir on $dir failed"
         fi
      else
         echo "$dir is missing."
      fi
   fi
   if [ $action. != c. ];then
      ls -l $dir
      continue
   fi
   # parse out log file location
   log=`sed -n s/lf=//p /tmp/prtr$$ | \
      head -1 | tr -d ':\011\040\134'`
   if [ ! -f $log ]; then
      touch $log
      if [ $? != 0 ]; then
         echo "Couldn't create $log."
      else
         chmod a+wr $log
         chown lp.lp $log
         echo "$log created"
      fi
   fi
   rm -f /tmp/prtr$$      # clean up
done