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