#!/bin/bash
#Creates pdfs from the Custom Layout (or default score layout if none) and loops over all parts then puts all into a pdf with overall title page
#last parameter is prefix for final pdf
#`ls -v *.denemo.gz`  gives them in version order
#this typesets the default score layout and the topmost staff, other staffs have bass figures dropped page breaks on each movement and stretchability limited
#ideally, only the bottom most part would be treated thus - not so good for trio sonatas

find . -name "*.denemo" -exec gzip -f '{}' \;
rm DenemoPDFoutput/*
rm DenemoBook.pdf
mkdir DenemoPDFoutput

echo "Number of params " $# " ok??"

if [[ "$#" -eq 9 ]]; then
echo dollar one $1
title="$1"
composer=$2
opus=$3
arranger=$4
tagline=$5
scalex=$6
scaley=$7
ident=$8
inc=$9
rm ${ident}DenemoBook.pdf
ScmFile=`pwd`/DenemoPDFoutput/script.scm
(
cat <<ADDTEXT

(if (d-PathFromFilename (d-Open "query=filename"))
    (let* ((filename (string-append (d-PathFromFilename (d-Open "query=filename")) "/DenemoPDFoutput/DenemoBookTitle.ly"))
            (tr (let ((lily ""))
					(let loop ((index 0))
						(define tag (d-DirectiveGetNthTag-score index))
							(if (and tag (not (equal? tag "SetPageCount")))
								(let ((prefix (d-DirectiveGet-score-prefix tag)))
									(if prefix
										(set! lily (string-append lily "\\n" prefix)))
									(loop (1+ index)))))
					(set! lily (string-append lily "\\n\\\\noPageBreak\\n"))
					lily))
            (incip (d-DirectiveGet-scoreheader-postfix "ScoreIncipit"))
            (tit (d-DirectiveGet-scoreheader-data "BookTitle"))
            (port (open-file filename "a")))
        (if (and filename tr incip tit port)
            (format port "  
                ~a\n~a\n\\\\markup  \\\\scale #'($scalex . $scaley ) \\\\line { \\\\hspace #10
                \\\\column {
                \\\\line {~a}
                \\\\incipit
                }
                }" tr incip tit))
            (disp "Couldn't open " filename "\n\n")))

ADDTEXT
) > "${ScmFile}"
echo Scheme script stored in ${ScmFile}

LyFile=`pwd`/DenemoPDFoutput/DenemoBookTitle.ly
(
cat <<ADDTEXT
    \header {
                tagline = \markup { \with-url #"http://www.denemo.org/~rshann/DenemoScores/" $tagline}
            }
                \markup  \line {
                    { \postscript #"
                    gsave
                    initmatrix
                    1 setlinewidth 40 40 moveto 517 0 rlineto 0 760 rlineto -517 0 rlineto 0 -760 rlineto  stroke
                    0.5 setlinewidth 45 45 moveto 507 0 rlineto 0 750 rlineto -507 0 rlineto 0 -750 rlineto  stroke
                    grestore" }
                 \line { 
                       \vspace #7
                        \hspace #-1.45 %for some reason the column is centered without this
                        \column {
                          \fill-line { \scale #'(4 . 4) \center-column{\line{\large {$title}
}\line \tiny {$composer
}\line \teeny \italic {$opus}
} }
                          \vspace #1
                          \fill-line { \postscript #"-20 0 moveto 40 0 rlineto stroke" }
                          \vspace #2
                          \fill-line { \center-column {\line { $arranger}}}
                          \vspace #2
                            }
                          }
                        }
ADDTEXT
) > "${LyFile}"

echo "Title Page stored in " $LyFile
echo "Working directory is " `pwd`

(IFS=$'\n'; for f in `ls -v *.denemo.gz` ; do denemo -n -i "${ScmFile}" $f ; done )
lilypond ${inc} -o ./DenemoPDFoutput/AAAA ./DenemoPDFoutput/DenemoBookTitle.ly

fi

(IFS=$'\n'; for f in `ls -v *.denemo.gz` ; do denemo -n -a "(d-SelectFirstCustomLayout)(d-ExportPDF \"\
"`pwd`/DenemoPDFoutput/"$f-Ascore.pdf\")\
(d-MoveToMovementBeginning) (ForAllMovements \"(while (d-StaffDown))(d-HideFiguredBass)\")\
(let loop () \
(d-LilyPondForPart)(d-ExportPDF (string-append \"\
"`pwd`/DenemoPDFoutput/"$f-B\"(number->string (d-GetStaff))\"part.pdf\"))\
(if (d-StaffDown)  (begin \
				(DenemoSpacingParams \"SystemSystemSpacing\"  \"system-system-spacing\" 'stretchability 1)\
			    (DenemoSpacingParams  \"MarkupSystemSpacing\" \"markup-system-spacing\" 'stretchability 1)\
			    (ForAllMovements \"(d-MovementPageBreak)\")\
				(loop))))" $f ; done )
(IFS=$'\n'; /usr/bin/gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="${ident}DenemoBook.pdf" `ls -v DenemoPDFoutput/*.pdf` )

rm -rf DenemoPDFoutput
