:
#	@(#) text2post 23.3 91/11/29 
#
#	Copyright (C) 1989-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
# Description	Convert files from ascii to PostScript.  
# Usage		reads stdin, out to stdout
# Options	-port: prints in portrait mode
# Options	-land: prints in landscape 1 page  mode
# Options	-land2: prints in landscape 2 page  mode
#
# MODE can be	land2 for 2 page landscape mode
#               port for portrait mode
#               land for 1 page landscape mode

IPFILTER=/usr/spool/lp/bin/escps

port_head()
{
cat << !!END!!OF!!PS!!PROLOGUE!!
36.0000 36.0000 translate
/width 540.0000 def
/height 720.0000 def
/deltaY 10.8000 def
/margin 36.0000 def
/indent 36.0000 def
/charWidth 5.4000 def
/Home { /y height deltaY sub def 0 y moveto } def
/Down { /y y deltaY sub def 0 y moveto } def
/Line { show Down } def
/Page { showpage restore save Home } def
/ISO8859 findfont 9 scalefont setfont
save Home
%%EndProlog
!!END!!OF!!PS!!PROLOGUE!!
}

# expand tabs to spaces
expand() 
{
   awk 'BEGIN			{ spaces="          "; }
	/\t/ 			{ 
		ntabs = split($0,tabs,"\t");
		pos   = 0;
		for (i = 1; i < ntabs; i++) {
			pos += length (tabs[i]);
			need = 8 - (pos % 8);
			pos += need;
			printf ("%s%s", tabs[i], substr(spaces, 1, need));
		}
		printf ("%s\n", tabs[ntabs]);
		next;
				}
				{ print $0 }'
}

# Define current date and time and start document

# Process standard input
port_proc()
{
#cat - | sed 's/	/        /g' | sed 's/[\\()]/\\&/g' \
expand | sed 's/[\\()]/\\&/g' | $IPFILTER \
    | awk 'BEGIN { line=0 }
	(line>65)	{ line=0; print "Page" }
	$0~/^/		{ print "Page"; line=0 ; next }
				{ print "(" $0 ") Line"; line++ }
	END			{print "%%Trailer"; print "showpage restore"}'
echo "restore"
}

land_head()
{
cat << !!END!!OF!!PS!!PROLOGUE!!
90 rotate 0 -612.0000 translate
36.0000 36.0000 translate
/width 720.0000 def
/height 540.0000 def
/deltaY 12.0000 def
/margin 36.0000 def
/indent 36.0000 def
/charWidth 6.0000 def
/Home { /y height deltaY sub def 0 y moveto } def
/Down { /y y deltaY sub def 0 y moveto } def
/Line { show Down } def
/Page { showpage restore save Home } def
/ISO8859 findfont 10 scalefont setfont
save Home
%%EndProlog
!!END!!OF!!PS!!PROLOGUE!!
}

# Define current date and time and start document

# Process standard input
land_proc()
{
#cat - | sed 's/	/        /g' | sed 's/[\\()]/\\&/g' \
expand | sed 's/[\\()]/\\&/g' | $IPFILTER \
    | awk 'BEGIN { line=0 }
	(line>42)		{ line=0; print "Page" }
	$0~/^/		{ print "Page"; line=0 ; next }
				{ print "(" $0 ") Line"; line++ }
	END			{print "%%Trailer"; print "showpage restore"}'
echo "restore"
}

land2_head()
{
cat << !!END!!OF!!PS!!PROLOGUE!!
/xdef {exch def} bind def
/inch {72 mul} bind def
/getfont {exch findfont exch scalefont} bind def

/filenamesize 12 def
/filenamefont /Helvetica-Bold filenamesize getfont def
/linesperpage 66 def

/datesize filenamesize 2 sub def
/datefont /Helvetica datesize getfont def
/headersize filenamesize 4 add def
/bodysize 6.8 def
/bodyfont /ISO8859 bodysize getfont def
/sidemargin 4 def
/topmargin 4 def
/pagewidth 
   bodyfont setfont (0) stringwidth pop 80 mul sidemargin dup add add
   def
/pageheight
   bodysize linesperpage mul topmargin dup add add headersize add
   def
/uppery 8.5 inch pageheight add 2 div def
/upperx [ 11 inch pagewidth 2 mul sub 3 div
          dup 2 mul pagewidth add ] def

/pnumbuf 10 string def

/endpage
   { pageside 1 eq
       { /pageside 0 def
	 numberpage
         copypage erasepage 
	 /sheet sheet 1 add def }
       { /pageside 1 def }
     ifelse
     /pagenum pagenum 1 add def
    } bind def
/numberpage
    { 11 inch upperx 0 get sub sidemargin add
      8.5 inch uppery sub headersize sub moveto
      datefont setfont
      sheet pnumbuf cvs show
    } bind def
/newfile
    { /filename xdef
      /pagenum 1 def 
    } bind def
/cleanup
    { pageside 1 eq
      {numberpage showpage} if
    } bind def
/startdoc
    { 8.5 inch 0 inch translate
      90 rotate 
      /pageside 0 def
      /sheet 1 def
    } bind def
/startpage
    { printheader
      printborder
      upperx pageside get sidemargin add
      uppery topmargin sub bodysize sub headersize sub moveto
      bodyfont setfont
    } bind def
/s  { gsave
        show
      grestore
      0 bodysize neg rmoveto
    } bind def
/printheader
    { upperx pageside get uppery headersize sub 1 add moveto
      datefont setfont
      gsave
        sidemargin 2 rmoveto date show
      grestore
      gsave
	/pnum pagenum pnumbuf cvs def
        pagewidth sidemargin sub pnum stringwidth pop sub
        (Page ) stringwidth pop sub 3 rmoveto
        (Page ) show pnum show
      grestore
      gsave
        filenamefont setfont
        pagewidth filename stringwidth pop sub 2 div 2 rmoveto
        filename show
      grestore
    } bind def
/printborder 
    { upperx pageside get uppery moveto
      gsave
        pagewidth 0 rlineto
        0 pageheight neg rlineto
        pagewidth neg 0 rlineto
        closepath stroke
      grestore
      0 headersize neg rmoveto pagewidth 0 rlineto stroke
    } bind def
%%EndProlog
!!END!!OF!!PS!!PROLOGUE!!
}

land2_proc()
{
# Define current date and time and start document
date +"/date (%d %h 19%y  %r) def"
echo "startdoc"

# Process standard input
echo "( ) newfile"
#cat - | sed 's/	/        /g' | sed 's/[\\()]/\\&/g' \
cat - | sed 's/	/        /g' | sed 's/[\\()]/\\&/g' | $IPFILTER \
    | awk 'BEGIN		{ line=0; print "startpage" }
	(line>65 || $0~/^/)	{ line=0; print "endpage startpage" }
	$0~/^ *$/		{ next }
				{ print "(" $0 ") s"; line++ }
	END			{ print "%%Trailer"; print "endpage"}'
echo "cleanup"
echo "restore"
}

#main

cat << !!END!!OF!!PS!!PROLOGUE!!
%!PS-Adobe-
save
!!END!!OF!!PS!!PROLOGUE!!

#download ISO8859 character set font
cat /usr/spool/lp/bin/iso8859.ps

#process MODE
if [ $MODE ];then
	case "$MODE" in
	land)	land_head
		land_proc
		;;
	land2)	land2_head
		land2_proc
		;;
	port)	port_head
		port_proc
		;;
	esac
else
	port_head
	port_proc
fi

exit 0
