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
20 # - Improved Lilypond error checking
21 # - Output orientation (landscape...). Overrides mudela file
22 # variable orientation="landscape";
23 # - Paper width and heigth put into variables (only A4!)
24 # - Adjusted top margin to default.....
27 # - Include more papersizes.
28 # - Manual page.......
29 # - should detect crashing lilypond
35 # - LILYINCLUDE update
38 # - handle LILYINCLUDE
42 # - Source rc-files, if present. Files are:
43 # /usr/local/share/lilyrc /etc/lilyrc $HOME/.lilyrc ./.lilyrc
44 # - tee output from Lilypond
45 # - Handles margins for A4 paper (equal on both sides)
46 # - new option -s (--separate) for one LaTeX run per file,
47 # else all files are run together
50 # - More useful ("two-level") debug.
51 # - The Q&D hack to find file names and not handling \include
52 # is replaced by grabbing output file names from Lilypond.
53 # = Detects multiple output files - adds them when running
55 # - Works with multiple input files - no matter if they are
56 # (a mix of) input to or output from Lilypond.
59 # - Still no margins handling.
60 # - We have to discuss how to handle multiple output files
61 # from Lilypond - 'ly2dvi standchen' gives a rather odd
65 # - Always exit after printing help info
67 # - Changes to ensure for more strict grep'ing of parameters
68 # Thanks to from G.B.Stott@bolton.ac.uk
69 # - More efficient use of sed -e 's///' -e 's///'
70 # Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
71 # - Ask tex for location of titledefs.tex (Thanks to JV)
72 # - Accept only exact match of "\def\mudelacomposer{"
73 # (or whatever mudela* defined in titledefs.tex)
74 # - Even more efficient use of sed (Thanks to JV)
75 # - Default file name for single output file implemented.
76 # - Moved help into function - finally included from 0.1.jcn1
79 # - Still doesn't handle \include
80 # - The Q&D for finding output file name from the sequence of
81 # \paper \output \midi \output really needs to be looked at.
82 # I have improved it a lot, but it's only capable of finding
83 # one (the last) file name.
84 # Well, I have to rewrite this entirely to handle \include,
86 # - Still no margins handling.
89 # - Some lines of output from lilypond do NOT start
90 # at first character position, therefore I have removed "^"
91 # in sed'ing and grep'ing.
94 # - add "Creator: " line to output
97 # - multiple input files to make score from several files
98 # (extra files assumed to be Lilypond output)
99 # - cp dvi-file instead of mv, to allow for xdvi reload
100 # - check for illegal long options
101 # - put in pt in text width, if not given
102 # - put in \nonstopmode in LaTeX file
103 # - restored LaTeX exit value check
106 # - temporarily omit LaTeX exit value check
107 # - remove ALL temporary files
110 # - fix for solaris - included from 0.1.jcn1
111 # - long option hack - included from 0.1.jcn1 - modified
112 # - moved help into function - NOT included from 0.1.jcn1 yet
113 # - various improvements - included from 0.1.jcn1
114 # - find mudela definitions from titledefs.tex
115 # - find papersize from lilypond output file (mudelapapersize),
116 # overridden by option '-p size' or '--papersize=size'
117 # - option -l lang or --language=lang overrides
118 # lilypond output file definition (mudelalanguage)
119 # - textwidth from lilypond output file (mudelapaperlinewidth)
124 # - moved help into function
131 Generate dvi file from mudela or lilypond output
132 Usage: $0 [options] file[s]
135 -D, --debug set debug mode
136 -O, --orientation= set orientation (landscape or portrait (default))
137 -o, --output= set output directory
138 -h, --help this help text
139 -k, --keep keep LaTeX file
140 -l, --language= give LaTeX language (babel)
141 -p, --papersize= give LaTeX papersize (eg. a4paper)
142 -s, --separate run all files separately through LaTeX
144 files may be (a mix of) input to or output from lilypond(1)
148 PWIDTH=600; # Width of A4 paper!
149 PHEIGTH=830; # Heigth of A4 paper!
153 for D in /usr/local/share/ /etc/ $HOME/. ./.
156 [ -f $RCfile ] && . $RCfile
160 # Keywords defined in titledefs.tex
162 TF=`kpsewhich -n tex tex titledefs.tex`
165 TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
170 MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
171 sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
176 MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
177 mudelaarranger mudelainstrument"
185 # All files in one LaTeX run
189 # Find command line options and switches
191 # "x:" x takes argument
193 switches="DO:hkl:o:p:s\?"
196 # ugh, "\-" is a hack to support long options
197 # while getopts \-:$options$switches O
198 # must be in double quotes for bash-2.0
199 while getopts "\-:$options$switches" O
201 $debug_echo "O: \`$O'"
202 $debug_echo "arg: \`$OPTARG'"
205 if [ $debug_echo = echo ]
239 $debug_echo "long option: \`$OPTARG'"
242 if [ $debug_echo = echo ]
256 LNG=`echo $OPTARG | sed -e s/"^.*="//`
259 PSZ=`echo $OPTARG | sed -e s/"^.*="//`
262 ORI=`echo $OPTARG | sed -e s/"^.*="//`
265 OUTPUTDIR=`echo $OPTARG | sed -e s/"^.*="//`
271 echo $0": illegal option -- "$OPTARG;
278 shift `expr $OPTIND - 1`
285 $debug_echo $IDENTIFICATION": No input file name given"
293 if [ "$KEEP" != "Y" ]
301 $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
305 BN=`basename $File .tex`
309 BN=`basename $File .tex`
315 # paper size (PSZ, overridden by command line option -p)
316 # paper orientation (ORI, overridden by command line option -o)
317 # language (LNG, overridden by command line option -l)
321 -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
322 -e 's/\\\\def\\\\mudelaorientation{\([^}]*\).*$/fORI=\1;/p' \\
323 -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
324 -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
341 if [ -z "$PAPEROPT" ]
345 PAPEROPT=$PAPEROPT,$ORI
349 if [ ! -z "$PAPEROPT" ]
351 PAPER="["$PAPEROPT"]"
360 LLNG="\usepackage["$LNG"]{babel}"
382 $debug_echo "Text width = "$TW
384 TWp=`echo $TW | sed -e 's/\..*$//'`
386 if [ "$ORI" = "landscape" ]
390 MARG=`expr $PWp - $TWp`
391 MARG=`expr $MARG / 2`"pt"
393 # Geometry: /var/lib/texmf/latex/geometry/geometry.dvi
399 % Creator: $IDENTIFICATION
400 % Automatically generated from $IF, $NOW
402 \documentclass$PAPER{article}
405 \usepackage{geometry}
406 \usepackage[T1]{fontenc}
407 %\addtolength{\oddsidemargin}{-1cm}
408 %\addtolength{\topmargin}{-1cm}
409 \setlength{\textwidth}{$TW}
410 %\geometry{width=$TW, left=$MARG, top=1cm}
411 \geometry{width=$TW, left=$MARG}
417 # Include \def\mudela-definitions
421 LL=`egrep '^\\\\def.'$L'{' $OF`
424 LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
427 echo '\'$L'{'$LLL'}%' >> $LF
441 \vfill\hfill{(\LilyIdString)}
454 if [ x$OUTPUTDIR != x ]; then
455 RESULT="$OUTPUTDIR/$RESULT"
462 if [ "$KEEP" != "Y" ]
471 $IDENTIFICATION: dvi file name is $RESULT
476 # ugh. GF is side-effect.
478 # should check for LILYINCLUDE
479 for lypath in "." `echo $LILYINCLUDE| sed 's/:/ /g'`
481 if [ -f "$lypath/$1" ]
487 if [ -f "$lypath/$1.ly" ]
493 $debug_echo $IDENTIFICATION": Input file "$GF" not found"
494 echo $NAME": Input file "$GF" not found" 1>&2
498 # Loop through all files
505 # Check whether the file is input to or output from lilypond
508 OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
512 # OK - it's the output from lilypond.
514 # Get lilypond source file name
517 IFL=`grep mudelafilename $OF`
520 IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
522 # Check if source file exists
526 $debug_echo $IDENTIFICATION": Mudela file not found."
530 $debug_echo $IDENTIFICATION": Mudela file name not found."
535 # I have to assume this is the lilypond input file
536 # Find output file name, if defined
541 # Grab output file names
543 $debug_echo "lilypond "$IF
545 lilypond $IF 2>&1 | tee /tmp/lilylog.$$
546 OF=`egrep '^TeX output to ' /tmp/lilylog.$$ | \\
547 sed -e 's/TeX output to//' -e 's/\.\.\.//'`
548 $debug_echo "==> "$OF
549 STATUS=`grep -i error /tmp/lilylog.$$`
551 if [ ! -z "$STATUS" ]
557 # "Spin through" all the files
561 $debug_echo "--- "$File
563 # Check if output file is generated
567 $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
571 # Is this the first file?