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