#!/bin/sh
# This is a2x
#
# Copyright (C) 1994 Christoph Beck, <beck@jack.rhein-main.de>
# All rights reserved.
#
# This software may be distributed under the terms of the GNU General
# Public License.
#
#  THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
#  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT
#  OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
#  NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# A2x uses a2x.ps to convert ascii text to different device formats.
#
# "Why another one", you ask?
#
# You may choose upon paper formats, orientations, fonts, their sizes,
# number of columns; you may print only odd/even pages, or just count
# the number of pages, the output would have; you may print the
# page's number, the filename and date on each page.
# Go into a2x.ps and change the defaults to your needs (carefully),
#
# However, some things are kept simple, as I wrote a2x.ps just to get
# characters dumped to my lineprinter, while not wasting too much paper.
# A line goes as it comes, clipped to it's column. Backspaces and tabs are 
# not handled.
#
# See the README file for details of usage.
#
# Any bugfixes, improvements, as well as comments are welcome.
# Email to beck@jack.rhein-main.de
#
# Enjoy!
#
#------------------- BEGIN USER DEFINITIONS ---------------------------
#
# Your printer (use 'gs -h' for a list of devices)
Device=deskjet
#
# one of a4, letter, legal
Paper=a4 
#
# Press RET to see next page for these devices:
InteractiveDevices="x11 linux vga"
#
#-------------------- END USER DEFINITIONS ----------------------------

files=
switches=
ShowpageHook="-dNOPAUSE"
Jstr=
#
while test -n "$1"
do
  case $1 in
    -[h?])
cat << EOF

------------------------------------------------+----------------------------
a2x: ascii --> something  (needs Ghostscript)   |   a2x version 0.3    9/3/94
(C) Christoph Beck, <beck@jack.rhein-main.de>   |         All rights reserved
------------------------------------------------+----------------------------

  usage: a2x [-dDEV] [-a4|-letter|-legal] -head[=name] [a2x-options] [files]

If no files are given, a2x reads from stdin. A2x always writes to stdout,
except for screen devices like x11. 

   -dDEV causes a2x (actually gs) to produce output for device DEV. 
        Default output device is $Device (change this in a2x, if you like. 
        Use gs -h for possible devices).
   -a4,-letter,-legal choose paper sizes. 
        The default paper size is $Paper (you may change this in a2x, too). 
   -head[=name] prints name (which defaults to the file's name) and current 
        date on top of each page.

All other options are recognized by a2x.ps. << Press any key to get them >>
EOF
        read key
        gs -dNODISPLAY -q -- a2x.ps
	exit;;
    -d[a-z0-9]*) Device=`expr $1 : '-d\(.*\)'`
	for i in $InteractiveDevices
	do
	  if test $Device = $i
	  then
	    ShowpageHook=""
	  fi
	done;;
    -a4|-letter|-legal) Paper=`expr $1 : '-\(.*\)'`;;
    -head=*) Jstr="--job=`expr $1 : '-head=\(.*\)'`" # to be used in header
        switches="$switches -head";;
    -*) switches="$switches $1";;
     *) if test -r $1
	then
          files="$files $1"
        fi;;
  esac
  shift
done

#
# read from stdin, if no files given.
if test -z $files 
then
   gs \
	-sDEVICE=$Device \
	-sPAPERSIZE=$Paper  \
     	-sOutputFile=-   \
        $ShowpageHook    \
     	-q               \
     	-- a2x.ps $switches \
	   --paper=$Paper"size" \
	   --date=`date +%D` \
	   $Jstr \
	   %stdin || exit
else
  for i in $files
  do
     if test -z $Jstr
     then
       Job="--job=$i"
     else
       Job=$Jstr
     fi

     gs \
	-sDEVICE=$Device \
	-sPAPERSIZE=$Paper  \
     	-sOutputFile=-   \
        $ShowpageHook    \
     	-q               \
     	-- a2x.ps $switches \
	   --paper=$Paper"size" \
	   --date=`date +%D` \
	   $Job \
	   $i || exit
  done
fi
#------------------------------------------------------------------------------