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