#!/bin/bash

#
#     render MusicXml files with lilypond and mscore
#     and put up a webpage for every testfile
#     webpages are created in subdirectory "html"
#     startpage is html/index.html
#
#     requires ImageMagick ("convert")
#

echo "Render test for MuseScore"

TESTDIR=html
testfiles="
00a-Basics-Pitches
00b-Basics-Intervals
00c-Basics-Durations
00d-Basics-RestDurations
00e-Basics-PitchedRests-PJB
00f-Basics-Clefs
00g-Basics-Keys
00h-Basics-TimeSignatures
00i-Basics-NoKeyOrClef
00j-Basics-NoTime-PJB
00k-Basics-Backup
00l-Basics-MultiMeasureRests
00l-Basics-Tie-PJB
01a-Chord-Rosegarden
01b-Chords-Rosegarden
01c-Chords-Rosegarden
01d-Chords-SchubertStabatMater
01e-Chords-PickupMeasures
02a-Notations-MusicXML
02b-Articulations-Texts-PJB
02c-MultipleNotationChildren-RFK
02d-Arpeggio
03a-Directions-MusicXML
03b-AccordionRegistrations-MusicXML
03c-MetronomeMarks
04a-Spanners-Finale
04b-Glissando
04c-Spanners-Noteedit
04d-Spanners-JScore
04e-OctaveShifts-Finale
05a-HeaderQuotes-Finale
06a-Lyrics-Finale
06b-MultipleLyrics-Finale
06c-Lyrics-Pianostaff-Finale
06d-Lyrics-Melisma-Finale
06e-Lyrics-Chords-Finale
06f-Lyrics-GracedNotes-Finale
06g-Lyrics-NameNumber
06h-Lyrics-BeamsMelismata
08a-Partorder-Rosegarden
08b-StaffGroups-Finale
08c-More-than-10-parts-Rosegarden
08d-NestedPartsBrackets-Finale
08e-LinebrokenInstrumentNames-Finale
08f-PianoStaff-PJB
08g-OverlappingPartGroups-Finale
09a-SimpleRepeat-Finale
09b-RepeatWithAlternatives-Finale
09c-Barlines-Finale
09d-RepeatMultipleTimes-Finale
09e-Alternatives-Finale
09f-Repeats-Finale
09g-Endings-Finale
09h-RepeatsNoEndBar-Finale
09i-MidmeasureBarline
09j-Midmeasure-Clef-Finale
09k-Upbeats-ImplicitMeasures-Finale
09l-PickupMeasure-SecondVoiceMessup
10a-TwoVoicesOnStaff-Finale
12a-TripletsDuration-NoBracket-PJB
12b-Tuplets-Finale
13a-GraceNotes-Finale
13b-ChordAsGraceNote-Finale
14a-MultistaffClefDynamics-Finale
14b-DifferentKeysAfterBackup-PJB
14b-DifferentKeys-PJB
14c-StaffChange-Finale
15a-Percussion-Finale
17a-Chords-Finale
17b-Fretboards-Finale
17c-ChordsFrets-Finale
17d-ChordsFretsOnMultistaff-Finale
17e-TabStaves-Finale
17f-AllChordTypes
18a-FiguredBass
19a-PageLayout-PrintMusic
99a-Sibelius5-IgnoreBeaming
99b-Lyrics-BeamsMelismata-IgnoreBeams
"

function genIndex() {
      echo "gen index"

      cd $TESTDIR

      echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" > index.html
      echo "   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"  >> index.html
      echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"  >> index.html
      echo ""  >> index.html
      echo "<body>"  >> index.html
      echo "   <h1>MuseScore Regression Tests</h1>"  >> index.html
      for test in $testfiles; do
            echo -n "   <a href=\"" >> index.html
            echo -n $test    >> index.html
            echo -n ".html\">"    >> index.html
            echo -n $test    >> index.html
            echo "</a><br>"  >> index.html
            done
      echo "   </body>"  >> index.html
      echo "</html>"  >> index.html
      cd ..
      }

function genHtml() {
      cd $TESTDIR
      echo "gen test $1"
      echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" > $1.html
      echo "   \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"  >> $1.html
      echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"  >> $1.html
      echo ""        >> $1.html
      echo "<body>"  >> $1.html

      echo "<h1>MuseScore Regression Tests</h1>"  >> $1.html
      echo "<h2>"               >> $1.html
      echo -n $1                >> $1.html
      echo -n "</h2>"           >> $1.html
      echo "<hr/>"              >> $1.html
      echo "<h3>Lilypond</h3>"  >> $1.html
      echo "<hr/>"              >> $1.html
      echo -n "<img src=\"lilypond/"  >> $1.html
      echo -n $1.png  >> $1.html
      echo "\"/><br>"  >> $1.html
      echo "<hr/>"  >> $1.html
      echo "<h3>MuseScore</h3>"  >> $1.html
      echo "<hr/>"  >> $1.html
      echo -n "<img src=\"mscore/"  >> $1.html
      echo -n $1.png  >> $1.html
      echo "\"/><br>"  >> $1.html

      echo "   </body>"  >> $1.html
      echo "</html>"  >> $1.html
      cd ..
      }

function genFiles() {
      musicxml2ly $1.xml -o $1.ly
      lilypond --png --output mops $1.ly
      convert -trim mops.png $TESTDIR/lilypond/$1.png
      rm $1.ly
      if [[ -f mops.png ]]; then
            rm mops.png
            fi
      mscore -S xmltest.mss -r 100 $1.xml -o mops.png
      convert -trim mops-1.png $TESTDIR/mscore/$1.png
      genHtml $1
      }

if [[ -n $1 ]]; then
      echo $1
      genFiles $1
      exit;
      fi

if [[ -d $TESTDIR ]]; then
      rm -rf $TESTDIR
      mkdir $TESTDIR
      mkdir $TESTDIR/mscore
      mkdir $TESTDIR/lilypond
      fi

genIndex

for test in $testfiles; do
      echo $test
      genFiles $test
      done

