#!/bin/sh
#
# FMprinterlist -- generates a list of printers
# 
# Copyright (c) 1998 Adobe Systems, Incorporated. All rights reserved.
# 

usage="Usage: `basename $0`  [ -notsilent ] listfile"

# By default, the FrameMaker product will run
#    FMprinterlist <LISTFILE> &
# at start-up, in case lpstat takes a long time to run.
# On some systems, "lpstat -a" takes a long time to browse the network.
# Then when the printer dialog comes up the first time,
# the FrameMaker product will get the list from <LISTFILE>
# and use that list of printers for the whole session.
# If <LISTFILE> does not exist yet when the printer dialog comes up,
# the FrameMaker product will look again next time the dialog comes up,
#
# If Maker quits quickly and FMprinterlist runs slowly, Maker may quit
# before FMprinterlist and LISTFILE's directory may disappear.
# To avoid error messages, stderr is routed to /dev/null.
# Use the -notsilent argument to disable this and see stderr messages.
#
# If the user has set the X resource
# renewPrinterListForDialog to True, maker will call 
#   FMprinterlist <LISTFILE>
# to generate a list of printers each time the print dialog is invoked,
# in order to load the latest list of printers.
#
# This script is very careful to only write <LISTFILE> if there is
# something to put into it.
#
# The FrameMaker product will use
#   ./fminit/FMprinterlist  if it exists, otherwise it will use
#   ~/fminit/FMprinterlist  if it exists, otherwise it will use
#   $FMHOME/fminit/FMprinterlist, this file.
#
# If you want your own version of this script that gets the list from your
# own file MYFILE, make a ./fminit/FMprinterlist or ~/fminit/FMprinterlist
# which just does:
#      #!/bin/sh
#      cp MYFILE $1
#
# The form of the file is an optional first line:
#    ### list from <program> by: $0  <date>
# followed by the list of printers.
# This file exists for the duration of the maker session,
# so a user can find and see when it was created and by what script.
#
# FMprinterlist tries to generate a printer list using "lpstat -a"
# and if that produces no names, it tries looking in both of
# /etc/lp/printers  and /etc/lp/classes.  If these fail it looks
# in /etc/printcap.

# add some debugging options so we can test the different
#   ways of getting printer lists easily
# setting a debugging option turns silence off.
dbg=0
notsilent=0
if [ $# -eq 2 ] ; then
  dolpstat=0
  doprintcap=0
  doprinters=0
  case $1 in
    -notsilent) notsilent=1 ;         
                dolpstat=1 ; doprintcap=1 ; doprinters=1 ; shift ;;
    -lpstat)   dolpstat=1   ; dbg=1 ; shift ;;
    -printcap) doprintcap=1 ; dbg=1 ; shift ;;
    -printers) doprinters=1 ; dbg=1 ; shift ;;
    -none)                    dbg=1 ; shift ;;
  esac
  if [ $dbg ] ; then
    notsilent=1
    # When debugging, often rerun with same args, so nuke
    #   the target file at outset.
    rm -f "$1"
  fi
else
  dolpstat=1
  doprintcap=1
  doprinters=1
fi

if [ $notsilent -eq 0 ] ; then
  exec 2>/dev/null
fi

# ensure legal arguments
if [ $# -ne 1 ] ; then
	echo $usage >&2
	exit 3
fi

file="$1"


# make sure we can write to this dir
touch $file.$$
if [ -w $file.$$ ] ; then
   rm -f $file.$$
else
  echo "$0: can not write $file.$$" >&2
  echo "$usage" >&2
fi


# header line for output file
hd1="### list from"
hd2="written by: $0 "

rm -f $file  $file.*

gotit=0

# #### lpstat -a
# IBM's lpstat -a starts with a header line "Queue   Dev..."
if [ $dolpstat -eq 1 ] ; then
  touch $file.tmp
  lpstat -a | grep -v "Queue   Dev" > $file.tmp
  # nuke lines starting with space, tab, # or dash & blank lines
  egrep '^[^- #	]' $file.tmp | sed -e "s/[ 	].*$//g" > $file.list
  if [ -s $file.list ] ; then
    echo "$hd1 'lpstat -a' $hd2 `date`" > $file.tmp
    gotit=1
  fi
fi

# #### /etc/lp/printers and /etc/lp/classes
if [ $gotit -eq 0  -a  $doprinters -eq 1 ] ; then
  rm -f $file.list
  if [ -d /etc/lp/printers ] ; then
    ls /etc/lp/printers > $file.list
  fi
  if [ -d /etc/lp/classes  ] ; then
    ls /etc/lp/classes >> $file.list
  fi
  if [ -s $file.list ] ; then
    echo "$hd1 /etc/lp/printers $hd2 `date`" > $file.tmp
    gotit=1
  fi
fi

    
# #### /etc/printcap
if [ $gotit -eq 0  -a  $doprintcap -eq 1 ] ; then
  if [ -f /etc/printcap ] ; then
    # nuke lines starting with space, tab, # or dash & blank lines
    egrep '^[^- #	]' /etc/printcap | sed -e 's/[|:].*//g' > $file.list
    if [ -s $file.list ] ; then
      echo "$hd1 /etc/printcap $hd2 `date`" > $file.tmp
      gotit=1
    fi
  fi
fi


# #### write the output file
if [ $gotit -eq 1 ] ; then
  cat $file.tmp $file.list > $file
else
  echo "### no printers found by $0, `date`" > $file
fi


# #### cleanup
if [ $dbg -eq 0 ] ; then
  rm -f $file.tmp $file.list
fi
