
#!/bin/csh
# File Name:  lp_prog
# Purpose:    This file is a c-shell script that is called from any Genesis print
#             command. It uses lp_prog.config to provide a list of available printers
# Written by: Ian Ticehurst (Autom8 Ltd.)
# Version 1A: Date: 04.09.00 - Initial design and development.
# Version 1B: Date: 05.09.00 - Inclusion of NT printer search as supplied by Ben (Frontline.).
# Version 1C: Date: 27.10.00 - Addition of $TMP definition and useage.
# Version 1D: Date: 04.12.00 - Modify "gui" alias line to generic path
# Version 1E: Date: 05.12.00 - Removal of "gui" alias.
# Version 21: Date: 21.04.01 - Revamped & Tested - Ben
############################# Start of notes ###############################
# This C-shell script is called by Genesis to print files. It
# uses lp_prog.config to store a list of available printers. If
# this file does not exist, the program attempts to detect printers
# available, allows the user to test them, and then stores them in
# lp_prog.config.
#
# Note: On Windows NT/2000 printers are named as: "//myprintserver/myprinter"
#
############################# End of notes ###############################

# Set this variable to specify the list of available printers
set PRINTERS = ""

# Set the maximum number of computer to scan for printers Windows
set MAX_SCAN = 40

if ($#argv != 2) then
   echo "Syntax: lp_prog <num_copies> <file>"
   exit 1
endif

set num_copies = $1
set file       = $2

if ("$num_copies" <= 0) then
   echo "Illegal number of copies: $num_copies"
   exit 1
endif

if (! -e "$file") then
   echo "Error: $file does not exist"
   exit 1
endif

# Define correct temporary directory

if (! $?GENESIS_DIR) then
   set GENESIS_DIR = /genesis
endif

if ($?GENESIS_TMP) then
   set TMP = $GENESIS_TMP
else
   set TMP = $GENESIS_DIR/tmp
endif

# Set up gui data and response files.
set GUI_DATA = $TMP/gui_data.$$
set GUI_RESP = $TMP/gui_resp.$$
alias o 'echo \!:* >> $GUI_DATA'
unsetenv PCMAP

# Detect operating system.
set CURR_OS = `uname -s`

if ($CURR_OS == WINDOWS_NT) then
   set EXE = ".exe"
else
   set EXE = ""
endif

if (-e $GENESIS_EDIR/all/gui$EXE) then
   alias gui $GENESIS_EDIR/all/gui$EXE
else if (-e $GENESIS_DIR/$GENESIS_EDIR/all/gui$EXE) then
   alias gui $GENESIS_DIR/$GENESIS_EDIR/all/gui$EXE
else
   echo gui utility not found - exiting
   exit 1
endif

# Define a variable for the printer configuration file (lp_prog.config).
if (-e $HOME/.genesis/hooks/lp_prog) then
   set PRINT_CONFIG_FILE = $HOME/.genesis/hooks/lp_prog.config
else
   set PRINT_CONFIG_FILE = $GENESIS_DIR/sys/hooks/lp_prog.config
endif

if (-e "$PRINT_CONFIG_FILE") then
   source $PRINT_CONFIG_FILE
endif

if ("$PRINTERS" != "") then
   goto select_printer
endif

set echo
# Check whether we can write configuration file
touch $PRINT_CONFIG_FILE
if ($status != 0) then
   o WIN 200 200
   o BW 0
   o FONT tbr18
   o BG 992222
   o FG 999999
   o LABEL No permission to modify $PRINT_CONFIG_FILE
   o LABEL Aborting Program
   o END
   gui $GUI_DATA > $GUI_RESP
   \rm $GUI_DATA $GUI_RESP
   goto abort
endif

# find printers
switch ($CURR_OS)
case "HP-UX"
case "SunOS"
case "AIX"
case "Linux"
   if ($CURR_OS == "AIX") then
      set PRINTERS_FOUND = `lpstat -a | grep -i "@prin" | awk '{print $1}'`
   else
      set PRINTERS_FOUND = `lpstat -a | awk '{print $1}'`
   endif
   breaksw
case "WINDOWS_NT"
   set COMPUTERS = `net view /network | awk95 '/^\\/ {print substr($1,3)}'`

   # Check for printers on each computer found.
   set PRINTERS_FOUND = ""

   if ($#COMPUTERS > $MAX_SCAN) then
      set COMPUTERS = ($COMPUTERS[1-$MAX_SCAN])
   endif

   foreach COMP ($COMPUTERS)
      echo Scanning $COMP
      # Find the names of the printers on each computer
      set PRINTERS_ON_COMP = `net view /network \\\\$COMP | grep ' Print' | sed 's/Print.*//g'`

      # Add each printer found to $printers
      foreach PRINTER ($PRINTERS_ON_COMP)
         set PRINTERS_FOUND = ($PRINTERS_FOUND //$COMP/$PRINTER)
      end
   end
   breaksw
default
   o WIN 200 200
   o BW 0
   o FONT tbr18
   o BG 992222
   o FG 999999
   o LABEL Cannot detect operating system.
   o LABEL Aborting Program
   o END
   gui $GUI_DATA > $GUI_RESP
   \rm $GUI_DATA $GUI_RESP
   goto abort
endsw

# If no printers found, inform user and abort
if ($#PRINTERS == 0) then
   o WIN 200 200
   o BW 0
   o FONT tbr18
   o BG 992222
   o FG 999999
   o LABEL Printer Configuration Program.
   o LABEL No Available Printers Detected.
   o LABEL Aborting Program
   o END
   gui $GUI_DATA > $GUI_RESP
   \rm $GUI_DATA $GUI_RESP

   goto abort
endif

set do_print_test = 1
while ($do_print_test)
   # Offer user the list of printers to test.
   o WIN 200 200
   o BW 1
   o FONT tbr18
   o BG 222299
   o FG 999999
   o LABEL Printer Configuration
   o LABEL Select a printer to test:
   o BG 222266
   o LIST SELECTED_PRINTER 10 S
   foreach PRINTER ($PRINTERS_FOUND)
      if ($CURR_OS != WINDOWS_NT) then
         set PRINTER = `echo $PRINTER | tr 'A-Z' 'a-z'`
      endif
      o $PRINTER
   end
   o END
   o BG 222299
   o END

   gui $GUI_DATA > $GUI_RESP
   source $GUI_RESP;
   \rm $GUI_DATA $GUI_RESP

   # Attempt to print a file on the chosen printer (use lyr_rule file).

   set test_file = $GENESIS_DIR/sys/hooks/lyr_rule
   if ($CURR_OS != WINDOWS_NT) then
      lp -d $SELECTED_PRINTER $test_file
   else
      cd $GENESIS_TMP
      set PRINTER = $SELECTED_PRINTER:as#/#\\#

      # Run print routine for number of copies.
      set line1 = `head -1 $test_file`
      if ("$line1" != %\!PS) then
         sed 's/$//g' < $test_file > $GUI_RESP
         set test_file = $GUI_RESP
      endif
      set test_file = $test_file:as#/#\\#
      echo "print /d:$PRINTER $test_file" > lp_tmp$$.bat
      ./lp_tmp$$.bat
      \rm lp_tmp$$.bat
      if ("$line1" != %\!PS) then
         \rm $GUI_RESP
      endif
   endif

   # Ask if test was successful and offer user various options.
   o WIN 200 200
   o BW 1
   o FONT tbr18
   o BG 222299
   o FG 999999
   o LABEL Printer Configuration
   o LABEL Did file print OK\? Select option
   o RADIO PRINT_TEST Select: V 1 992222
   o Yes, use only this printer
   o Yes, add this to printer list - test another printer
   o Yes, add this to printer list - do not test another printer
   o No, let me test another printer
   o No, abort program
   o END

   gui $GUI_DATA > $GUI_RESP
   source $GUI_RESP;
   \rm $GUI_DATA $GUI_RESP

   if ($PRINT_TEST == 1) then
      # Only printer used
      set PRINTERS       = "$SELECTED_PRINTER"
      set do_print_test = 0
   else if ($PRINT_TEST == 2) then
      # Add this to selection.  Test another
      set PRINTERS = ($PRINTERS $SELECTED_PRINTER)
   else if ($PRINT_TEST == 3) then
      # Add to list. Do not test another
      set PRINTERS = ($PRINTERS $SELECTED_PRINTER)
      set do_print_test = 0
   else if ($PRINT_TEST == 4) then
      # Test another printer
      continue
   else if ($PRINT_TEST == 5) then
      # Abort
      goto abort
   endif
end
                
# Create lp_prog.config file
echo set PRINTERS = \($PRINTERS\) > $PRINT_CONFIG_FILE

# Inform user that configuration is complete.
o WIN 200 200
o BW 0
o FONT tbr18
o BG 222299
o FG 999999
o LABEL Printer Configuration
o LABEL Printer configuration complete
o BG 884466
o FONT tbi18
o LABEL To re-run printer configuration,
o LABEL remove $PRINT_CONFIG_FILE.
o FONT tbr18            
o END

gui $GUI_DATA
\rm $GUI_DATA

select_printer:
        
if ($#PRINTERS > 1) then
   # Ask user to select printer.
   o WIN 200 200
   o BW 1
   o FONT tbr18
   o BG 222299
   o FG 999999
   o LABEL Printer Selection Pop-up.
   o LABEL Select the desired printer from the list below.
   o BG 222266
   o LIST SELECTED_PRINTER 10 S
   foreach PRINT ($PRINTERS)
      o $PRINT
   end
   o END
   o FONT tbi14
   o BG 884466
   o LABEL To print using default printer
   o LABEL \(without this pop-up\)
   o LABEL see lp_prog.config
   o FONT tbr18
   o BG 222299
   o END

   gui $GUI_DATA > $GUI_RESP
   source $GUI_RESP
   \rm $GUI_DATA $GUI_RESP

   set PRINTER = $SELECTED_PRINTER
else
   set PRINTER = $PRINTERS[1]
endif

if ($CURR_OS != WINDOWS_NT) then
   lp -d $PRINTER -n $num_copies $file
else
   cd $GENESIS_TMP
   set line1 = `head -1 $file`
   if ("$line1" != %\!PS) then
      # Add ^M for printing text files
      sed 's/$//g' < $file > $GUI_RESP
      set file = $GUI_RESP
   endif
   set file    = $file:as#/#\\#
   set PRINTER = $PRINTER:as#/#\\#

   # Run print routine for number of copies.
   echo "print /d:$PRINTER $file" > lp_tmp$$.bat
   @ i = 0
   while ($i < $num_copies)
      ./lp_tmp$$.bat
      @ i++
   end
   if ("$line1" != '%\!PS') then
      \rm $file
   endif
   rm lp_tmp$$.bat
endif

exit 0

abort:

exit 1
###################### End Of Program ###################################
