#!/bin/bash

if [ ! $# -eq 2 ]; then
 echo "Usage: $0 directory destinationfile"
 exit 0
fi

if [ ! -d "$1" ]; then
 echo "Usage: $0 directory destinationfile"
 exit 0
fi

echo -e "\nPlease type the title for the listing:"
read title
echo -e "\nPlease give the value for the line spacing (default is 60):"
read spacing

if [ -z $spacing ] || [ $spacing -eq 0 ] || [ $spacing = "" ]; then
  spacing=60
fi

cd "$1"
cat << EOF > "$2"
%!PS-Adobe-3.0
%%BoundingBox: 0 0 595 842
%%LanguageLevel: 2
%%DocumentData: Clean7Bit
%%Pages: (atend)
%%PageOrder: Ascend
%%EndComments
%%BeginProlog
/ISO1252Encoding [
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/space /exclam /quotedbl /numbersign /dollar /percent /ampersand /quotesingle
/parenleft /parenright /asterisk /plus /comma /hyphen /period /slash
/zero /one /two /three /four /five /six /seven
/eight /nine /colon /semicolon /less /equal /greater /question
/at /A /B /C /D /E /F /G
/H /I /J /K /L /M /N /O
/P /Q /R /S /T /U /V /W
/X /Y /Z /bracketleft /backslash /bracketright /asciicircum /underscore
/grave /a /b /c /d /e /f /g
/h /i /j /k /l /m /n /o
/p /q /r /s /t /u /v /w
/x /y /z /braceleft /bar /braceright /asciitilde /unused
/Euro /unused /quotesinglbase /florin /quotedblbase /ellipsis /dagger /daggerdbl
/circumflex /perthousand /Scaron /guilsinglleft /OE /unused /Zcaron /unused
/unused /quoteleft /quoteright /quotedblleft /quotedblright /bullet /endash /emdash
/tilde /trademark /scaron /guilsinglright /oe /unused /zcaron /Ydieresis
/space /exclamdown /cent /sterling /currency /yen /brokenbar /section
/dieresis /copyright /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron
/degree /plusminus /twosuperior /threesuperior /acute /mu /paragraph /periodcentered
/cedilla /onesuperior /ordmasculine /guillemotright /onequarter /onehalf /threequarters /questiondown
/Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla
/Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis
/Eth /Ntilde /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply
/Oslash /Ugrave /Uacute /Ucircumflex /Udieresis /Yacute /Thorn /germandbls
/agrave /aacute /acircumflex /atilde /adieresis /aring /ae /ccedilla
/egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis
/eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide
/oslash /ugrave /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis] def

/psp_definefont { exch dup findfont dup length dict begin { 1 index /FID ne
{ def } { pop pop } ifelse } forall /Encoding 3 -1 roll def
currentdict end exch pop definefont pop } def

/pathdict dup 8 dict def load begin
/rcmd { { currentfile 1 string readstring pop 0 get dup 32 gt { exit }
{ pop } ifelse } loop dup 126 eq { pop exit } if 65 sub dup 16#3 and 1
add exch dup 16#C and -2 bitshift 16#3 and 1 add exch 16#10 and 16#10
eq 3 1 roll exch } def
/rhex { dup 1 sub exch currentfile exch string readhexstring pop dup 0
get dup 16#80 and 16#80 eq dup 3 1 roll { 16#7f and } if 2 index 0 3
-1 roll put 3 1 roll 0 0 1 5 -1 roll { 2 index exch get add 256 mul }
for 256 div exch pop exch { neg } if } def
/xcmd { rcmd exch rhex exch rhex exch 5 -1 roll add exch 4 -1 roll add
1 index 1 index 5 -1 roll { moveto } { lineto } ifelse } def end
/readpath { 0 0 pathdict begin { xcmd } loop end pop pop } def

systemdict /languagelevel known not {
/xshow { exch dup length 0 1 3 -1 roll 1 sub { dup 3 index exch get
exch 2 index exch get 1 string dup 0 4 -1 roll put currentpoint 3 -1
roll show moveto 0 rmoveto } for pop pop } def
/rectangle { 4 -2 roll moveto 1 index 0 rlineto 0 exch rlineto neg 0
rlineto closepath } def
/rectfill { rectangle fill } def
/rectstroke { rectangle stroke } def } if

/psp_lzwfilter { currentfile /ASCII85Decode filter /LZWDecode filter } def
/psp_ascii85filter { currentfile /ASCII85Decode filter } def
/psp_lzwstring { psp_lzwfilter 1024 string readstring } def
/psp_ascii85string { psp_ascii85filter 1024 string readstring } def
/psp_imagedict {
/psp_bitspercomponent { 3 eq { 1 }{ 8 } ifelse } def
/psp_decodearray { [ [0 1 0 1 0 1] [0 255] [0 1] [0 255] ] exch get }
def 7 dict dup
/ImageType 1 put dup
/Width 7 -1 roll put dup
/Height 5 index put dup
/BitsPerComponent 4 index psp_bitspercomponent put dup
/Decode 5 -1 roll psp_decodearray put dup
/ImageMatrix [1 0 0 1 0 0] dup 5 8 -1 roll put put dup
/DataSource 4 -1 roll 1 eq { psp_lzwfilter } { psp_ascii85filter } ifelse put
} def
%%EndProlog
%%Page: 0 0
%%PageBoundingBox: 18 18 577 824
%%BeginSetup
%
%%BeginFeature: *PageSize A4
<</PageSize [595 842] /ImagingBBox null>> setpagedevice
%%EndFeature
%%EndSetup
%%BeginPageSetup
%
gsave
[0.24 0 0 -0.24 18 824] concat
gsave
%%EndPageSetup
/Times-Roman-iso1252 /Times-Roman ISO1252Encoding psp_definefont
500 300 moveto
0 0 0 setrgbcolor
/Times-Roman-iso1252  findfont 50 -90 matrix scale makefont setfont (
EOF

echo "$title" >> "$2"

cat << EOF >> "$2"
)
show

/Times-Roman-iso1252 /Times-Roman ISO1252Encoding psp_definefont
500 380 moveto
0 0 0 setrgbcolor
/Times-Roman-iso1252  findfont 50 -50 matrix scale makefont setfont (
EOF

IFS=$'\n';
y=380
for i in `ls -1`; do
let y=y+${spacing};
echo "${i%.*})show 500 $y moveto (" >> "$2"
done

cat << EOF >> "$2"
) show

457 185 1 1418 rectfill
1873 185 1 1418 rectfill
457 185 1417 1 rectfill
457 1602 1417 1 rectfill
showpage
grestore grestore
%%PageTrailer

%%Trailer
%%Pages: 1
%%EOF
EOF

