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