3 # Script to make a LaTeX file for Lilypond
5 # Written by Jan Arne Fagertun <Jan.A.Fagertun@energy.sintef.no>
6 # Sat Nov 22 22:26:43 CET 1997
8 # Original LaTeX file made by Mats Bengtsson, 17/8 1997
13 IDENTIFICATION
="$NAME $VERSION"
15 echo "$IDENTIFICATION" 1>&2
19 # 0.8 - Trap Lilypond segmentation fault
20 # - Function for cleanup
22 # - More human-readable variables
23 # - Some logics concerning rc-files
26 # - Improved Lilypond error checking
27 # - Output orientation (landscape...). Overrides mudela file
28 # variable orientation="landscape";
29 # - Paper width and heigth put into variables (only A4!)
30 # - Adjusted top margin to default.....
33 # - Include more papersizes.
34 # - Manual page.......
36 # - LILYINCLUDE update
39 # - handle LILYINCLUDE
43 # - Source rc-files, if present. Files are:
44 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
45 # - tee output from Lilypond
46 # - Handles margins for A4 paper (equal on both sides)
47 # - new option -s (--separate) for one LaTeX run per file,
48 # else all files are run together
51 # - More useful ("two-level") debug.
52 # - The Q&D hack to find file names and not handling \include
53 # is replaced by grabbing output file names from Lilypond.
54 # = Detects multiple output files - adds them when running
56 # - Works with multiple input files - no matter if they are
57 # (a mix of) input to or output from Lilypond.
60 # - Still no margins handling.
61 # - We have to discuss how to handle multiple output files
62 # from Lilypond - 'ly2dvi standchen' gives a rather odd
66 # - Always exit after printing help info
68 # - Changes to ensure for more strict grep'ing of parameters
69 # Thanks to from G.B.Stott@bolton.ac.uk
70 # - More efficient use of sed -e 's///' -e 's///'
71 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
72 # - Ask tex for location of titledefs.tex (Thanks to JV)
73 # - Accept only exact match of "\def\mudelacomposer{"
74 # (or whatever mudela* defined in titledefs.tex)
75 # - Even more efficient use of sed (Thanks to JV)
76 # - Default file name for single output file implemented.
77 # - Moved help into function - finally included from 0.1.jcn1
80 # - Still doesn't handle \include
81 # - The Q&D for finding output file name from the sequence of
82 # \paper \output \midi \output really needs to be looked at.
83 # I have improved it a lot, but it's only capable of finding
84 # one (the last) file name.
85 # Well, I have to rewrite this entirely to handle \include,
87 # - Still no margins handling.
90 # - Some lines of output from lilypond do NOT start
91 # at first character position, therefore I have removed "^"
92 # in sed'ing and grep'ing.
95 # - add "Creator: " line to output
98 # - multiple input files to make score from several files
99 # (extra files assumed to be Lilypond output)
100 # - cp dvi-file instead of mv, to allow for xdvi reload
101 # - check for illegal long options
102 # - put in pt in text width, if not given
103 # - put in \nonstopmode in LaTeX file
104 # - restored LaTeX exit value check
107 # - temporarily omit LaTeX exit value check
108 # - remove ALL temporary files
111 # - fix for solaris - included from 0.1.jcn1
112 # - long option hack - included from 0.1.jcn1 - modified
113 # - moved help into function - NOT included from 0.1.jcn1 yet
114 # - various improvements - included from 0.1.jcn1
115 # - find mudela definitions from titledefs.tex
116 # - find papersize from lilypond output file (mudelapapersize),
117 # overridden by option '-p size' or '--papersize=size'
118 # - option -l lang or --language=lang overrides
119 # lilypond output file definition (mudelalanguage)
120 # - textwidth from lilypond output file (mudelapaperlinewidth)
125 # - moved help into function
131 $debug_echo "("$LF")("$FN")("$LOGFILE")"
132 if [ "$KEEP" != "Y" ]
134 [ -n "$LF" -a -f "$LF" ] && rm -f $LF
135 [ -n "$LOGFILE" -a -f "$LOGFILE" ] && rm -f $LOGFILE
136 [ -n "$FN" ] && rm -f $FN.
*
144 Generate dvi file from mudela or lilypond output
145 Usage: $0 [options] file[s]
148 -D, --debug set debug mode
149 -O, --orientation= set orientation (landscape or portrait (default))
150 -o, --output= set output directory
151 -h, --help this help text
152 -k, --keep keep LaTeX file
153 -l, --language= give LaTeX language (babel)
154 -p, --papersize= give LaTeX papersize (eg. a4paper)
155 -s, --separate run all files separately through LaTeX
157 files may be (a mix of) input to or output from lilypond(1)
161 # Trap function (cleanup)
167 [ -z "$TMP" ] && TMP
=/tmp
170 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
173 LOGFILE
=$TMP/lilylog.$$
# Logfile for lilypond
174 PWIDTH
=600; # Width of A4 paper!
175 PHEIGTH
=830; # Heigth of A4 paper!
179 for D
in /usr
/local
/share
/ /etc
/ $HOME/. .
/.
182 [ -f $RCfile ] && .
$RCfile
187 unset ORIENTATION LANGUAGE PAPERSIZE
189 # Keywords defined in titledefs.tex
191 TF
=`kpsewhich -n tex tex titledefs.tex`
194 TF
=/usr
/lib
/texmf
/tex
/lilypond
/titledefs.tex
199 MU_DEF
=`egrep "^.newcommand...mudela" $TF | \\
200 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
205 MU_DEF
="mudelatitle mudelasubtitle mudelacomposer \
206 mudelaarranger mudelainstrument"
214 # All files in one LaTeX run
218 # Find command line options and switches
220 # "x:" x takes argument
222 switches
="DO:hkl:o:p:s\?"
225 # ugh, "\-" is a hack to support long options
226 # while getopts \-:$options$switches O
227 # must be in double quotes for bash-2.0
228 while getopts "\-:$options$switches" O
230 $debug_echo "O: \`$O'"
231 $debug_echo "arg: \`$OPTARG'"
234 [ $debug_echo = echo ] && set -x
265 $debug_echo "long option: \`$OPTARG'"
268 [ $debug_echo = echo ] && set -x
279 LANGUAGE
=`echo $OPTARG | sed -e s/"^.*="//`
282 PAPERSIZE
=`echo $OPTARG | sed -e s/"^.*="//`
285 ORIENTATION
=`echo $OPTARG | sed -e s/"^.*="//`
288 OUTPUTDIR
=`echo $OPTARG | sed -e s/"^.*="//`
294 echo $0": illegal option -- "$OPTARG;
301 shift `expr $OPTIND - 1`
308 $debug_echo $IDENTIFICATION": No input file name given"
316 BN
=`basename $File .tex`
318 if [ "$KEEP" != "Y" ]
326 # paper size (PAPERSIZE, overridden by command line option -p)
327 # paper orientation (ORIENTATION, overridden by option -o)
328 # language (LANGUAGE, overridden by option -l)
332 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
333 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
334 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
335 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
337 if [ -z "$PAPERSIZE" ]
341 if [ -n "$PAPERSIZE" ]
346 if [ -z "$ORIENTATION" ]
350 if [ -n "$ORIENTATION" ]
352 if [ -z "$PAPEROPT" ]
354 PAPEROPT
=$ORIENTATION
356 PAPEROPT
=$PAPEROPT,$ORIENTATION
360 if [ -n "$PAPEROPT" ]
362 PAPER
="["$PAPEROPT"]"
365 if [ -z "$LANGUAGE" ]
369 if [ -n "$LANGUAGE" ]
371 LLNG
="\usepackage["$LANGUAGE"]{babel}"
393 $debug_echo "Text width = "$TW
395 TWp
=`echo $TW | sed -e 's/\..*$//'`
397 if [ "$ORIENTATION" = "landscape" ]
401 MARG
=`expr $PWp - $TWp`
402 MARG
=`expr $MARG / 2`"pt"
404 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
410 % Creator: $IDENTIFICATION
411 % Automatically generated from $IF, $NOW
413 \documentclass$PAPER{article}
416 \usepackage{geometry}
417 \usepackage[T1]{fontenc}
418 %\addtolength{\oddsidemargin}{-1cm}
419 %\addtolength{\topmargin}{-1cm}
420 \setlength{\textwidth}{$TW}
421 %\geometry{width=$TW, left=$MARG, top=1cm}
422 \geometry{width=$TW, left=$MARG}
428 # Include \def\mudela-definitions
432 LL
=`egrep '^\\\\def.'$L'{' $OF`
435 LLL
=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
438 echo '\'$L'{'$LLL'}%' >> $LF
452 \vfill\hfill{(\LilyIdString)}
465 [ -n "$OUTPUTDIR" ] && RESULT="$OUTPUTDIR/$RESULT"
477 $IDENTIFICATION: dvi file name is $RESULT
482 # ugh. GF is side-effect.
484 # should check for LILYINCLUDE
485 for lypath in "." `echo $LILYINCLUDE| sed 's
/:/ /g
'`
487 if [ -f "$lypath/$1" ]
493 if [ -f "$lypath/$1.ly" ]
499 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
500 echo $NAME": Input file "$GF" not found" 1>&2
504 # Loop through all files
511 # Check whether the file is input to or output from lilypond
514 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
518 # OK - it's the output from lilypond.
520 # Get lilypond source file name
523 IFL
=`grep mudelafilename $OF`
526 IF
=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
528 # Check if source file exists
532 $debug_echo $IDENTIFICATION": Mudela file not found."
536 $debug_echo $IDENTIFICATION": Mudela file name not found."
541 # I have to assume this is the lilypond input file
542 # Find output file name, if defined
547 # Grab output file names
549 $debug_echo "lilypond "$IF
551 lilypond
$IF 2>&1 |
tee $LOGFILE
552 OF
=`egrep '^TeX output to ' $LOGFILE | \\
553 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
554 $debug_echo "==> "$OF
555 STATUS
=`egrep -i "error|segmentation" $LOGFILE`
557 if [ ! -z "$STATUS" ]
563 # "Spin through" all the files
567 $debug_echo "--- "$File
569 # Check if output file is generated
573 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
577 # Is this the first file?