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