#!/bin/sh
################ 
#
# Listing a listing printer (first version 1.0 : 06/08/96)
# This software permits the printing of postscript listing files on 
# non postscript printers, with combination of A2ps and Ghostscript
#
# Copyright (C) 1996 Sebastien FERNIER fernier@miaif.ibp.fr
#
# 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 of the License, 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, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# For more information, please have a look at the respective man pages 
# gs(1) and a2ps(1), and any of respective information files that refers
# to these program software
#
# The author whish to thanks and congratulate ;)
#	- Miguel Santana & for A2ps 
#	- Aladdin Enterprises for Ghostscript 
#	
#  (because without their talent, Listing wouldn't exists !)
#
#################
#
# Version changes : 
#	
#	- 1.1 : fixing some bugs
#	- 1.2 : adding some help and the -vgv option
#	- 1.3 : permits the reading from stdin (the output
#	        is then considered as a single file);
#		adding a user define section;	
#		all output files are created in the OUTPUT_DIR
#		directory
#	- 1.4 : switch for ps file generation only (for postscript
#		printers)
#
#
# TODO :
#
#	- adding the -nodate and -setgray<percentage> switches,
#	  that avoid the editing of the postscript file
#
#

VERSION=1.4
DATE="11/05/96"


############## Begin of the user define section #############



TARGET=listing	# name of the temp file and output file 
		# for the lpr command

OUTPUT_DIR="/tmp"  # by default, all output files are created there
		   # in case one wants to print a file located
		   # where no file can be created 


############### End of the user define section ##############



HEAD="Listing version $VERSION ($DATE)
Copyright (C) 1996 Sebastien FERNIER"

scheme="$HEAD
Scheme of work: (depending of the switches)

 
                  edit the listing.ps file
                             |              ___ create a listing.lpr 
                             |             /    file
read files --- create the listing.ps file /
                             |            \____ print directly
                             |    
                         display the  
                       listing.ps file          
"

custom="$HEAD
Here are some hints to help you customizing the postscript file generated 
by A2ps (not exhaustive, read the file for further modifications)

-- In a future version, I'll add some switches in order to treat 
that automatically 

  At the beginning of the file, in the 'Function header' function:

	- changing the value preceding the 'setgray' command which
	sets the grey tint of the header 
	- commenting the date-of-last-modification-of-the-source-file-being-
	printed, that is included in the header (not switchable with the 
	original A2ps command). Search for the line containing the 'd show' 
	command 

  Some places elsewhere:
	
	- the text contained by the header (by default it's the filename)

  ... and whatever you want, provided you don't crash the postscript file,
otherwise Ghostscript will tell you about it ;P
"

help="$HEAD
A simple listing printer, using A2ps and Ghostscript
Permits the printing of postscript listing files on non postscript printers
Usage: listing [switches] [[-f file1 file2 ...] [-]]
Files can be omitted but must be given with their extension

WARNING: this program is based on A2ps version 4.3 and Ghostscript 2.6.2
please refer to their own description files

Switches:
	
	-		read the file to be printed from stdin
        -a e1 e2 ...	print all files with extensions e1, e2 ...
	-c		compact mode for a sequence of files (see A2ps)
	-custom		give information about what to custom in the
			postscript output file of A2ps 
	-d <device>	set the device printer. Default: laserjet
			(see Ghostscript)
	-e		emacs editing of the postscript file generated 
			by A2ps (in order to customize it). Try
			listing -custom for more informations
	-f <files>	take files as input (even if the names begin with @ 
			but files beginning with - will make A2ps fail)
	-h		print this help information
	-header <text>	only when reading from stdin : use text in the page
			header (default text is 'stdin')
	-i		allow iso characters mode printing (8 bits)
	-m		set manual page output 
	-n		output lines are numbered
	-p		print automatically. If not set, creates a 
			listing.lpr/ps file
	-ps		generate postscript file only (postscript printers)
	-s		give the Listing scheme of work
	-vgs		display the postscript file with Ghostscript
	-vgv		idem as with -vgs but uses Ghostview

Any switch that is not set is considered as unset by default.
For more complete information, please read the 'listing' file.
"

######### Beginning of the script ###########
case $# in
0) echo "$help"; exit;;
1) case $1 in
     -h) echo "$help";exit;;
     -s) echo "$scheme";exit;;	
     -custom) echo "$custom";exit;;
   esac
esac

COMP='';
DEVICE='laserjet';
EDIT=false;
ERR=false;
ERRMSG='';
EXT='';
FILES='';
HEADER='stdin';
ISO='';
MAN='';
NUM='';
PRINT=false;
POSTSCRIPT=false;
STDIN=false;
SWI='';
VERIFY='';

PRFILE=$OUTPUT_DIR/$TARGET.lpr
PSFILE=$OUTPUT_DIR/$TARGET.ps
TEMP=$OUTPUT_DIR/$TARGET.tmp


##### Extraction of line command options ######

for i in $@; do
case $i in
-a) SWI='a';;
-c) COMP=$i;SWI='';;
-d) SWI='d';;
-e) EDIT=true;SWI='';;
-f) SWI='f';;
-header) HEADER='';SWI='h';;
-i) ISO="-8";SWI='';;
-m) MAN=$i;SWI='';;
-n) NUM=$i;SWI='';;
-p) PRINT=true;SWI='';;
-ps) POSTSCRIPT=true;;
-vgs) VERIFY=$i;SWI='';;
-vgv) VERIFY=$i;SWI='';;
-) STDIN=true;cat > $TEMP;IFS='\t';   
   while read stdin; do
     echo $stdin >> $TEMP;
   done;IFS=' ';;
*)  if [ -z "$SWI" ]; then
      echo "Unknown switch $i";
      exit;
    else
      case $SWI in 
	a) EXT="$EXT *.$i";;
	d) DEVICE="$i";SWI='';;
	f) FILES="$FILES $i";; 
	h) HEADER="$HEADER $i";;
	*) echo "$help";exit;;
      esac
    fi
esac
done


############## Parameters found ################

function display_value() {
        if [ -z $1 ]; then
          echo "$2: no";
	else
          echo "$2: yes";
        fi
}

function display_true() {
        if [ $1 = true ]; then
          echo "$2: yes";
	else
          echo "$2: no";
        fi
}

echo
echo "### Parameters ###"

display_value "$COMP" "Compact mode"
echo "Device: $DEVICE"
display_true "$EDIT" "Edition"

if ! [ -z "$EXT" ]; then
   FILES="$FILES `ls $EXT 2>/dev/null`";
   if ! [ $? -eq 0 ]; then
	ERRMSG="some problems with file extensions"
	ERR=true;
   fi
fi

echo "Files: $FILES"
display_value "$ISO"    "Iso character mode (8bits)"
display_value "$MAN"    "Manual page output"
display_value "$NUM"    "Numbering"
display_true  "$PRINT"  "Printing"
display_true  "$POSTSCRIPT"  "Postscript output"
display_true  "$STDIN"  "Reading from stdin"

if [ $STDIN = true ]; then
  echo "Header text : $HEADER";
fi

display_value  "$VERIFY" "Verify"

############## Check for errors ################

echo
if [ -z "$FILES" ] && [ $STDIN = false ]; then
	echo "no files to print out !";
	ERR=true;
else
  for i in $FILES; do
    if ! [ -e $i ];then
	echo "file $i doesn't exist";
	ERR=true;
    else
      if ! [ -s $i ];then
	echo "file $i has zero length";
	ERR=true;
      fi
    fi 
  done
fi

if [ $STDIN = true ] && ( ! [ -z "$FILES" ] || ! [ -z "$EXT" ]); then
    echo "can't combine stdin reading with files, sorry...";
    ERR=true;
fi

if [ $ERR = true ]; then
	if ! [ -z "ERRMSG" ]; then
	  echo $ERRMSG;
	fi
	echo "there was errors, check again";
	exit;
fi

############# Keep going !! ###################

# let the place clean in case of abrupt exit :

trap 'rm -f $PRFILE $PSFILE $PSFILE~ $TEMP; exit' 1 2 3 

echo "$HEAD
Listing comes with NO WARRANTY: see the file COPYING for details."


########### Creation of the postscript file with A2ps ############

echo
echo "Creating the $TARGET.ps file"

if [ $STDIN = true ]; then
  a2ps -q -nL -nu -H"$HEADER" $COMP $ISO $MAN $NUM $TEMP > $PSFILE;
  rm -f $TEMP;
else
  a2ps -q -nL -nu $COMP $ISO $MAN $NUM $FILES > $PSFILE
  if [ $? -ne 0 ]; then
    echo
    echo "Oooopss, A2ps failed, check if filenames are correct...";
    exit;
  fi
fi


########### Edition of the postscript file for custom ############

if [ $EDIT = true ]; then
  echo "Editing the $TARGET.ps file...";
  emacs $PSFILE;
  rm -f $PSFILE~;
fi


############### Display of the postscript file ################


if ! [ -z "$VERIFY" ]; then
  echo "Verification of the $TARGET.ps file";

  if [ $VERIFY = "-vgv" ]; then
    ghostview -a4 -landscape $PSFILE;
  else
    gs -q -sPAPERSIZE=a4 -sDEVICE=x11 $PSFILE;
  fi
fi


################## Ghostscript section ######################

if [ $PRINT = false ]; then
  if [ $POSTSCRIPT = false ]; then
    echo "Creating the $TARGET.lpr file; full name : $PRFILE";
    echo;
    gs -q -dNOPAUSE -sPAPERSIZE=a4 -sDEVICE=$DEVICE -sOutputFile=$PRFILE $PSFILE &
   wait
   echo "Deleting the $TARGET.ps file"
   rm -f $PSFILE
  fi
else
  echo "Printing the file";
  if [ $POSTSCRIPT = false ]; then
    gs -q -dNOPAUSE -sPAPERSIZE=a4 -sDEVICE=$DEVICE -sOutputFile=- - < $PSFILE | lpr &
  else
    lpr $PSFILE &
  fi  
  wait
  echo "Deleting the $TARGET.ps file"
  rm -f $PSFILE
fi

#wait
#echo "Deleting the $TARGET.ps file"
#rm -f $PSFILE

####### Z-end ! ##### 


