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