# Original LaTeX file made by Mats Bengtsson, 17/8 1997
#
-VERSION="0.3.hwn1"
+VERSION="0.5"
IDENTIFICATION="lytodvi.sh $VERSION"
NOW=`date`
echo "$IDENTIFICATION" 1>&2
# NEWS
+# 0.5.hwn1
+# - do tee of output.
+# 0.5
+# - More useful ("two-level") debug.
+# - The Q&D hack to find file names and not handling \include
+# is replaced by grabbing output file names from Lilypond.
+# = Detects multiple output files - adds them when running
+# LaTeX.
+# - Works with multiple input files - no matter if they are
+# (a mix of) input to or output from Lilypond.
+#
+#TODO
+# - Still no margins handling.
+# - We have to discuss how to handle multiple output files
+# from Lilypond - 'ly2dvi standchen' gives a rather odd
+# result....
+
+# 0.4.1
+# - Always exit after printing help info
+# 0.4
+# - Changes to ensure for more strict grep'ing of parameters
+# Thanks to from G.B.Stott@bolton.ac.uk
+# - More efficient use of sed -e 's///' -e 's///'
+# Thanks to Johan Vromans <jvromans@squirrel.nl> and GBS
+# - Ask tex for location of titledefs.tex (Thanks to JV)
+# - Accept only exact match of "\def\mudelacomposer{"
+# (or whatever mudela* defined in titledefs.tex)
+# - Even more efficient use of sed (Thanks to JV)
+# - Default file name for single output file implemented.
+# - Moved help into function - finally included from 0.1.jcn1
+#
+#TODO
+# - Still doesn't handle \include
+# - The Q&D for finding output file name from the sequence of
+# \paper \output \midi \output really needs to be looked at.
+# I have improved it a lot, but it's only capable of finding
+# one (the last) file name.
+# Well, I have to rewrite this entirely to handle \include,
+# then I can fix it.
+# - Still no margins handling.
+#
+#WARNING
+# - Some lines of output from lilypond do NOT start
+# at first character position, therefore I have removed "^"
+# in sed'ing and grep'ing.
+
# 0.3.hwn1
# - add "Creator: " line to output
#
# - moved help into function
#
-# Exit value, when needed
+# print usage
#
-EXIT=0
+help() {
+ cat << EOF
+Generate dvi file from mudela or lilypond output
+Usage: $0 [options] file[s]
+
+Options:
+ -D, --debug set debug mode
+ -h, --help this help text
+ -k, --keep keep LaTeX file
+ -l, --language= give LaTeX language (babel)
+ -p, --papersize= give LaTeX papersize (eg. a4paper)
+ files may be (a mix of) input to or output from lilypond(1)
+EOF
+}
#
# Keywords defined in titledefs.tex
-# Isn't there a way to ask LaTeX for the location of titledefs.tex?
#
-TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
+TF=`kpsewhich -n tex tex titledefs.tex`
+if [ -n $TF ]
+then
+ TF=/usr/lib/texmf/tex/lilypond/titledefs.tex
+fi
MU_DEF=""
if [ -f $TF ]
then
MU_DEF=`egrep "^.newcommand...mudela" $TF | \\
- sed -e s/^.newcommand...//g | sed -e "s/\\}.*$//"`
+ sed -e 's/^.newcommand...//' -e 's/\\}.*$//'`
fi
-if [ "$MU_DEF" = "" ]
+if [ -z "$MU_DEF" ]
then
MU_DEF="mudelatitle mudelasubtitle mudelacomposer \
mudelaarranger mudelainstrument"
#
# debugging
#
-# debug_echo=echo
debug_echo=true
#
$debug_echo "arg: \`$OPTARG'"
case $O in
D )
- set -x
+ if [ $debug_echo = echo ]
+ then
+ set -x
+ fi
+ debug_echo=echo
;;
h )
- HELP=Y
+ help;
+ exit 0
;;
k )
KEEP=Y
PSZ=$OPTARG
;;
\? )
- HELP=Y
+ help;
+ exit -1
;;
# a long option!
-)
$debug_echo "long option: \`$OPTARG'"
case "$OPTARG" in
h*|-h*)
- HELP=Y
+ help;
+ exit 0
;;
k*|-k*)
KEEP=Y
PSZ=`echo $OPTARG | sed -e s/"^.*="//`
;;
D*|-D*)
- set -x;
+ if [ $debug_echo = echo ]
+ then
+ set -x
+ fi
+ debug_echo=echo
;;
*|-*)
echo $0": illegal option -- "$OPTARG;
- EXIT=-1;
- HELP=Y
+ help;
+ exit -1
;;
esac
esac
#
# Input file name
#
-if [ "$HELP" != "Y" ]
+if [ "$1" = "" ]
then
- if [ "$1" = "" ]
- then
- cat << EOF
-
-$0 - no input file name given
-EOF
- EXIT=1
- HELP=Y
- fi
+ help
+ $debug_echo $IDENTIFICATION": No input file name given"
+ exit 1
fi
-GF=$1
#
-# Check if input file exists...
-#
-if [ "$HELP" != "Y" ]
-then
+for GF in $*
+do
+ #
+ # Check if input file exists...
+ #
if [ ! -f $GF ]
then
- GF=$1.ly
+ GF=$GF.ly
if [ ! -f $GF ]
then
- cat << EOF
-
-$0 - input file $GF not found
-EOF
- EXIT=2
- HELP=Y
+ $debug_echo $IDENTIFICATION": Input file "$GF" not found"
+ exit 2
fi
fi
-fi
-
-#
-# print usage
-#
-#help() {
-if [ "$HELP" = "Y" ]
-then
- cat << EOF
-Generate dvi file from mudela or lilypond output
-Usage: $0 [options] file
-
-Options:
- -D, --debug set debug mode
- -h, --help this help text
- -k, --keep keep LaTeX file
- -l, --language= give LaTeX language (babel)
- -p, --papersize= give LaTeX papersize (eg. a4paper)
-
- file may be input to or output from lilypond(1)
-EOF
- exit $EXIT
-fi
-#}
-
-#
-# More files?
-#
-shift 1
-
-#
-# Check whether input file is the input to or output from lilypond
-#
-L1=`head -1 $GF`
-OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
-if [ "$OP" != "" ]
-then
#
- # OK - it's the output from lilypond.
- # Get lilypond source file name
+ # Check whether the file is input to or output from lilypond
#
- OF=$GF
- IFL=`grep mudelafilename $OF`
- if [ "$IFL" != "" ]
+ L1=`head -1 $GF`
+ OP=`echo $L1 | grep "^% Creator: GNU LilyPond"`
+ if [ -n "$OP" ]
then
- IF=`echo $IFL | sed -e s/^.*{// | sed -e s/"}*.$"//`
#
- # Check if source file exists
+ # OK - it's the output from lilypond.
+ #
+ # Get lilypond source file name
#
- if [ ! -f $IF ]
+ OF=$GF
+ IFL=`grep mudelafilename $OF`
+ if [ "$IFL" != "" ]
then
- cat << EOF
-
-$0 - mudela file not found.
-
-EOF
+ IF=`echo $IFL | sed -e 's/.*{//' -e 's/}*.$//'`
+ #
+ # Check if source file exists
+ #
+ if [ ! -f $IF ]
+ then
+ $debug_echo $IDENTIFICATION": Mudela file not found."
+ TW=15.5cm
+ fi
+ else
+ $debug_echo $IDENTIFICATION": Mudela file name not found."
TW=15.5cm
fi
else
- cat << EOF
-
-$0 - mudela file name not found.
+ #
+ # I have to assume this is the lilypond input file
+ # Find output file name, if defined
+ #
+ IF=$GF
+ #
+ # Run lilypond
+ # Grab output file names
+ #
+ $debug_echo "lilypond "$IF
-EOF
- TW=15.5cm
+ lilypond $IF 2>&1 | tee /tmp/lilylog.$$
+ OF=`cat /tmp/lilylog.$$| egrep '^TeX output to ' | \\
+ sed -e 's/TeX output to//' -e 's/\.\.\.//'`
+ $debug_echo "==> "$OF
fi
-else
- #
- # I have to assume this is the lilypond input file
- # Find output file name, if defined
- #
- IF=$GF
- OFS=`egrep "paper|midi|output" $IF`
- OF1=`echo $OFS | sed -e s/".midi.*$"// | sed -e s/"^.*paper"//`
- if [ "$OF1" = "" ]
- then
- OF1=`echo $OFS | sed -e s/"^.*paper"// | sed -e s/".midi.*$"//`
- fi
- if [ "$OF1" = "" ]
- then
- OF=lelie.tex
- else
- OF2=`echo $OF1 | grep output`
- if [ "$OF2" = "" ]
- then
- OF=lelie.tex
- else
- OF=`echo $OF2 | sed -e "s/\\";.*$//" | sed -e "s/^.*\\"//"`
- fi
- if [ "$OF2" = "" ]
- then
- OF=lelie.tex
- fi
- fi
- #
- # Remove the output file, to avoid being misled by an old one
- #
-# if [ -f $OF ]
-# then
-# rm $OF
-# fi
- #
- # Run lilypond - exit if unsuccessfull
- #
- lilypond $IF || exit 3
#
# Check if output file is generated
#
- if [ ! -f $OF ]
- then
- cat << EOF
-
-$0 - hmm, I could not find the output file $OF
-
-EOF
- exit 4
- fi
-fi
-#
-# Find textwidth
-#
-if [ "$IF" != "" ]
-then
- if [ -f $IF ]
- then
- TWL=`grep linewidth $IF`
- TWS=`echo $TWL | grep -v "^%"`
- if [ "$TWS" != "" ]
+ for File in $OF
+ do
+ $debug_echo "--- "$File
+ if [ ! -f $File ]
then
- TW=`echo $TWS | sed -e s/^.*=// | sed -e s/";.*$"// | \\
- sed -e s/.mm/mm/ | sed -e s/.cm/cm/ | sed -e s/.p/p/`
- case $TW in
- *mm)
- ;;
- *cm)
- ;;
- *pt)
- ;;
- *)
- TW=$TW"pt"
- ;;
- esac
- else
- TW=15.5cm
+ $debug_echo $IDENTIFICATION": hmm, I could not find the output file "$File
+ exit 4
fi
- fi
-fi
-#
-# LaTeX file name
-#
-if [ "$KEEP" != "Y" ]
-then
- if [ "$TMP" = "" ]
- then
- TMP=/tmp
- fi
- if [ ! -d $TMP ]
- then
- cat << EOF
-
-$0 - temporary directory $TMP not found, set to /tmp
-
-EOF
- TMP=/tmp
- fi
-#
- BN=`basename $OF .tex`
- FN=$BN.$$
- LF=$TMP/$FN.tex
-else
- BN=`basename $OF .tex`
- FN=$BN.$$
- LF=$FN.tex
-fi
-#
-# Find if a paper size is defined
-#
-if [ "$PSZ" = "" ]
-then
- PSZ=`egrep ".def.mudelapapersize" $OF | \\
- sed -e s/.def.mudelapapersize// | \\
- sed -e s/^{// | sed -e s/}.*$//`
-fi
-
-if [ "$PSZ" != "" ]
-then
- PAPER="["$PSZ"]"
-fi
-
-#
-# Find if a language is defined
-#
-if [ "$LNG" = "" ]
-then
- LNG=`egrep ".def.mudelalanguage" $OF | \\
- sed -e s/.def.mudelalanguage// | \\
- sed -e s/^{// | sed -e s/}.*$//`
-fi
-if [ "$LNG" != "" ]
-then
- LLNG="\usepackage["$LNG"]{babel}"
-else
- LLNG="%"
-fi
-
-#
-# Find if a textwidth is defined
-#
-TWN=`egrep ".def.mudelapaperlinewidth" $OF | \\
- sed -e s/.def.mudelapaperlinewidth// | \\
- sed -e s/^{// | sed -e s/}.*$//`
-if [ "$TWN" != "" ]
-then
- TW=$TWN
- case $TW in
- *mm)
- ;;
- *cm)
- ;;
- *pt)
- ;;
- *)
- TW=$TW"pt"
- ;;
- esac
- $debug_echo "Text width = "$TW
-fi
-
-#
-# Write LaTeX file
-#
-cat << EOF > $LF
+ if [ -z "$FFile" ]
+ then
+ FFile=$File
+ #
+ # LaTeX file name
+ #
+ if [ "$KEEP" != "Y" ]
+ then
+ if [ "$TMP" = "" ]
+ then
+ TMP=/tmp
+ fi
+ if [ ! -d $TMP ]
+ then
+ $debug_echo $IDENTIFICATION": temporary directory "$TMP" not found, set to /tmp"
+ TMP=/tmp
+ fi
+ #
+ BN=`basename $FFile .tex`
+ FN=$BN.$$
+ LF=$TMP/$FN.tex
+ else
+ BN=`basename $FFile .tex`
+ FN=$BN.$$
+ LF=$FN.tex
+ fi
+ #
+ # Find:
+ # paper size (PSZ, overridden by command line option -p)
+ # language (LNG, overridden by command line option -l)
+ # textwidth
+ #
+ eval `sed -n \\
+ -e 's/\\\\def\\\\mudelapapersize{\([^}]*\).*$/fPSZ=\1;/p' \\
+ -e 's/\\\\def\\\\mudelalanguage{\([^}]*\).*$/fLNG=\1;/p' \\
+ -e 's/\\\\def\\\\mudelapaperlinewidth{\([^}]*\).*$/TWN=\1;/p' \\
+ $OF`
+ if [ "$PSZ" = "" ]
+ then
+ PSZ=$fPSZ
+ fi
+ if [ "$PSZ" != "" ]
+ then
+ PAPER="["$PSZ"]"
+ fi
+ #
+ if [ "$LNG" = "" ]
+ then
+ LNG=$fLNG
+ fi
+ if [ "$LNG" != "" ]
+ then
+ LLNG="\usepackage["$LNG"]{babel}"
+ else
+ LLNG="%"
+ fi
+
+ #
+ # Find textwidth
+ #
+ if [ "$TWN" != "" ]
+ then
+ TW=$TWN
+ case $TW in
+ *mm)
+ ;;
+ *cm)
+ ;;
+ *pt)
+ ;;
+ *)
+ TW=$TW"pt"
+ ;;
+ esac
+ $debug_echo "Text width = "$TW
+ fi
+
+ #
+ # Write LaTeX file
+ #
+ cat << EOF > $LF
% Creator: $IDENTIFICATION
% Automatically generated from $IF, $NOW
\input titledefs
\begin{document}
EOF
-#
-# Include \def\mudela-definitions
-#
-for L in $MU_DEF
-do
- LL=`grep ".def."$L $OF`
- if [ "$LL" != "" ]
- then
- LLL=`echo $LL | sed -e s/^.def.$L// | \\
- sed -e s/^{// | sed -e s/}.*$//`
- if [ "$LLL" != "" ]
- then
- echo '\'$L'{'$LLL'}%' >> $LF
- fi
- fi
-done
-#
-cat << EOF >> $LF
+ #
+ # Include \def\mudela-definitions
+ #
+ for L in $MU_DEF
+ do
+ LL=`egrep '^\\\\def.'$L'{' $OF`
+ if [ "$LL" != "" ]
+ then
+ LLL=`echo $LL | sed -e 's/}.*$//' -e 's/.*{//'`
+ if [ "$LLL" != "" ]
+ then
+ echo '\'$L'{'$LLL'}%' >> $LF
+ fi
+ fi
+ done
+ #
+ cat << EOF >> $LF
\makelilytitle
-\input{$OF}
EOF
-for EX in $*; do echo "\input{"$EX"}%" >> $LF; done
+ fi
+ cat << EOF >> $LF
+\input{$File}
+EOF
+ done
+done
cat << EOF >> $LF
\vfill\hfill{(\LilyIdString)}
\end{document}
# Run LaTeX
#
latex $LF || exit 5
-#latex $LF
-cat << EOF
-
-
-Take care - LaTeX exit value check temporarily omitted!
-
-
-EOF
#
# Rename dvi file
#
#
cat << EOF
-$0 - dvi file name is $BN.dvi
+$IDENTIFICATION: dvi file name is $BN.dvi
EOF
# OK - finished