#! /bin/tcsh -f
#
# Usage:
#	codeprint2.csh outdir header infiles...
#
# Description:
#
# Generate PostScript output for code files.  The outdir argument in $1
# specifies the destination directory of the output file(s).  The header
# argument in $2 is the string put in the left part of the page header for each
# output page.  Specifically, if $2 = xxx, then the left part of the page
# header is "xxx".  The infiles in arguments $3 - $end are the source files to
# be printed.  The output files are named $x-2c.ps, foreach input file x.  When
# multi-file input is given, page numbers for each file start at 1.
#
# The output format is line numbered, two column, 7 point courier font.  This
# script uses the emacs batch script ../emacs/number-lines-batch.el and inserts
# the troff header from ./code/hdr2.me, q.q.v.
#
# Cf. ./codeprint1.csh, which prints one-columned, 9 pt.
#

if (($1 == "") || ($1 == "-h")) then
    echo "Usage:  codeprint2.csh outdir header infiles..."
    exit
endif

set subdir = $1
shift
set lhdr = $1
shift
foreach f ($*)

#   Uncomment any or all of the echo commands for debugging purposes.

#
# Number lines and untabify using emacs batch script.
#
#   echo "Processing $f, step 1"
    emacs -batch $f -l ~/projects/support/documentation/emacs/number-lines-batch.el \
	-f number-lines-untabify >& /tmp/$f.tmp1

#
# Cat the header onto the front of the file, saving the result in a tmp file.
#
#   echo "Processing $f, step 2"
    emacs -batch /tmp/$f.tmp1 -l ~/emacs/lib/remove-fink-vars-line.el >& /dev/null
    cat ~/projects/support/documentation/code/hdr2.me /tmp/$f.tmp1 > /tmp/$f.tmp2

#
# Do the deed with troff.
#
#   echo "Processing $f, step 3, outputting to $subdir/$f-2c.ps"
    troff -me -dl"$lhdr" -df"$f" /tmp/$f.tmp2 | grops -l > $subdir/$f-2c.ps

#
# Make a pdf
#
set d = `pwd`
cd $subdir
ps2pdf $f-2c.ps
cd $d

#
# Delete or save the troff source file; normally it should be deleted.
#
#   echo "Processing $f, step 4"
#   Exchange commenting of the next line two lines to save .me troff file for
#   debugging purposes.
#  rm /tmp/$f.tmp2
   mv /tmp/$f.tmp2 $subdir/$f.lno2.me

#
# Remove the tmp.
#   echo "Processing $f, step 5"
    rm /tmp/$f.tmp1
#    if (-l ".#$f") rm -f ".#$f"

end
