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