#!/bin/bash

MSCORE=../../build/mscore/mscore

echo "------------------------------"
echo "Regression Tests for MuseScore"
echo "------------------------------"
echo
$MSCORE -v
echo

testcount=0
failures=0

rwtest() {
      echo -n "testing load/save $1";
      $MSCORE $1 -o mops.mscx &> /dev/null
      if diff -q $1 mops.mscx &> /dev/null; then
            echo -e "\r\t\t\t\t\t\t...OK";
      else
            echo -e "\r\t\t\t\t\t\t...FAILED";
            failures=$(($failures+1));
            echo "+++++++++DIFF++++++++++++++"
            diff $1 mops.mscx
            echo "+++++++++++++++++++++++++++"
      fi
      rm mops.mscx
      testcount=$(($testcount+1))
      }

rwtestBww() {
      echo -n "testing load/save $1";
      REF=bwwrefs/`basename $1 .bww`.xml
      $MSCORE ../bww2mxml/test/$1 -d -o mops.xml &> /dev/null
      if diff -q $REF mops.xml &> /dev/null; then
            echo -e "\r\t\t\t\t\t\t...OK";
      else
            echo -e "\r\t\t\t\t\t\t...FAILED";
            failures=$(($failures+1));
            echo "+++++++++DIFF++++++++++++++"
            diff $REF mops.xml
            echo "+++++++++++++++++++++++++++"
      fi
      rm mops.xml
      testcount=$(($testcount+1))
      }

rwtestXml() {
      echo -n "testing load/save $1";
      $MSCORE $1 -d -o mops.xml &> /dev/null
      if diff -q $1 mops.xml &> /dev/null; then
            echo -e "\r\t\t\t\t\t\t...OK";
      else
            echo -e "\r\t\t\t\t\t\t...FAILED";
            failures=$(($failures+1));
            echo "+++++++++DIFF++++++++++++++"
            diff $1 mops.xml
            echo "+++++++++++++++++++++++++++"
      fi
      rm mops.xml
      testcount=$(($testcount+1))
      }

rwtestXmlRef() {
      echo -n "testing load/save $1";
      REF=`basename $1 .xml`_ref.xml
      $MSCORE $1 -d -o mops.xml &> /dev/null
      if diff -q $REF mops.xml &> /dev/null; then
            echo -e "\r\t\t\t\t\t\t...OK";
      else
            echo -e "\r\t\t\t\t\t\t...FAILED";
            failures=$(($failures+1));
            echo "+++++++++DIFF++++++++++++++"
            diff $REF mops.xml
            echo "+++++++++++++++++++++++++++"
      fi
      rm mops.xml
      testcount=$(($testcount+1))
      }

rwtestAllBww() {
      rwtestBww testBeams.bww
      rwtestBww testDuration.bww
      rwtestBww testGraces.bww
      rwtestBww testHello.bww
      rwtestBww testMidMeasureRepeat.bww
      rwtestBww testNotes.bww
      rwtestBww testNoTimeSig1.bww
      rwtestBww testNoTimeSig2.bww
      rwtestBww testRepeats.bww
      rwtestBww testTempo60.bww
      rwtestBww testTempo120.bww
      rwtestBww testTieTriplet.bww
      rwtestBww testTriplets.bww
      }

rwtestAllDemos() {
      rwtest ../demos/promenade.mscx
      rwtest ../demos/adeste.mscx
      rwtest ../demos/inv10.mscx
      rwtest ../demos/inv13.mscx
      rwtest ../demos/inv1.mscx
      rwtest ../demos/inv6.mscx
      rwtest ../demos/praeludium1.mscx
      rwtest ../demos/prelude.mscx
      rwtest ../demos/scales.mscx
      rwtest ../demos/sonata16.mscx
      rwtest ../demos/bach-bc2.mscx
      }

rwtestAllMsc() {
      rwtest accidental1.msc
      rwtest accidental2.msc
      rwtest chromatic.msc
      rwtest coda.msc
      rwtest crossbeams.msc
      rwtest RepeatTest6.msc
      rwtest RepeatTest7.msc
      rwtest scales1.msc
      rwtest slur2.msc
      rwtest test1.msc
      rwtest test2.msc
      rwtest test7.msc
      rwtest testKeysig2.msc
      rwtest testsmall.msc
      rwtest testTimesig2.msc
      rwtest tuplets.msc
      }

rwtestAllXml() {
      rwtestXml testAccidentals1.xml
      rwtestXml testAccidentals2.xml
      rwtestXml testArpGliss1.xml
      rwtestXml testArpGliss2.xml
      rwtestXml testBarStyles.xml
      rwtestXml testClefs1.xml
      rwtestXml testDalSegno.xml
      rwtestXml testDCalCoda.xml
      rwtestXml testDCalFine.xml
      rwtestXml testDirections1.xml
      rwtestXml testDynamics1.xml
      rwtestXml testDynamics2.xml
      rwtestXmlRef testDynamics3.xml
      rwtestXml testGrace1.xml
      rwtestXml testGrace2.xml
      rwtestXml testHarmony1.xml
      rwtestXml testHarmony2.xml
      rwtestXml testHello.xml
      rwtestXml testImplicitMeasure1.xml
      rwtestXml testInvisibleElements.xml
      rwtestXml testKeysig1.xml
      rwtestXml testLines1.xml
      rwtestXml testLines2.xml
      rwtestXml testLyricsVoice2a.xml
      rwtestXmlRef testLyricsVoice2b.xml
      rwtestXml testMetaData.xml
      rwtestXml testMultiMeasureRest.xml
      rwtestXml testNoteAttributes1.xml
      rwtestXml testNoteAttributes2.xml
      rwtestXml testNoteheads.xml
      rwtestXml testNotesRests1.xml
      rwtestXml testNotesRests2.xml
      rwtestXmlRef testPartsSpecialCases.xml
      rwtestXml testSystemBrackets1.xml
      rwtestXml testSystemBrackets2.xml
      rwtestXml testTempo1.xml
      rwtestXml testTimesig1.xml
      rwtestXml testTimesig3.xml
      rwtestXml testTremolo.xml
      rwtestXml testTuplets.xml
      rwtestXmlRef testVoiceMapper1.xml
      rwtestXml testVoicePiano1.xml
      rwtestXml testVolta1.xml
      rwtestXml testWedge1.xml
      rwtestXml testWedge2.xml
      rwtestXml testWords1.xml
      }

usage() {
      echo "usage: $0 [bww | demos | msc | xml]"
      echo "or: $0 [bww | msc | xml] <file>"
      echo
      exit 1
      }

if [ $# -eq 0 ]; then
      rwtestAllBww
      rwtestAllDemos
      rwtestAllMsc
      rwtestAllXml
elif [ $# -eq 1 ]; then
      if [ "$1" == "bww" ]; then
            rwtestAllBww
      elif [ "$1" == "demos" ]; then
            rwtestAllDemos
      elif [ "$1" == "msc" ]; then
            rwtestAllMsc
      elif [ "$1" == "xml" ]; then
            rwtestAllXml
      else
            usage
      fi
elif [ $# -eq 2 ]; then
      if [ "$1" == "bww" ]; then
            rwtest $2
      elif [ "$1" == "msc" ]; then
            rwtest $2
      elif [ "$1" == "xml" ]; then
            rwtestXml $2
      else
            usage
      fi
elif [ $# -ge 3 ]; then
      usage
fi

echo
echo "$testcount test(s), $failures failure(s)"
