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