#!/bin/csh
#####################################################################
# set default parameters
####################################################################
set PRINTHOST = local
#set PRINTHOST = remote
set PID = $$
set filename = ""
set pitch = "10"
set leftmargin = 10
set complete = 0

  #---------------------------------------------------------------
  # Interactive parameter input
  set complete = 0
  while ($complete == 0)
      #Display current parameter settings
      echo '___________________________________________________________________'
      echo '                    EPRINT - Version 23 May 97                     '
      echo '               Print an ASCII file to Epson LQ-510'
      echo '___________________________________________________________________'
      echo 'Filename:' $filename
      echo '-------------------------------------------------------------------'
      echo '   Parameter              Description                      Value'
      echo ''
      echo '1) Filename    File to print                               '
      echo '2) Pitch       characters per inch (10,12,15)              '$pitch
      echo '3) L. margin   #columns to indent                          '$leftmargin
      echo '   -------------------------------------------------------------'
      echo '   Type number of your choice or 0 to continue:'

      #Prompt for parameter to change
      set paramnum = $<
      switch ($paramnum)
        case 0:
          if ($filename == "")  then 
             echo ">>> Must specify filename"
             echo ">>> Press RETURN to continue"
             set filename = $< 
          else
            set complete = 1
          endif
          breaksw
        case 1:
          echo 'Name of file to be printed:'
          set filename = $<
          if ($filename != "") then
            if  (-e $filename  && -r $filename) then
            else
                echo ">>> $filename nonexistent or read-protected"
                echo ">>>Enter another filename"
                set filename = ""
            endif
          endif
          breaksw
        case 2:
          echo 'Pitch to use (10,12, or 15):'
          set pitch = $<
          if ($pitch == 10 || $pitch == 12 || $pitch == 15) then
          else
                echo ">>> invalid pitch specified"
                echo ">>> Pitch reset to 10"
                set pitch = "10"
          endif
          breaksw
        case 3:
          echo 'Left margin, columns (0..80):'
          set leftmargin = $<
          if ($leftmargin >= 0 && $leftmargin <= 80) then
          else
                echo ">>> invalid left margin specified"
                echo ">>> Reset to 10 columns"
                set leftmargin = 10
          endif
          breaksw
        default:
          breaksw
        endsw
       #If parameter chosen is 0, and a minimal set of parameters are
      #set, terminate the loop (complete=1)
    end #while


# Create file for epson LQ-510
set tempfile = /tmp/plq510.$PID


echo '@' > $tempfile    # reinitialize the printer
switch ($pitch)
    case 12:
         echo ' M' >> $tempfile   # set 12 pitch
         breaksw
    case 15:
         echo 'g' >> $tempfile     # set 15 pitch
         breaksw
    default:
         breaksw
endsw

#Append the file indented $leftmargin characters
set pagelength = `wc -l $filename`

# For reasons that aren't clear, including $filename in the command
# causes two copies of the file to be printed. Re-directing input
# from $filename does not.
pr  -t -o$leftmargin -l$pagelength < $filename >> $tempfile
echo '' >> $tempfile            # FormFeed
echo '@' >> $tempfile           # reinitialize the printer

# print the file
if ($PRINTHOST == local) then
   lpr -l -Pplants.bfprint1 $tempfile
else
   set PRINTHOST = `choosehost`
   set USERID = `whoami`
   set REMOTEFN = $1.$PID
   echo Printing $filename on $PRINTHOST...
   rcp $tempfile  $USERID@$PRINTHOST\:$REMOTEFN
   rsh $PRINTHOST -l $USERID lp -d plants.bfprint1 $REMOTEFN
   rsh $PRINTHOST -l $USERID /usr/bin/rm $REMOTEFN
endif

/usr/bin/rm $tempfile
