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