#!/bin/sh
#
# Slpd -- start/stop/check lpd
#
# Variant on the SVR4 rc script interface.
#
# This version allows checking to see if the daemon is
# running, and can be easily hacked for different daemons.
#
# -- Justin Mason <jmason@iona.ie>
#
############################################################

cmdline="/usr/local/bin/lpd"
regexp=' lpd'
noprocs=1
daemon_id="PLP line printer daemon"

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

pname=`basename $0`
piddir=/var/spool/pids
if [ -f /vmunix ] ; then
  SYSV=0
else
  SYSV=1
fi
thisid=$$

echon () {
  case $SYSV in
  1 ) /bin/echo "$*\c" ;;
  0 ) /bin/echo -n "$*" ;;
  * ) echo "[echon is broken] $*"
  esac
}

grepproc () {
  if [ $# -lt 2 ] ; then
    echo "usage: grepproc { -s regexp | \"command\" regexp }" 1>&2
    return
  fi

  if [ "x$1" = "x-s" ] ; then
    show_lines=y
  else
    show_lines=n ; cmd=$1
  fi
  shift

  if [ $SYSV = 0 ] ; then
     ps alxww | egrep -e "$*"|grep -v grep|while read f uid pid ppid rest ; do
       if [ $show_lines = y ] ; then
         echo "$f $uid $pid $ppid $rest"
       else
         /bin/echo -n "$pid "; eval $cmd $pid
       fi
     done
  else
     ps -ef | egrep -e "$1"|grep -v grep|while read uname pid ppid rest ; do
       if [ $show_lines = y ] ; then
         echo "$uname $pid $ppid $rest"
       else
         /bin/echo "$pid \c"; eval $cmd $pid
       fi
     done
  fi
  [ $show_lines = n ] && echo
}

case "$1" in

  "start" )
     $cmdline &&	echo "started: $daemon_id."
     ;;

  "stop" )		echon "Halting $daemon_id: "
     x=`lpc lpd | awk '{print $3}'`
     if [ "$x" = "not" ] ; then
	grepproc "kill -15" "$regexp"
     else
	kill -15 $x
     fi
     ;;

  "restart" )		echo "Restarting $daemon_id: "
     $0 stop
     $0 start
     ;;

  "isup" )		echo "There should be $noprocs processes here."
     grepproc -s "$regexp"
     ;;

  "info" )		echo "Information on $daemon_id: "
     ;;

  "check" )
     if [ `grepproc -s "$regexp" | wc -l` -lt $noprocs ] ; then
       echo "$0 check failed."
       $0 isup
       $0 restart
     fi
     ;;

  * ) echo "usage: $pname {start|stop|restart|isup|info|check}"
     ;;
esac
