]> git.donarmstrong.com Git - lilypond.git/blob - bin/ly2dvi.sh
2f048fb4da8cedf4960ddca8e6df765058fa4420
[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 #  Original LaTeX file made by Mats Bengtsson, 17/8 1997
9 #
10
11 VERSION="0.10"
12 NAME=ly2dvi.sh
13 IDENTIFICATION="$NAME $VERSION" 
14 NOW=`date`
15 echo "$IDENTIFICATION" 1>&2
16
17 # NEWS
18
19 #0.10
20 #       - -K,--keeplilypond : Keep lilypond output files (default delete)
21 #       - -k,--keeply2dvi   : Keep ly2dvi   output files (default delete)
22 #       - -L,--landscape    : Set landscape orientation
23 #       - -N,--nonumber     : Turn off page numbering (\pagestyle{empty})
24 #       - Could not reinsert "\usepackage[T1]{fontenc}" because
25 #         "los-toros" won't work with it
26 #       - Ekstra LaTeX headers from input file
27
28 #0.9.hwn1
29 #       - option to remove output of lily
30 # 0.9   - Trap Lilypond abort
31 #       - Replaced "\usepackage[T1]{fontenc}" with
32 #         \usepackage[latin1]{inputenc} (takk, Mats)
33 #       - Removed "()" around "\LilyIdString" (Janne didn't want it)
34 # 0.8   - Trap Lilypond segmentation fault
35 #       - Function for cleanup
36 #       - Trap line
37 #       - More human-readable variables
38 #       - Some logics concerning rc-files
39 # 0.7
40 #       - Improved Lilypond error checking
41 #       - Output orientation (landscape...). Overrides mudela file
42 #         variable orientation="landscape";
43 #       - Paper width and heigth put into variables (only A4!)
44 #       - Adjusted top margin to default.....
45 #
46 #TODO
47 #       - Include more papersizes.
48 #       - Manual page.......
49 # 0.6.jaf2
50 #       - LILYINCLUDE update
51 #
52 # 0.6.hwn1
53 #       - handle LILYINCLUDE
54 #       - --output
55 #
56 # 0.6
57 #       - Source rc-files, if present. Files are:
58 #         /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
59 #       - tee output from Lilypond
60 #       - Handles margins for A4 paper (equal on both sides)
61 #       - new option -s (--separate) for one LaTeX run per file,
62 #         else all files are run together
63 #
64 # 0.5
65 #       - More useful ("two-level") debug.
66 #       - The Q&D hack to find file names and not handling \include
67 #         is replaced by grabbing output file names from Lilypond.
68 #       = Detects multiple output files - adds them when running
69 #         LaTeX.
70 #       - Works with multiple input files - no matter if they are
71 #         (a mix of) input to or output from Lilypond.
72 #
73 #TODO
74 #       - Still no margins handling.
75 #       - We have to discuss how to handle multiple output files
76 #         from Lilypond - 'ly2dvi standchen' gives a rather odd
77 #         result....
78
79 # 0.4.1
80 #       - Always exit after printing help info
81 # 0.4
82 #       - Changes to ensure for more strict grep'ing of parameters
83 #         Thanks to from G.B.Stott@bolton.ac.uk
84 #       - More efficient use of sed -e 's///' -e 's///'
85 #         Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
86 #       - Ask tex for location of titledefs.tex (Thanks to JV)
87 #       - Accept only exact match of "\def\mudelacomposer{"
88 #         (or whatever mudela* defined in titledefs.tex)
89 #       - Even more efficient use of sed (Thanks to JV)
90 #       - Default file name for single output file implemented.
91 #       - Moved help into function - finally included from 0.1.jcn1
92 #
93 #TODO
94 #       - Still doesn't handle \include
95 #       - The Q&D for finding output file name from the sequence of
96 #         \paper \output \midi \output really needs to be looked at.
97 #         I have improved it a lot, but it's only capable of finding
98 #         one (the last) file name.
99 #         Well, I have to rewrite this entirely to handle \include,
100 #         then I can fix it.
101 #       - Still no margins handling.
102 #
103 #WARNING
104 #       - Some lines of output from lilypond do NOT start
105 #         at first character position, therefore I have removed "^"
106 #         in sed'ing and grep'ing.
107
108 # 0.3.hwn1
109 #       - add "Creator: " line to output
110 #
111 # 0.3
112 #       - multiple input files to make score from several files
113 #         (extra files assumed to be Lilypond output)
114 #       - cp dvi-file instead of mv, to allow for xdvi reload
115 #       - check for illegal long options
116 #       - put in pt in text width, if not given
117 #       - put in \nonstopmode in LaTeX file
118 #       - restored LaTeX exit value check
119  
120 # 0.2.1
121 #       - temporarily omit LaTeX exit value check
122 #       - remove ALL temporary files
123
124 # 0.2
125 #       - fix for solaris          - included from 0.1.jcn1
126 #       - long option hack         - included from 0.1.jcn1 - modified
127 #       - moved help into function - NOT included from 0.1.jcn1 yet
128 #       - various improvements     - included from 0.1.jcn1
129 #       - find mudela definitions from titledefs.tex
130 #       - find papersize from lilypond output file (mudelapapersize),
131 #         overridden by option '-p size' or '--papersize=size'
132 #       - option -l lang or --language=lang overrides
133 #         lilypond output file definition (mudelalanguage)
134 #       - textwidth from lilypond output file (mudelapaperlinewidth)
135
136 # 0.1.jcn1
137 #       - fix for solaris
138 #       - long option hack
139 #       - moved help into function
140
141 #
142 # Clean up
143 #
144 cleanup() {
145   if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
146   then
147     [ -n "$LatF" -a -f "$LatF" ]       && rm -f $LatF
148     [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
149     [ -n "$FN" ]                       && rm -f $FN.*
150     for F in *$$* $TMP/*$$*
151     do
152       rm -f $F
153     done
154   fi
155   if [ "$KEEP_LILY_OUTPUT" != "Y" ]
156   then
157     for F in $LILY_OUTPUT_FILES
158     do
159       [ -f $F ] && rm -f $F
160     done
161   fi
162 }
163 #
164 # print usage
165 #
166 help() {
167   cat << EOF
168 Generate dvi file from mudela or lilypond output
169 Usage: $0 [options] file[s]
170
171 Options:
172   -D,--debug           set debug mode
173   -H,--headers=        name of additional LaTeX headers file
174   -K,--keeplilypond    keep lilypond output files
175   -L,--landscape       set landscape orientation
176   -N,--nonumber        switch off page numbering
177   -O,--orientation=    set orientation (landscape or portrait (default))
178   -o,--output=         set output directory
179   -h,--help            this help text
180   -k,--keeply2dvi      keep ly2dvi output files
181   -l,--language=       give LaTeX language (babel)
182   -p,--papersize=      give LaTeX papersize (eg. a4paper)
183   -s,--separate        run all files separately through LaTeX
184
185   files may be (a mix of) input to or output from lilypond(1)
186 EOF
187 }
188 #
189 # Trap function (cleanup)
190 #
191 trap cleanup 0 9 15
192 #
193 # Various defaults
194 #
195 [ -z "$TMP" ] && TMP=/tmp
196 if [ ! -d $TMP ]
197 then
198   $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
199   TMP=/tmp
200 fi
201 LOGFILE=$TMP/lilylog.$$                 # Logfile for lilypond
202 PWIDTH=600;                             # Width of A4 paper!
203 PHEIGTH=845;                            # Heigth of A4 paper!
204 PNUM="%"                                # Page numbering on
205 #
206 # RC-files ?
207 #
208 for D in /usr/local/share/ /etc/ $HOME/. ./.
209 do
210   RCfile=$D"lilyrc"
211   [ -f $RCfile ] && . $RCfile
212 done
213 fORI=$ORIENTATION
214 fLNG=$LANGUAGE
215 fPSZ=$PAPERSIZE
216 fLHF=$LATEXHF
217 unset ORIENTATION LANGUAGE PAPERSIZE LATEXHF
218
219 # Keywords defined in titledefs.tex
220 #
221 TF=`kpsewhich -n tex tex titledefs.tex`
222 if [ -n $TF ]
223 then
224   TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
225 fi
226 MU_DEF=""
227 if [ -f $TF ]
228 then
229   MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
230     sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
231 fi
232
233 if [ -z "$MU_DEF" ]
234 then
235   MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
236           mudelaarranger mudelainstrument"
237 fi
238
239 #
240 # debugging
241 #
242 debug_echo=true
243 #
244 # All files in one LaTeX run
245 #
246 SEPFILE=N
247 #
248 # Find command line options and switches
249 #
250 # "x:" x takes argument
251 #
252 switches="DH:KLNO:hkl:o:p:s\?"
253 options=""
254 #
255 # ugh, "\-" is a hack to support long options
256 # while getopts \-:$options$switches O
257 # must be in double quotes for bash-2.0
258 while getopts "\-:$options$switches" O
259 do
260   $debug_echo "O: \`$O'"
261   $debug_echo "arg: \`$OPTARG'"
262   case $O in
263     D  )
264       [ $debug_echo = echo ] && set -x
265       debug_echo=echo
266       ;;
267     H  )
268       LATEXHF=$OPTARG
269       ;;
270     K  )
271       KEEP_LILY_OUTPUT=Y
272       ;;
273     L  )
274       ORIENTATION=landscape
275       ;;
276     O  )
277       ORIENTATION=$OPTARG
278       ;;
279     N  )
280       PNUM="\pagestyle{empty}"
281       ;;
282     h  )
283       help;
284       exit 0
285       ;;
286     k  )
287       KEEP_LY2DVI_OUTPUT=Y
288       ;;
289     l  )
290       LANGUAGE=$OPTARG
291       ;;
292     o  )
293       OUTPUTDIR=$OPTARG
294       ;;
295     p  )
296       PAPERSIZE=$OPTARG
297       ;;
298     s  )
299       SEPFILE=Y
300       ;;
301     \? )
302       help;
303       exit -1
304       ;;
305     # a long option!
306     -)
307       $debug_echo "long option: \`$OPTARG'"
308       case "$OPTARG" in
309         d*|-d*)
310           [ $debug_echo = echo ] && set -x
311           debug_echo=echo
312           ;;
313         hea*|-hea*)
314           LATEXHF=`echo $OPTARG | sed -e s/"^.*="//`
315           ;;
316         h*|-h*)
317           help;
318           exit 0
319           ;;
320         keepli*|-keepli*)
321           KEEP_LILY_OUTPUT=Y
322           ;;
323         k*|-k*)
324           KEEP_LY2DVI_OUTPUT=Y
325           ;;
326         land*|-land*)
327           ORIENTATION=landscape
328           ;;
329         lang*|-lang*)
330           LANGUAGE=`echo $OPTARG | sed -e s/"^.*="//`
331           ;;
332         n*|-n*)
333           PNUM="\pagestyle{empty}"
334           ;;
335         or*|-or*)
336           ORIENTATION=`echo $OPTARG | sed -e s/"^.*="//`
337           ;;
338         ou*|-ou*)
339           OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
340           ;;
341         p*|-p*)
342           PAPERSIZE=`echo $OPTARG | sed -e s/"^.*="//`
343           ;;
344         s*|-s*)
345           SEPFILE=Y
346           ;;
347         *|-*)
348           echo $0": illegal option -- "$OPTARG;
349           help;
350           exit -1
351           ;;
352       esac
353   esac
354 done
355 shift `expr $OPTIND - 1`
356 #
357 # Input file name
358 #
359 if [ "$1" = "" ]
360 then
361   help
362   $debug_echo $IDENTIFICATION": No input file name given"
363   exit 1
364 fi
365 #
366 startFile(){
367 #
368 # LaTeX file name
369 #
370 BN=`basename $File .tex`
371 FN=$BN.$$
372 if [ "$KEEP_LY2DVI_OUTPUT" != "Y" ]
373 then
374   LatF=$TMP/$FN.tex
375 else
376   LatF=$FN.tex
377 fi
378 #
379 # Find:
380 #   paper size        (PAPERSIZE, overridden by command line option -p)
381 #   paper orientation (ORIENTATION, overridden by option -o)
382 #   language          (LANGUAGE, overridden by option -l)
383 #   textwidth
384 #
385 eval `sed -n \\
386   -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
387   -e 's/\\\\def\\\\mudelalatexheaders{\([^}]*\).*$/fLHF=\1;/p' \\
388   -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
389   -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
390   -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
391     $File`
392 #
393 if [ -z "$LATEXHF" ]
394 then
395   LATEXHF=$fLHF
396 fi
397 LLHF="%"
398 if [ -n "$LATEXHF" ]
399 then
400   [ -f $LATEXHF ] && LLHF="\input{$LATEXHF}"
401 fi
402 #
403 if [ -z "$PAPERSIZE" ]
404 then
405   PAPERSIZE=$fPSZ
406 fi
407 if [ -n "$PAPERSIZE" ]
408 then
409   PAPEROPT=$PAPERSIZE
410 fi
411 #
412 if [ -z "$ORIENTATION" ]
413 then
414   ORIENTATION=$fORI
415 fi
416 if [ -n "$ORIENTATION" ]
417 then
418   if [ -z "$PAPEROPT" ]
419   then
420     PAPEROPT=$ORIENTATION
421   else
422     PAPEROPT=$PAPEROPT,$ORIENTATION
423   fi
424 fi
425 #
426 if [ -n "$PAPEROPT" ]
427 then
428   PAPER="["$PAPEROPT"]"
429 fi
430 #
431 if [ -z "$LANGUAGE" ]
432 then
433   LANGUAGE=$fLNG
434 fi
435 if [ -n "$LANGUAGE" ]
436 then
437   LLNG="\usepackage["$LANGUAGE"]{babel}"
438 else
439   LLNG="%"
440 fi
441
442 #
443 # Find textwidth
444 #
445 if [ -n "$TWN" ]
446 then
447   TW=$TWN
448   case $TW in
449     *mm)
450       ;;
451     *cm)
452       ;;
453     *pt)
454       ;;
455     *)
456       TW=$TW"pt"
457       ;;
458   esac
459   $debug_echo "Text width = "$TW
460 fi
461 TWp=`echo $TW | sed -e 's/\..*$//'`
462 PWp=$PWIDTH
463 if [ "$ORIENTATION" = "landscape" ]
464 then
465   PWp=$PHEIGTH
466 fi
467 MARG=`expr $PWp - $TWp`
468 MARG=`expr $MARG / 2`"pt"
469 #
470 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
471 #
472 #
473 # Write LaTeX file
474 #
475 cat << EOF > $LatF
476 % Creator: $IDENTIFICATION
477 % Automatically generated from  $IF, $NOW
478
479 \documentclass$PAPER{article}
480 \nonstopmode
481 $LLNG
482 \usepackage{geometry}
483 \usepackage[latin1]{inputenc}
484 %\usepackage[T1]{fontenc}
485 $PNUM
486 %\addtolength{\oddsidemargin}{-1cm}
487 %\addtolength{\topmargin}{-1cm}
488 \setlength{\textwidth}{$TW}
489 %\geometry{width=$TW, left=$MARG, top=1cm}
490 \geometry{width=$TW, left=$MARG}
491 \input lilyponddefs
492 \input titledefs
493 $LLHF
494 \begin{document}
495 EOF
496 #
497 # Include \def\mudela-definitions
498 #
499 for L in $MU_DEF
500 do
501   LL=`egrep '^\\\\def.'$L'{' $OF`
502   if [ "$LL" != "" ]
503   then
504     LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
505     if [ "$LLL" != "" ]
506     then
507       echo "\\"$L'{'$LLL'}%'                                >> $LatF
508     fi
509   fi
510 done
511 #
512 cat << EOF >> $LatF
513 \makelilytitle
514 EOF
515 }
516 #
517 # Conclusion
518 #
519 endFile(){
520 cat << EOF >> $LatF
521 \vfill\hfill{\LilyIdString}
522 \end{document}
523 EOF
524 #
525 # Run LaTeX
526 #
527 latex $LatF || exit 5
528 #
529 # Rename dvi file
530 #
531 if [ -f $FN.dvi ]
532 then
533     RESULT=$BN.dvi
534     [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
535     cp $FN.dvi $RESULT
536 fi
537 #
538 # Output some info
539 #
540 cat << EOF
541
542 $IDENTIFICATION: dvi file name is $RESULT
543
544 EOF
545 }
546
547 # ugh. GF is side-effect.
548 findInput() {
549 # should check for LILYINCLUDE
550   for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
551   do
552     if [ -f "$lypath/$1" ]
553     then
554       GF="$lypath/$1"
555       return        
556     fi
557
558     if [ -f "$lypath/$1.ly" ]
559     then
560       GF="$lypath/$1.ly"
561       return
562     fi
563   done
564   $debug_echo $IDENTIFICATION": Input file "$GF" not found"
565   echo $NAME": Input file "$GF" not found"                       1>&2
566   exit 2
567 }
568 #
569 # Loop through all files
570 #
571
572 LILY_OUTPUT_FILES=
573
574 for GF in $*
575 do
576     findInput $GF
577
578   #
579   # Check whether the file is input to or output from lilypond
580   #
581   L1=`head -1 $GF` 
582   OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
583   if [ -n "$OP" ]
584   then
585     #
586     # OK - it's the output from lilypond.
587     #
588     # Get lilypond source file name
589     #
590     OF=$GF
591     IFL=`grep mudelafilename $OF`
592     if [ "$IFL" != "" ]
593     then
594       IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
595       #
596       # Check if source file exists
597       #
598       if [ ! -f $IF ]
599       then
600         $debug_echo $IDENTIFICATION": Mudela file not found."
601         TW=15.5cm
602       fi
603     else
604       $debug_echo $IDENTIFICATION": Mudela file name not found."
605       TW=15.5cm
606     fi
607   else
608     #
609     # I have to assume this is the lilypond input file
610     # Find output file name, if defined
611     #
612     IF=$GF
613     #
614     # Run lilypond
615     # Grab output file names
616     #
617     $debug_echo "lilypond "$IF
618
619     lilypond $IF 2>&1  | tee $LOGFILE
620     OF=`egrep '^TeX output to ' $LOGFILE | \\
621         sed -e 's/TeX output to//' -e 's/\.\.\.//'`
622     $debug_echo "==> "$OF
623     STATUS=`egrep -i "error|segmentation|abort" $LOGFILE`
624     echo $STATUS
625     if [ ! -z "$STATUS" ]
626     then
627       exit 10
628     fi
629   fi
630   #
631   # "Spin through" all the files
632   #
633   for File in $OF
634   do
635     $debug_echo "--- "$File
636     #
637     # Check if output file is generated
638     #
639     if [ ! -f $File ]
640     then
641       $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
642       exit 4
643     fi
644     #
645     # Is this the first file?
646     #
647     if [ -z "$FFile" ]
648     then
649       FFile=$File
650       startFile
651     fi
652     cat << EOF >> $LatF
653 \input{$File}
654 EOF
655     if [ $SEPFILE = Y ]
656     then
657       FFile=""
658       endFile
659     fi
660     LILY_OUTPUT_FILES="$LILY_OUTPUT_FILES $OF"
661   done
662 done
663 if [ $SEPFILE = N ]
664 then
665   endFile
666 fi
667 #
668 # OK - finished
669 #