#! /bin/sh
# Fprint
# Script to translate metafiles generated by Ferret.  It runs either gksm2ps
# (on systems where Ferret uses xgks -- releases after Mar 1994), or mtt;
# please see the documentation on those.  It is intended to simplify sending
# plots to printers, to an output file only, or to a workstation screen.  This
# version translates to PostScript or Xwindow output.
#
# The script should work without modification to send output to the default
# printer at your site (whichever is specified in $PRINTER).
#
# The default behavior of this script is to translate metafiles to color PS (or
# grayscale) using line types appropriate for a monochrome PS printer.
#
# BUT the script should be modified to include the printers at your site.
# The section "MODIFY HERE" below contains case statements.  Each case is
# intended to be the name of a printer at your site, and the options set
# for that case determines the default action taken by the metafile translator.
# For example, whether lines are rendered in color or b&w.  The printers named
# in this template illustrate the use for supported device types.
#
# Examples of use:
#
# "Fprint metafile.plt" translates and prints 'metafile.plt' on $PRINTER
# "Fprint -P COLOR_PS metafile.plt" sends the plot to printer COLOR_PS
# "Fprint -o my_plot.ps metafile.plt" writes to a PS file named 'my_plot.ps'
# "Fprint -X metafile.plt" renders on the workstation screen.
#
# If you have questions please send email to davison@pmel.noaa.gov

# 12.15.94 Due to a bug in the DEC OSF lpr command, use of the -s (symbolic
# link, ie, don't copy the file -- used with large files) with the -r option
# (remove file after printing) cannot be done.  The file is deleted before it
# printed.  Consequently for OSF machines, I have dropped the use of the -r
# option and the print files will accumulate.

print_usage() {
   if [ -n "$err_txt" ]; then
      echo " "
      echo "ERROR: You made a syntax error near $err_txt"
      echo " "
   fi
   echo "Script to translate Ferret's graphics metafile(s) to PostScript"
   echo "usage: Fprint [-P printer || -o file_name || -X]"
   echo "              [-p landscape || portrait] [-# <number of copies>]"
   echo "              [-l ps || cps] [-R] metafile(s)"
   echo " "
   echo "   -P: send PostScript output to the named printer"
   echo "   -o: send PostScript output to a file and don't print the plots"
   echo "   -X: send the plots to your X Window for preview"
   echo "   -p: page orientation, landscape or portrait"
   echo "   -#: print more than one copy of the plots"
   echo "   -l: line styles, ps = monochrome, cps = color"
   echo "   -R: do not rename files with a date stamp appended (default is to stamp)"
   echo "   -C: Output a CMYK postscript file (default is RGB)"
   echo "Examples: "
   echo "    Fprint metafile.plt*"
   echo "    Fprint -P our_color_printer -l cps -# 10 metafile.plt*"
   echo "    Fprint -p portrait -R metafile.plt.~2~"
}

if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
   print_usage
   exit
fi

touch Fprint_output0.ps
rm -f Fprint_output*.ps

output_args=" "
output="printer"
printer_named=0
translator_args="-l cps -d cps"

gksm2ps_files=""
mtt_files=""
got_gksm2ps_files=0
got_mtt_files=0

REMOVE="-r"

# Pick off arguments til the end of the arguments
while [ $# != 0 ]; do
   case "$1" in
#     Send output to named printer
      "-P")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-P $1"
            print_usage
            exit 1
         fi
         output_args="$output_args -P$1"
         output="printer"
         fprinter=$1
         printer_named=1
         shift
         ;;
#     Output is to be directed to plot file and not printed
      "-o")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-o $1"
            print_usage
            exit 1
         fi
         translator_args="$translator_args -o $1"
         output="file"
         shift
         ;;
#     Output is to be directed to Xwindow
      "-X")
         shift
         translator_args=""
         output="Xwindow"
         ;;
#     Print N copies
      "-#")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-# $1"
            print_usage
            exit 1
         fi
         output_args="$output_args -#$1"
         shift
         ;;
#     Page orientation: landscape or portrait
      "-p")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-p $1"
            print_usage
            exit 1
         fi
         if [ "$1" != "landscape" ] && [ "$1" != "portrait" ]; then
            err_txt="-p $1"
            print_usage
            exit 1
         fi
         translator_args="$translator_args -p $1"
         shift
         ;;
#     Line color: cps (color) or ps (b&w)
      "-l")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-l $1"
            print_usage
            exit 1
         fi
         if [ "$1" != "cps" ] && [ "$1" != "ps" ]; then
            err_txt="-l $1"
            print_usage
            exit 1
         fi
         translator_args="$translator_args -l $1"
         shift
         ;;
#     Supported devices: cps (color PostScript) or phaser (TEK phaser ps)
      "-d")
         shift
         val=`echo $1 | cut -c1`
         if [ "$val" = "-" ]; then
            err_txt="-d $1"
            print_usage
            exit 1
         fi
         if [ "$1" != "cps" ] && [ "$1" != "phaser" ]; then
            err_txt="-d $1"
            print_usage
            exit 1
         fi
         translator_args="$translator_args -d $1"
         shift
         ;;
#     Don't append to metafiles with date stamp when translation is complete
      "-R")
         translator_args="$translator_args -R"
         shift
         ;;
#     Use CMYK color model
      "-C")
         translator_args="$translator_args -C"
         shift
         ;;
#     Everything else is passed to translator -- gotta be a file
      *)
         if [ ! -f $1 ]; then
            echo " "
            echo "ERROR: $1 is not a file name"
            echo " "
            print_usage
            exit 1
         fi
         if head -n 1 $1 | grep -q "^GKSM" ; then
            gksm2ps_files="$gksm2ps_files $1"
            got_gksm2ps_files=1
         else
            mtt_files="$mtt_files $1"
            got_mtt_files=1
         fi
         shift
         ;;
   esac
done

if [ "$output" = "printer" ] && [ $printer_named -ne 0 ]; then

############################ MODIFY HERE ###################################

#    Now set device characteristics for named printers
   case "$fprinter" in
#     If the printer is a color PS printer, use that device type
      "COLOR_PS")
         translator_args="$translator_args -l cps -d cps"
         ;;
#     BUT If the printer is a PHASER PX with transfer sheets, type is "phaser"
      "PHASER_PX_TRANSFER")
         translator_args="$translator_args -l cps -d phaser"
         ;;
#     The following example printers take the default options mentioned above.
      "MONOCHROME_PS1")
         ;;
      "MONOCHROME_PS2")
         ;;
      "MONOCHROME_PS3")
         ;;
#     The named printer is not a valid choice
      *)
#        This is the sort of message I anticipate the user getting
#        if one names an unsupported printer:
#
#        echo -n "'${fprinter}' is not a valid printer."
#        if [ -n "$PRINTER" ]; then
#           echo " The default printer is $PRINTER."
#        else
#           echo " "
#        endif
#        echo " Available printers are COLOR_PS, PHASER_PX_TRANSFER,"
#        echo " MONOCHROME_PS1, MONOCHROME_PS2, and MONOCHROME_PS3"
#
#        For now a user gets this message:
         echo " "
         echo " The Fprint script has not been set up to use the -P option"
         echo " yet -- it should work with the default printer."
         echo " "
         exit
   esac

##################### END OF "MODIFY HERE" SECTION ##########################

fi

# Now process the metafile(s) and print the plot file if appropriate
if [ `uname -s` = "OSF1" ]; then
   REMOVE=""
fi
case $output in
   "printer")
      if [ $got_gksm2ps_files -ne 0 ]; then
         gksm2ps -o Fprint_output1$$.ps $translator_args $gksm2ps_files
         if [ -f Fprint_output1$$.ps ]; then
            lpr -s ${REMOVE} $output_args ./Fprint_output1$$.ps
         fi
      fi

      if [ $got_mtt_files -ne 0 ]; then
         mtt -o Fprint_output2$$.ps $translator_args $mtt_files
         if [ -f Fprint_output2$$.ps ]; then
            lpr -s ${REMOVE} $output_args ./Fprint_output2$$.ps
         fi
      fi
      ;;
   "file")
      if [ $got_gksm2ps_files -ne 0 ]; then
         gksm2ps $translator_args $gksm2ps_files
      fi
      if [ $got_mtt_files -ne 0 ]; then
         mtt $translator_args $mtt_files
      fi
      ;;
   "Xwindow")
      if [ $got_gksm2ps_files -ne 0 ]; then
         gksm2ps -X $gksm2ps_files
      fi
      if [ $got_mtt_files -ne 0 ]; then
         mtt $mtt_files
      fi
      ;;
esac

