#!/bin/csh -f

# Watch for jobs of $USER in queue, and ring bell once when active,
# thrice when number of jobs decreases.

set printer = $1
if ($printer == "" && $?PRINTER) then
   set printer = $PRINTER
endif
set tempfile = /tmp/watchprinter.$USER.$$

set lastjobs = 0
while (1)
  set curjobs = ( `lpq -P$printer | fgrep $USER | tee $tempfile | wc -l`)
  if ($lastjobs == 0) echo "Watching $curjobs print jobs"
  if ($curjobs<$lastjobs)\
    echo print job on $printer finished\; $curjobs left
  if ( { grep active $tempfile } )\
    echo one print job of $curjobs active on $printer
  if ($curjobs == 0) then
    rm $tempfile
    exit(0)
  endif
  set lastjobs = $curjobs
  sleep 120
end
