]> git.donarmstrong.com Git - lilypond.git/blob - scripts/ly2dvi.sh
release: 1.0.1
[lilypond.git] / scripts / ly2dvi.sh
1 #!/bin/sh
2 #
3 # Script to make a LaTeX file for Lilypond
4 #
5 # Written by Jan Arne Fagertun <Jan.A.Fagertun@energy.sintef.no>
6 #  Sat Nov 22 22:26:43 CET 1997
7 #
8 # $Id: ly2dvi.sh,v 1.16 1998/05/08 21:18:34 janaf Exp $
9 #
10 #  Original LaTeX file made by Mats Bengtsson, 17/8 1997
11 #
12
13 VERSION="0.12"
14 NAME=ly2dvi.sh
15 IDENTIFICATION="$NAME $VERSION" 
16 NOW=`date`
17 echo "$IDENTIFICATION" 1>&2
18
19 # TODO:
20 #  prevent orphaned "Lily is here" strings
21
22 # NEWS
23
24 # 0.12
25 #  - -S/--sourcedir switch
26 #
27
28 #
29 #0.11.jcn3
30 # - mudelaDefs fixes, (thanks PC)
31 #0.11.jcn2
32 # - pass -I, --include to Lily
33 # - bf: split $LILYINCLUDE and try all dirs
34 # - bf: geometry left/top
35 #
36 #0.11.pc
37 # - fix backslash gobbling, reduce number of separate processes used
38 # - require readable rc files, allow rc file in /usr/local/share/lilypond/lilyrc
39 # - use latex path, not tex path
40 # - Don't override if kpsepath returns non-NULL
41 # - Don't clobber x.tex if it wasn't created by ly2dvi
42 #
43 #
44 #0.11.jcn1
45 # - find .fly files too
46 #
47 #0.11.hwn1
48 # - height vs heigth
49 # - robustification: give \nonstopmode as LaTeX arg; no hanging if no TeX file.
50 # - robustification: notice failed cp.
51
52 #0.11
53 #       - more papersizes (thanks Han-Wen) - don't expect LaTeX to accept all...
54 #       - -W,--Width=       : set paper width  (points)
55 #       - -H,--Height=      : set paper height (points)
56 #       - -H and -W is used only when an unknown papersize is to be specified,
57 #         and has to be combined with a papersize option known to LaTeX
58 #NB!    - -F,--headers=     : name of additional LaTeX headers input file.
59 #NB!      Changed from -H to -F
60 #       - -d,--dependencies : passed to lilypond
61
62
63 #0.10.jcn1
64 #       - HEIGHT -> HEIGHT
65 #       - vertical margins (for a4), same on both sides
66 #       - textheight from lilypond output file (mudelapapertextheight)
67 #       - piece titling
68 #       - mudelapiece, mudelaopus
69 #
70 #0.10
71 #       - -K,--keeplilypond : Keep lilypond output files (default delete)
72 #       - -k,--keeply2dvi   : Keep ly2dvi   output files (default delete)
73 #       - -L,--landscape    : Set landscape orientation
74 #       - -N,--nonumber     : Turn off page numbering (\pagestyle{empty})
75 #       - Could not reinsert "\usepackage[T1]{fontenc}" because
76 #         "los-toros" won't work with it
77 #       - Ekstra LaTeX headers from input file
78 #0.9.hwn1
79 #       - option to remove output of lily
80 # 0.9
81 #       - Trap Lilypond abort
82 #       - Replaced "\usepackage[T1]{fontenc}" with
83 #         \usepackage[latin1]{inputenc} (takk, Mats)
84 #       - Removed "()" around "\LilyIdString" (Janne didn't want it)
85 # 0.8   - Trap Lilypond segmentation fault
86 #       - Function for cleanup
87 #       - Trap line
88 #       - More human-readable variables
89 #       - Some logics concerning rc-files
90 # 0.7
91 #       - Improved Lilypond error checking
92 #       - Output orientation (landscape...). Overrides mudela file
93 #         variable orientation="landscape";
94 #       - Paper width and height put into variables (only A4!)
95 #       - Adjusted top margin to default.....
96 #
97 #TODO
98 #       - Include more papersizes.
99 #       - Manual page.......
100 # 0.6.jaf2
101 #       - LILYINCLUDE update
102 #
103 # 0.6.hwn1
104 #       - handle LILYINCLUDE
105 #       - --output
106 #
107 # 0.6
108 #       - Source rc-files, if present. Files are:
109 #         /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
110 #       - tee output from Lilypond
111 #       - Handles margins for A4 paper (equal on both sides)
112 #       - new option -s (--separate) for one LaTeX run per file,
113 #         else all files are run together
114 #
115 # 0.5
116 #       - More useful ("two-level") debug.
117 #       - The Q&D hack to find file names and not handling \include
118 #         is replaced by grabbing output file names from Lilypond.
119 #       = Detects multiple output files - adds them when running
120 #         LaTeX.
121 #       - Works with multiple input files - no matter if they are
122 #         (a mix of) input to or output from Lilypond.
123 #
124 #TODO
125 #       - Still no margins handling.
126 #       - We have to discuss how to handle multiple output files
127 #         from Lilypond - 'ly2dvi standchen' gives a rather odd
128 #         result....
129
130 # 0.4.1
131 #       - Always exit after printing help info
132 # 0.4
133 #       - Changes to ensure for more strict grep'ing of parameters
134 #         Thanks to from G.B.Stott@bolton.ac.uk
135 #       - More efficient use of sed -e 's///' -e 's///'
136 #         Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
137 #       - Ask tex for location of titledefs.tex (Thanks to JV)
138 #       - Accept only exact match of "\def\mudelacomposer{"
139 #         (or whatever mudela* defined in titledefs.tex)
140 #       - Even more efficient use of sed (Thanks to JV)
141 #       - Default file name for single output file implemented.
142 #       - Moved help into function - finally included from 0.1.jcn1
143 #
144 #TODO
145 #       - Still doesn't handle \include
146 #       - The Q&D for finding output file name from the sequence of
147 #         \paper \output \midi \output really needs to be looked at.
148 #         I have improved it a lot, but it's only capable of finding
149 #         one (the last) file name.
150 #         Well, I have to rewrite this entirely to handle \include,
151 #         then I can fix it.
152 #       - Still no margins handling.
153 #
154 #WARNING
155 #       - Some lines of output from lilypond do NOT start
156 #         at first character position, therefore I have removed "^"
157 #         in sed'ing and grep'ing.
158
159 # 0.3.hwn1
160 #       - add "Creator: " line to output
161 #
162 # 0.3
163 #       - multiple input files to make score from several files
164 #         (extra files assumed to be Lilypond output)
165 #       - cp dvi-file instead of mv, to allow for xdvi reload
166 #       - check for illegal long options
167 #       - put in pt in text width, if not given
168 #       - put in \nonstopmode in LaTeX file
169 #       - restored LaTeX exit value check
170  
171 # 0.2.1
172 #       - temporarily omit LaTeX exit value check
173 #       - remove ALL temporary files
174
175 # 0.2
176 #       - fix for solaris          - included from 0.1.jcn1
177 #       - long option hack         - included from 0.1.jcn1 - modified
178 #       - moved help into function - NOT included from 0.1.jcn1 yet
179 #       - various improvements     - included from 0.1.jcn1
180 #       - find mudela definitions from titledefs.tex
181 #       - find papersize from lilypond output file (mudelapapersize),
182 #         overridden by option '-p size' or '--papersize=size'
183 #       - option -l lang or --language=lang overrides
184 #         lilypond output file definition (mudelalanguage)
185 #       - textwidth from lilypond output file (mudelapaperlinewidth)
186
187 # 0.1.jcn1
188 #       - fix for solaris
189 #       - long option hack
190 #       - moved help into function
191
192 #
193 # Clean up
194 #
195 cleanup() {
196   if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
197   then
198     [ -n "$LatF" -a -f "$LatF" ]       && rm -f $LatF
199     [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
200     [ -n "$FN" ]                       && rm -f $FN.*
201     for F in *$$* $TMP/*$$*
202     do
203       rm -f $F
204     done
205   fi
206   if [ "$KEEP_LILY_OUTPUT" != "Y" ]
207   then
208     for F in $LILY_OUTPUT_FILES
209     do
210       [ -f $F ] && rm -f $F
211     done
212   fi
213 }
214
215 #
216 # print usage
217 #
218 help() {
219   cat << EOF
220 Generate dvi file from mudela or lilypond output
221 Usage: $0 [OPTION]... [FILE]...
222
223 Options:
224   -D,--debug           increase verbosity
225   -F,--headers=        name of additional LaTeX headers file
226   -H,--Height=         set paper height (points) (see manual page)
227   -I,--include=DIR     add DIR to search path of LilyPond
228   -K,--keeplilypond    keep lilypond output files
229   -L,--landscape       set landscape orientation
230   -N,--nonumber        switch off page numbering
231   -O,--orientation=    set orientation (obsolete - use -L instead) 
232   -W,--Width=          set paper width (points) (see manual page)
233   -d,--dependencies    tell lilypond make a dependencies file
234   -h,--help            this help text
235   -k,--keeply2dvi      keep ly2dvi output files
236   -l,--language=       give LaTeX language (babel)
237   -o,--output=         set output directory
238   -p,--papersize=      give LaTeX papersize (eg. a4)
239   -s,--separate        run all files separately through LaTeX
240   -S,--sourcedir       set sourcedir 
241
242   files may be (a mix of) input to or output from lilypond(1)
243 EOF
244 }
245
246
247 #
248 # SVr4 echo swallows backslashes, and there's no way to turn it off.
249 # Therefore use Echo whenever backslashes may be important.
250 # printf is in the posix.2 standard -- which means it's in 
251 # all modern shells.
252 #
253 Echo() {
254         printf "%s\n" "$@"
255 }
256
257 #
258 setPaperZize() {
259 case "$PAPERSIZE" in
260   a0*)
261     PWIDTH=2389
262     PHEIGHT=3381
263     PAPERSIZE=a0paper
264     ;;
265   a1|a1p*)
266     PWIDTH=1690
267     PHEIGHT=2389
268     PAPERSIZE=a1paper
269     ;;
270   a2*)
271     PWIDTH=1194
272     PHEIGHT=1690
273     PAPERSIZE=a2paper
274     ;;
275   a3*)
276     PWIDTH=845
277     PHEIGHT=1194
278     PAPERSIZE=a3paper
279     ;;
280   a4*)
281     PWIDTH=597
282     PHEIGHT=845
283     PAPERSIZE=a4paper
284     ;;
285   a5*)
286     PWIDTH=423
287     PHEIGHT=597
288     PAPERSIZE=a5paper
289     ;;
290   a6*)
291     PWIDTH=298
292     PHEIGHT=423
293     PAPERSIZE=a6paper
294     ;;
295   a7*)
296     PWIDTH=211
297     PHEIGHT=298
298     PAPERSIZE=a7paper
299     ;;
300   a8*)
301     PWIDTH=305
302     PHEIGHT=211
303     PAPERSIZE=a8paper
304     ;;
305   a9*)
306     PWIDTH=105
307     PHEIGHT=305
308     PAPERSIZE=a9paper
309     ;;
310   a10*)
311     PWIDTH=74
312     PHEIGHT=105
313     PAPERSIZE=a10paper
314     ;;
315   b0*)
316     PWIDTH=2847
317     PHEIGHT=4023
318     PAPERSIZE=b0paper
319     ;;
320   b1*)
321     PWIDTH=2012
322     PHEIGHT=2847
323     PAPERSIZE=b1paper
324     ;;
325   b2*)
326     PWIDTH=1423
327     PHEIGHT=2012
328     PAPERSIZE=b2paper
329     ;;
330   b3*)
331     PWIDTH=1006
332     PHEIGHT=1423
333     PAPERSIZE=b3paper
334     ;;
335   b4*)
336     PWIDTH=712
337     PHEIGHT=1006
338     PAPERSIZE=b4paper
339     ;;
340   b5*)
341     PWIDTH=503
342     PHEIGHT=712
343     PAPERSIZE=b5paper
344     ;;
345   archA)
346     PWIDTH=650
347     PHEIGHT=867
348     ;;
349   archB)
350     PWIDTH=867
351     PHEIGHT=1301
352     ;;
353   archC)
354     PWIDTH=1301
355     PHEIGHT=1734
356     ;;
357   archD)
358     PWIDTH=1734
359     PHEIGHT=2602
360     ;;
361   archE)
362     PWIDTH=2602
363     PHEIGHT=3469
364     ;;
365   flsa|flse)
366     PWIDTH=614
367     PHEIGHT=940
368     ;;
369   halfletter)
370     PWIDTH=397
371     PHEIGHT=614
372     ;;
373   ledger)
374     PWIDTH=1229
375     PHEIGHT=795
376     ;;
377   legal)
378     PWIDTH=614
379     PHEIGHT=1012
380     ;;
381   letter)
382     PWIDTH=614
383     PHEIGHT=795
384     ;;
385   note)
386     PWIDTH=542
387     PHEIGHT=723
388     ;;
389   *)
390     echo ""
391     echo $0": unknown papersize -- "$PAPERSIZE
392     echo ""
393     ;;
394 esac
395 }
396
397 doRcFiles()
398 {
399 #
400 # RC-files ?
401 #
402 for D in /usr/local/share/ /usr/local/share/lilypond/ /etc/ $HOME/. ./.
403 do
404   RCfile=$D"lilyrc"
405   [ -r $RCfile ] && . $RCfile
406 done
407 fORI=$ORIENTATION
408 fLNG=$LANGUAGE
409 fPSZ=$PAPERSIZE
410 fLHF=$LATEXHF
411 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
412 }
413
414
415
416 #
417 # Trap function (cleanup)
418 #
419
420
421
422 trap cleanup 0 9 15
423
424
425
426 #
427 # Various defaults
428 #
429 [ -z "$TMP" ] && TMP=/tmp
430 if [ ! -d $TMP ]
431 then
432   $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
433   TMP=/tmp
434 fi
435 LOGFILE=$TMP/lilylog.$$                 # Logfile for lilypond
436 PAPERSIZE=a4                            # Default papersize name
437 PWIDTH=597                              # Default paperwidth
438 PHEIGHT=845                             # Default paperheight
439 PNUM="%"                                # Page numbering on
440 LILYOPTS=""                             # Options to lilypond
441 SOURCEDIR=""
442 LILYPOND_EXECUTABLE=lilypond
443 doRcFiles
444
445
446
447 # Keywords defined in titledefs.tex
448 #
449 TF="`kpsewhich -n latex tex titledefs.tex`"
450 MU_DEF=""
451 if [ -r "${TF:=/usr/local/share/texmf/tex/lilypond/titledefs.tex}" ]
452 then
453   MU_DEF=`sed -n -e 's/.*newcommand\*{.\(mudela[^}]*\)}.*$/\1/p' "$TF"`
454 fi
455
456 : "${MU_DEF:=mudelatitle mudelasubtitle mudelacomposer \
457           mudelameter mudelaopus mudelaarranger mudelapiece \
458           mudelapoet mudelainstrument \
459           }"
460
461 #
462 # debugging
463 #
464 debug_echo=:
465 #
466 # All files in one LaTeX run
467 #
468 SEPFILE=N
469 #
470 # Find command line options and switches
471 #
472 # "x:" x takes argument
473 #
474 switches="DF:H:I:KLNO:W:dhkl:o:p:S:s\?"
475 options=""
476 #
477 # ugh, "\-" is a hack to support long options
478 # while getopts \-:$options$switches O
479 # must be in double quotes for bash-2.0
480 while getopts "\-:$options$switches" O
481 do
482   $debug_echo "O: \`$O'"
483   $debug_echo "arg: \`$OPTARG'"
484   case $O in
485     D  )
486       [ $debug_echo = echo ] && set -x
487       debug_echo=echo
488       ;;
489     F  )
490       LATEXHF=$OPTARG
491       ;;
492     H  )
493       PHEIGHT=$OPTARG
494       ;;
495     I  )
496       LILYOPTS="$LILYOPTS -I $OPTARG"
497       LILYINCLUDE="$LILYINCLUDE:$OPTARG"
498       ;;
499     K  )
500       KEEP_LILY_OUTPUT=Y
501       ;;
502     L  )
503       ORIENTATION=landscape
504       ;;
505     O  )
506       ORIENTATION=$OPTARG
507       ;;
508     N  )
509       PNUM="\pagestyle{empty}"
510       ;;
511     W  )
512       PWIDTH=$OPTARG
513       ;;
514     d  )
515       LILYOPTS=$LILYOPTS" -d"
516       ;;
517     h  )
518       help;
519       exit 0
520       ;;
521     k  )
522       KEEP_LY2DVI_OUTPUT=Y
523       ;;
524     l  )
525       LANGUAGE=$OPTARG
526       ;;
527     o  )
528       OUTPUTDIR=$OPTARG
529       ;;
530     p  )
531       PAPERSIZE=$OPTARG
532       ;;
533     s  )
534       SEPFILE=Y
535       ;;
536     S  )
537       SOURCEDIR=$OPTARG
538       ;;
539     \? )
540       help;
541       exit -1
542       ;;
543     # a long option!
544     -)
545       $debug_echo "long option: \`$OPTARG'"
546       case "$OPTARG" in
547         He*|-He*)
548           PHEIGHT"`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"=
549           ;;
550         W*|-W*)
551           PWIDTH="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
552           ;;
553         dep*|-dep*)
554           LILYOPTS="$LILYOPTS -d"
555           ;;
556         d*|-d*)
557           [ $debug_echo = echo ] && set -x
558           debug_echo=echo
559           ;;
560         hea*|-hea*)
561           LATEXHF="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
562           ;;
563         h*|-h*)
564           help;
565           exit 0
566           ;;
567         i*|-i*)
568           dir="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
569           LILYOPTS="$LILYOPTS --include=$dir"
570           LILYINCLUDE="$LILYINCLUDE:$dir"
571           ;;
572         keepli*|-keepli*)
573           KEEP_LILY_OUTPUT=Y
574           ;;
575         k*|-k*)
576           KEEP_LY2DVI_OUTPUT=Y
577           ;;
578         land*|-land*)
579           ORIENTATION=landscape
580           ;;
581         lang*|-lang*)
582           LANGUAGE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
583           ;;
584         n*|-n*)
585           PNUM="\pagestyle{empty}"
586           ;;
587         or*|-or*)
588           ORIENTATION="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
589           ;;
590         ou*|-ou*)
591           OUTPUTDIR="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
592           ;;
593         p*|-p*)
594           PAPERSIZE="`expr \"$OPTARG\" ':' '[^=]*=\(.*\)'`"
595           ;;
596         separa*|-sep*)
597           SEPFILE=Y
598           ;;
599         sourcedir*|-sourcedir*)
600           SOURCEDIR=$OPTARG
601           ;;
602         *|-*)
603           echo "$0: illegal option -- \"$OPTARG\""
604           help;
605           exit -1
606           ;;
607       esac
608   esac
609 done
610 shift `expr $OPTIND - 1`
611
612 if [ "$SOURCEDIR" != "" ]; then
613 # apparently MakeTeXTFM can't handle relative dirs
614         SOURCEDIR=`cd $SOURCEDIR; pwd`
615         export LILYINCLUDE="$SOURCEDIR/init:$SOURCEDIR/mf/out:$LILYINCLUDE"
616         export TEXINPUTS="$SOURCEDIR/tex:$TEXINPUTS:"
617         export MFINPUTS="$SOURCEDIR/mf:$MFINPUTS:"
618         export LILYPOND_EXECUTABLE="$SOURCEDIR/lily/out/lilypond"
619
620         set|grep MF
621         set|grep TEXIN
622 fi
623
624 #
625 # Input file name
626 #
627 if [ "$1" = "" ]
628 then
629   help
630   $debug_echo "$IDENTIFICATION: No input file name given"
631   exit 1
632 fi
633 # regexp_quote -- quote a string as a regular expression for egrep or sed
634 regexp_quote(){
635         Echo "$@" | sed 's/\([]\[\.*?+^$]\)/\\\1/g'
636 }
637 #
638 #
639 mudelaDefs(){
640 # Include \def\mudela-definitions
641 #
642 for L in $MU_DEF
643 do
644   for F in "$File" "$OF"
645   do
646     LL=`sed -n 's/\\\\def\\\\'"$L"'{\([^}]*\)}.*$/\1/p' "$F"`
647     [ "$LL" ] && {
648           Echo "$1\\"$L'{'"$LL"'}%'                                >> $LatF
649           break
650     }
651   done
652 done
653 }
654 #
655 startFile(){
656 #
657 # LaTeX file name
658 #
659 BN=`basename "$File" .tex`
660 FN="$BN.$$"
661 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
662 then
663   LatF="$TMP/$FN.tex"
664 else
665   LatF="$FN.tex"
666 fi
667 #
668 # Find:
669 #   paper size        (PAPERSIZE, overridden by command line option -p)
670 #   paper orientation (ORIENTATION, overridden by option -o)
671 #   language          (LANGUAGE, overridden by option -l)
672 #   textwidth
673 #
674 eval `sed -n \\
675   -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
676   -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
677   -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
678   -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
679   -e 's/\\\\def\\\\mudelapapertextheight{\([^}]*\).*$/THN=\1;/p' \\
680   -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
681     "$File"`
682 #
683 if [ -z "$LATEXHF" ]
684 then
685   LATEXHF="$fLHF"
686 fi
687 LLHF="%"
688 if [ -n "$LATEXHF" ]
689 then
690   [ -f  "$LATEXHF" ] && LLHF="\input{$LATEXHF}"
691 fi
692 #
693 if [ -z "$PAPERSIZE" ]
694 then
695   PAPERSIZE="$fPSZ"
696 fi
697 if [ -n "$PAPERSIZE" ]
698 then
699   setPaperZize
700   PAPEROPT="$PAPERSIZE"
701 fi
702 #
703 if [ -z "$ORIENTATION" ]
704 then
705   ORIENTATION="$fORI"
706 fi
707 if [ -n "$ORIENTATION" ]
708 then
709   if [ -z "$PAPEROPT" ]
710   then
711     PAPEROPT="$ORIENTATION"
712   else
713     PAPEROPT="$PAPEROPT,$ORIENTATION"
714   fi
715 fi
716 #
717 if [ -n "$PAPEROPT" ]
718 then
719   PAPER="[$PAPEROPT]"
720 fi
721 #
722 if [ -z "$LANGUAGE" ]
723 then
724   LANGUAGE="$fLNG"
725 fi
726 if [ -n "$LANGUAGE" ]
727 then
728   LLNG="\usepackage[$LANGUAGE]{babel}"
729 else
730   LLNG="%"
731 fi
732
733 #
734 # Find textwidth
735 #
736 if [ -n "$TWN" ]
737 then
738   TW="$TWN"
739   case "$TW" in
740     *mm)
741       ;;
742     *cm)
743       ;;
744     *pt)
745       ;;
746     *)
747       TW="${TW}pt"
748       ;;
749   esac
750   $debug_echo "Text width = $TW"
751 fi
752 TWp=`Echo $TW | sed -e 's/\..*$//'`
753 PWp="$PWIDTH"
754 #
755 # Find textheight
756 #
757 if [ -n "$THN" ]
758 then
759   TH=$THN
760   case $TH in
761     *mm)
762       ;;
763     *cm)
764       ;;
765     *pt)
766       ;;
767     *)
768       TH="${TH}pt"
769       ;;
770   esac
771   $debug_echo "Text height = $TH"
772 fi
773 THp="`echo $TH | sed -e 's/\..*$//'`"
774 PHp="$PHEIGHT"
775 if [ "$ORIENTATION" = "landscape" ]
776 then
777   PWp="$PHEIGHT"
778   PHp="$PWIDTH"
779 fi
780 HMARG="`expr '(' $PWp - $TWp ')' / 2`"pt
781 $debug_echo "Text left = $HMARG"
782 VMARG="`expr '(' $PHp - $THp ')' / 2`"pt
783 $debug_echo "Text top = $VMARG"
784 #
785 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
786 #
787 #
788 # Write LaTeX file
789 #
790 cat << EOF > $LatF
791 % Creator: $IDENTIFICATION
792 % Automatically generated from  $IF, $NOW
793
794 \documentclass$PAPER{article}
795
796 $LLNG
797 \usepackage{geometry}
798 \usepackage[latin1]{inputenc}
799 %\usepackage[T1]{fontenc}
800 $PNUM
801 %\addtolength{\oddsidemargin}{-1cm}
802 %\addtolength{\topmargin}{-1cm}
803 %\setlength{\textwidth}{$TW}
804 %\setlength{\textheight}{$TH}
805 \geometry{width=$TW, left=$HMARG, height=$TH, top=$VMARG}
806 \input lilyponddefs
807 \input titledefs
808 $LLHF
809 \begin{document}
810 EOF
811 mudelaDefs
812 cat << EOF >> $LatF
813 \cmrtwenty% ugh
814 \makelilytitle
815 EOF
816 }
817 nextFile(){
818 cat << EOF >> $LatF
819 \def\theopus{}%
820 \def\thepiece{}%
821 \def\mudelaopus{}%
822 \def\mudelapiece{}%
823 EOF
824 mudelaDefs "\\def"
825 cat << EOF >> $LatF
826 \def\theopus{\mudelaopus}% ugh
827 \def\thepiece{\mudelapiece}%
828 \makelilypiecetitle
829 EOF
830 }
831 #
832 # Conclusion
833 #
834 endFile(){
835 cat << EOF >> $LatF
836 \vfill\hfill{\LilyIdString}
837 \end{document}
838 EOF
839 #
840 # Run LaTeX
841 #
842 latex '\nonstopmode \input '$LatF || exit 5
843 #
844 # Rename dvi file
845 #
846 if [ -f $FN.dvi ]
847 then
848     RESULT="$BN.dvi"
849     [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
850     
851     cp "$FN.dvi" "$RESULT" || exit 5
852 fi
853 #
854 # Output some info
855 #
856 cat << EOF
857
858 $IDENTIFICATION: dvi file name is $RESULT
859
860 EOF
861 }
862
863 # ugh. GF is side-effect.
864 findInput() {
865 # should check for LILYINCLUDE
866   OIFS="$IFS"
867   IFS=':'
868   x=`echo $LILYINCLUDE | sed "s!:! !g"`
869   IFS="$OIFS"
870   for lypath in . $x
871   do
872     if [ -f "$lypath/$1" ]
873     then
874       GF="$lypath/$1"
875       return
876     fi
877
878     if [ -f "$lypath/$1.ly" ]
879     then
880       GF="$lypath/$1.ly"
881       return
882     fi
883     if [ -f "$lypath/$1.fly" ]
884     then
885        GF="$lypath/$1.fly"
886        return
887     fi
888   done
889   $debug_echo "$IDENTIFICATION: Input file \"$GF\" not found"
890   echo "$NAME: Input file \"$GF\" not found"                       1>&2
891   exit 2
892 }
893 #
894 # Loop through all files
895 #
896
897 LILY_OUTPUT_FILES=
898
899 for GF 
900 do
901     findInput $GF
902
903   #
904   # Check whether the file is input to or output from lilypond
905   #
906   L1=`head -1 $GF` 
907   OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
908   if [ -n "$OP" ]
909   then
910     #
911     # OK - it's the output from lilypond.
912     #
913     # Get lilypond source file name
914     #
915     OF=$GF
916     IFL=`grep mudelafilename $OF`
917     if [ "$IFL" != "" ]
918     then
919       IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
920       #
921       # Check if source file exists
922       #
923       if [ ! -f $IF ]
924       then
925         $debug_echo $IDENTIFICATION": Mudela file not found."
926         TW=15.5cm
927         TH=25.0cm
928       fi
929     else
930       $debug_echo $IDENTIFICATION": Mudela file name not found."
931       TW=15.5cm
932       TH=25.0cm
933     fi
934   else
935     #
936     # I have to assume this is the lilypond input file
937     # Find output file name, if defined
938     #
939     IF=$GF
940     #
941     # Run lilypond
942     # Grab output file names
943     #
944     $debug_echo "lilypond "$LILYOPTS $IF
945
946     $LILYPOND_EXECUTABLE $LILYOPTS $IF 2>&1  | tee $LOGFILE
947     OF="`sed -n -e 's/TeX output to \([^\.]*\.tex\)\.\.\.$/\1/p' $LOGFILE`"
948     $debug_echo "==> $OF"
949     LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"
950     STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
951     echo $STATUS
952     if [ ! -z "$STATUS" ]
953     then
954       exit 10
955     fi
956   fi
957   #
958   # "Spin through" all the files
959   #
960   for File in $OF
961   do
962     $debug_echo "--- "$File
963     #
964     # Check if output file is generated
965     #
966     if [ ! -f "$File" ]
967     then
968       $debug_echo "$IDENTIFICATION: hmm, I could not find the output file $File"
969       exit 4
970     fi
971     #
972     # Is this the first file?
973     #
974     if [ -z "$FFile" ]
975     then
976       FFile="$File"
977       startFile
978     else
979       nextFile
980     fi
981     cat << EOF >> $LatF
982 \input{$File}
983 EOF
984     if [ $SEPFILE = Y ]
985     then
986       FFile=""
987       endFile
988     fi
989   done
990 done
991 if [ $SEPFILE = N ]
992 then
993   endFile
994 fi
995 #
996 # OK - finished
997 #