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