#! /bin/sh
# pstoepson --- convert PostScript images to Epson dot matrix

# Copyright (C) 1996 Noah S. Friedman <friedman@prep.ai.mit.edu>

# $Id: pstoepson,v 2.2 1996/06/26 17:27:36 friedman Exp $

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.

# Commentary:

# This program requires Ghostscript with epson drivers installed.

# Code:

# This string is expanded by RCS automatically when this file is checked out.
rcs_revision='$Revision: 2.2 $'
revision=`set fnord $rcs_revision; echo $3`

# Name by which this script was invoked.
progname=`echo "$0" | sed -e 's/[^\/]*\///g'`

GS="${GS-gs}"

resolutions='240x72 60x72'
devices='epson eps9high'

resolution=240x72
device=epson

# To prevent hairy quoting and escaping later.
bq='`'
eq="'"

usage="Usage: $progname {options} file1.ps {file2.ps {...}}
(Version $revision)

Options are:
-D, --debug                  Turn on shell debugging ($bq${bq}set -x$eq$eq).
                             Implies --verbose.
-d, --device                 Select output device; one of: $devices.
                             Default is $device.
-f, --force                  Overwrite existing output files without
                             notification.  By default, this program
                             refuses to overwrite any existing files.
-h, --help                   You're looking at it.
-o, --output-file     FILE   Use FILE as the output file name.
                             This is only appropriate when used with a
                             single input file to convert.
-q, --quiet                  Do not report progress during conversion.
-r, --resolution      RES    Dot matrix resolution; one of: $resolutions.
                             Default is $resolution.
-V, --version                Show version number of this program & exit.
-v, --verbose                Report progress.  This is the default.
"

# Usage: eval "$getopt"; value=$optarg
# or     optarg_optional=t; eval "$getopt"; value=$optarg
#
# This function automatically shifts the positional args as appropriate.
# The argument to an option is optional if the variable `optarg_optional'
# is non-empty.  Otherwise, the argument is required and getopt will cause
# the program to exit on an error.  optarg_optional is reset to be empty
# after every call to getopt.  The argument (if any) is stored in the
# variable `optarg'.
#
# Long option syntax is `--foo=bar' or `--foo bar'.  2nd argument
# won't get used if first long option syntax was used.
#
# Note: because of broken bourne shells, using --foo=bar syntax can
# actually screw the quoting of args that end with trailing newlines.
# Specifically, most shells strip trailing newlines from substituted
# output, regardless of quoting.
getopt='
  {
    optarg=
    case "$1" in
      --*=* )
        optarg=`echo "$1" | sed -e "1s/^[^=]*=//"`
        shift
       ;;
      * )
        case ${2+set} in
          set )
            optarg="$2"
            shift
            shift
           ;;
          * )
            case "$optarg_optional" in
              "" )
                case "$1" in
                  --*=* ) option=`echo "$1" | sed -e "1s/=.*//;q"` ;;
                  * ) option="$1" ;;
                esac
                exec 1>&2
                echo "$progname: option $bq$option$eq requires argument."
                echo "$progname: use $bq--help$eq to list option syntax."
                exit 1
               ;;
           esac
           shift
         ;;
        esac
     ;;
    esac
    optarg_optional=
  }'

# Initialize variables.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
debug=
force=
verbose=t
gs_quiet=
outfilename=

# Parse command line arguments.
# Make sure that all wildcarded options are long enough to be unambiguous.
# It's a good idea to document the full long option name in each case.
# Long options which take arguments will need a `*' appended to the
# canonical name to match the value appended after the `=' character.
while : ; do
  case $# in 0) break ;; esac
  case "$1" in
    -D | --debug | --deb* )
      debug=t
      verbose=t
      gs_quiet=
      shift
     ;;
    -d | --device* | --dev* )
      eval "$getopt"
      resolution="$optarg"
     ;;
    -f | --force | --f* )
      force=t
      shift
     ;;
    -h | --help | --h* )
      echo "$usage" 1>&2
      exit 0
     ;;
    -o | --output-file* | --o* )
      eval "$getopt"
      outfilename="$optarg"
     ;;
    -q | --quiet | --q* )
      verbose=
      gs_quiet=-q
      shift
     ;;
    -r | --resolution* | --re* )
      eval "$getopt"
      resolution="$optarg"
     ;;
    -V | --version | --vers* )
      echo "$progname version $revision"
      exit 0
     ;;
    -v | --verbose | --verb* )
      verbose=t
      gs_quiet=
      shift
     ;;
    -- )     # Stop option processing
      shift
      break
     ;;
    -? | --* )
      case "$1" in
        --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
        * )     arg="$1" ;;
      esac
      exec 1>&2
      echo "$progname: unknown or ambiguous option $bq$arg$eq"
      echo "$progname: Use $bq--help$eq for a list of options."
      exit 1
     ;;
    -??* )
      # Split grouped single options into separate args and try again
      optarg="$1"
      shift
      set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"}
      shift
     ;;
    * )
      break
     ;;
  esac
done

case "$debug" in t ) set -x ;; esac
case "$verbose" in '' ) exec 1> /dev/null ;; esac

deverr=t
for dev in $devices; do
  case "$device" in
    $dev ) deverr= ; break ;;
  esac
done
case "$deverr" in t)
  echo "$progname: $device: Unknown device." 1>&2
  exit 1
esac

reserr=t
for res in $resolutions; do
  case "$resolution" in
    $res ) reserr= ; break ;;
  esac
done
case "$reserr" in t)
  echo "$progname: $resolution: Unknown resolution." 1>&2
  exit 1
esac

file_exists_hint_given_p=

for file in ${1+"$@"}; do
  outfile=
  case "$file" in
    - ) : ;;
    * )
      case "$outfilename" in
        '' )
          basename=`echo "$file" \
                     | sed -e 's/\/*$//
                               s/.*\///
                               s/\..*//'`
          outfile="${basename}.$device"
         ;;
        * ) outfile="$outfilename" ;;
      esac
     ;;
  esac

  case "$force:$outfile" in
    t:* ) : ;;
    :?* )
      set fnord `ls -d -- "$outfile" 2> /dev/null`
      shift
      case $# in
        0 ) : ;;
        * )
          echo "$progname: skipping conversion of $bq$file$eq" 1>&2

          case "$file_exists_hint_given_p" in
            t ) : ;;
            * )
              file_exists_hint_given_p=t
              exec 3>&1 1>&2
              echo "$progname: The file $bq$outfile$eq would be overwritten."
              echo "$progname: Rename that file or use $bq--force$eq."
              exec 1>&3 3>&-
             ;;
          esac
          continue
         ;;
      esac
     ;;
  esac

  gsflags="$gs_quiet
           -dNOPAUSE
           -dSAFER
           -sDEVICE="$device"
           -r$resolution"

  case "$verbose" in
    t )
      echo "$progname: converting $file ..." 1>&2
      case "$file" in
        - ) s="-q -sOutputFile=- -" ;;
        * ) s="-sOutputFile=$outfile $file -" ;;
      esac
      echo ${PS4-+} $GS $gsflags "$s" 1>&2
     ;;
  esac

  case "$file" in
    - ) { cat; echo quit; } | $GS $gsflags -q -sOutputFile=- - ;;
    * ) { echo quit; }      | $GS $gsflags -sOutputFile="$outfile" "$file" - ;;
  esac
done

# pstoepson ends here
