#!/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.
#
# See the manual page for details of installation and usage.
#
# Any bugfixes, improvements, as well as comments are welcome.
# Email to beck@jack.rhein-main.de
#
# Enjoy!
#
#------------------- BEGIN USER DEFINITIONS ---------------------------
#
# where did you copy a2x.ps and a2xshell.ps ?
A2X_PATH=/usr/lib/ghostscript
#A2X_PATH=.
#
# Your printer (use 'gs -h' for a list of devices) or ps for PostScript
#Device=deskjet
Device=ps
#
# one of a4, letter, legal
Paper=a4 
#
# Press RET to get next page for these devices:
InteractiveDevices="x11 linux"
#
#-------------------- END USER DEFINITIONS ----------------------------

files=""
Out="-"
A2Xoptions=""
ShowpageHook="-dNOPAUSE"
Papersize="$Paper"size
Jstr=""
#
while test -n "$1"
do
  case $1 in
    -[h?])
cat << EOHELP

------------------------------------------------+----------------------------
a2x: ascii --> something                        |   a2x version 0.9  11/19/94
(c) Christoph Beck, <beck@jack.rhein-main.de>   |         All rights reserved
------------------------------------------------+----------------------------

  usage: a2x [-dDEV]
 	     [-a4|-letter|-legal]
             [-title[=name]] [-date] [-num]
             [-p] [-l]
             [-cn]
             [-o|-e]
	     [-nm|-sn]
	     [-bs] [-tn]
	     [-man]
	     [-ffont]
	     [-ger|-us]             
	     [-pagecount]
             [files]

If no files are given, a2x reads from stdin. A2x always writes to stdout,
except for gs-devices like x11 (may be set in a2x).

	... hit <return> to get more ...
EOHELP
  read
cat << EOHELP
   -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). Use ps for PostScript device.
   -a4,-letter,-legal choose paper sizes.
            The default paper size is $Paper (you may change this in a2x, too).
   -title[=name] prints name (which defaults to the file's name)
            on top (left) of each page.
   -date causes the current date being printed on top (right) of each page.
   -p       print in portrait mode (default).
   -l       print in landscape mode.
   -man     print (preformatted) man page (try a2x -man -l ... pretty!).
   -cn      n columns per page (default 1-col-portrait, 2-col-landscape).
   -tn      set tabwidth to n (default 8).
   -bs      turn on backspace handling.
   -sn      select fontsize (default 10pt-portrait, 8pt-landscape).
   -nm      set lines per page to m.
   -e       don't print odd  pages.
   -o       don't print even pages.
   -num     print pagenumbers on bottom of each page (default off).
   -ffont   use font font (default is Courier).
   -ger,-us german (default), us font encoding.
   -pagecount just print number of pages to stdout (and nothing else).

EOHELP
	exit;;
    -date) A2Xoptions="$A2Xoptions --date=`date +%D`";;
    -d[a-z0-9]*) Device=`expr $1 : '-d\(.*\)'`
	for i in $InteractiveDevices
	do
	  if test $Device = $i; then Interactive=TRUE; fi
	done;;
    -fax) Device=dfaxhigh; A2Xoptions="-p -s12 $A2Xoptions"; Out=page%d.$$;;
    -a4|-letter|-legal) Paper=`expr $1 : '-\(.*\)'`size;;
    -title=*) Jstr="$Jstr`expr $1 : '-title=\(.*\)'`" # to be used in header
        A2Xoptions="$A2Xoptions -title";;
    -*) A2Xoptions="$A2Xoptions $1";;
     *) if test -r $1; then files="$files $1"; fi;;
  esac
  shift
done


A2Xoptions="--paper=$Papersize $A2Xoptions"

#
# (1) PostScript
#
if test $Device = "ps"
then
  A2Xargs=""; for j in $A2Xoptions; do A2Xargs="$A2Xargs ($j)"; done
  if test -z "$files"  # read from stdin, write to stdout
  then
    if test -z "$Jstr"; then Job=""; else Job="(--job=$Jstr)"; fi
    cat $A2X_PATH/a2x.ps
    echo "a2xdict begin [ (--eof=1) $A2Xargs $Job () ] A2x"
    cat - | sed "s///g"
    echo 
    echo "end"
  else                 # read from file(s)
    cat $A2X_PATH/a2x.ps
    echo "a2xdict begin"
    for i in $files
    do
      if test -z "$Jstr"; then Job="(--job=$i)"; else Job="(--job=$Jstr)"; fi
      echo  "[ (--eof=1) $A2Xargs $Job () ] A2x"
      cat $i | sed "s///g"
      echo 
    done
    echo "end"
  fi   
  exit
fi

#
# (2) GhostScript
#
GSoptions="-sDEVICE=$Device -sPAPERSIZE=$Paper -sOutputFile=$Out -q"
if test -z $Interactive; then GSoptions="$GSoptions -sNOPAUSE"; fi

if test -z "$files" # read from stdin, write to stdout
then
  if test -z "$Jstr"; then Job=""; else Job="(--job=$Jstr)"; fi
  gs $GSoptions $A2X_PATH/a2x.ps \
     -- $A2X_PATH/a2xshell.ps $A2Xoptions $Job %stdin
else		    # read from file(s)
  for i in $files
  do
    if test -z "$Jstr"; then Job="--job=$i"; else Job="--job=$Jstr"; fi
    gs $GSoptions $A2X_PATH/a2x.ps -- $A2X_PATH/a2xshell.ps $A2Xoptions $Job $i
  done
fi

#------------------------------------------------------------------------------
