#!/bin/bash

faust --version
echo location $(which faust)

# detect the current system 
system=$(uname -s)
# normalizes MINGW versions
system=$(echo $system | grep MINGW > /dev/null && echo MINGW || echo $system)

## exclusion list for faust errors
XLIST1="faust2md"

OSXSOUND="faust2android faust2caqt faust2jaqt faust2jack faust2ios faust2max6 faust2msp faust2jackconsole faust2juce faust2bela"
OSXSOUNDSR="faust2caqt faust2jaqt faust2jack"
OSX="faust2android faust2au faust2caqt faust2csound faust2faustvst faust2jack faust2jaqt faust2w32max6 faust2w64max6 faust2max6 faust2msp faust2puredata faust2supercollider faust2unity faust2vst faust2vsti faust2w32vst faust2w64vst faust2ios faust2netjackconsole faust2netjackqt faust2jackconsole faust2lv2 faust2sndfile faust2csvplot faust2plot"
OSXMIDI="faust2caqt faust2jack faust2jaqt faust2max6 faust2msp faust2ios faust2netjackconsole faust2netjackqt faust2jackconsole"
OSXPOLY="faust2caqt faust2jack faust2jaqt faust2max6 faust2msp faust2ios faust2juce faust2netjackconsole faust2netjackqt faust2jackconsole faust2lv2"
OSXPOLYMIDI="faust2caqt faust2jack faust2jaqt faust2max6 faust2msp faust2ios faust2juce faust2netjackconsole faust2netjackqt faust2jackconsole"
OSXPOLY2MIDI="faust2caqt faust2ios faust2max6 faust2msp faust2juce faust2netjackconsole faust2netjackqt"
OSXHTTPD="faust2caqt faust2jack faust2jaqt faust2jackconsole faust2netjackconsole faust2netjackqt faust2jackconsole"
OSXOSC="faust2ios faust2caqt faust2jack faust2jaqt faust2jackconsole faust2netjackconsole faust2netjackqt faust2jackconsole faust2max6 faust2msp faust2android"
OSXOSCHTTPD="faust2caqt faust2jack faust2jaqt faust2jackconsole faust2netjackconsole faust2netjackqt faust2jackconsole"
OSXPRESET="faust2caqt faust2jack faust2jaqt"

LINUX="faust2alqt faust2alsa faust2alsaconsole faust2android faust2asmjs faust2bela faust2csound faust2dssi faust2eps faust2faustvst faust2firefox faust2graph faust2graphviewer faust2jack faust2jackinternal faust2jackserver faust2jaqt faust2ladspa faust2lv2 faust2mathdoc  faust2md  faust2netjackconsole faust2netjackqt faust2octave faust2owl faust2paqt faust2pdf faust2plot faust2png faust2puredata faust2raqt faust2ros faust2rosgtk faust2rpialsaconsole faust2rpinetjackconsole faust2sig faust2sigviewer faust2supercollider faust2svg faust2w32max6 faust2w32msp faust2w32puredata faust2w32vst faust2w64vst faust2netjackconsole faust2netjackqt faust2jackconsole"
LINUXMIDI="faust2jaqt faust2netjackconsole faust2netjackqt faust2jackconsole"
LINUXPOLY="faust2jaqt faust2juce faust2netjackconsole faust2netjackqt faust2jackconsole"
LINUXPOLYMIDI="faust2jaqt faust2juce faust2netjackconsole faust2netjackqt faust2jackconsole"
LINUXOSCHTTPD="faust2jaqt faust2netjackconsole faust2netjackqt faust2jackconsole"

## test exclusion : notinlist "toto" "a b c d e"
notinlist() {
  for word in $2; do
    if [ $word = $1 ]; then
        return 1
    fi
  done
  return 0
}

echo "------------------------"
echo Run tests for $system
echo "------------------------"

if [ "$system" = "Darwin" ]; then
    echo "Test $OSX"
    echo "Test -midi $OSXMIDI"
    echo "Test -nvoices 8 $OSXPOLY"
    echo "Test -nvoices 8 -midi $OSXPOLYMIDI"
    echo "Test -nvoices 8 -effect <foo.dsp> -midi $OSXPOLY2MIDI"
    echo "Test -soundfile $OSXSOUND"
    echo "Test -soundfile -resample $OSXSOUNDSR"
    echo "Test -httpd $OSXHTTPD"
    echo "Test -osc $OSXOSC"
    echo "Test -osc -httpd $OSXOSCHTTPD"
    echo "Test -preset auto $OSXPRESET"
  
    faust2dummy good.dsp > /dev/null
    ./good > good.txt
    diff good.txt good_ref.txt && echo "OK: faust2dummy good.dsp succeeded !" || echo "ERROR: faust2dummy good.dsp failed"

    faust2dummy -midi good.dsp > /dev/null
    ./good > good_midi.txt
    diff good_midi.txt good_midi_ref.txt && echo "OK: faust2dummy -midi good.dsp succeeded !" || echo "ERROR: faust2dummy -midi good.dsp failed"

    faust2dummy -midi -nvoices 2 organ.dsp > /dev/null
    ./organ > organ_2voices.txt
    diff organ_2voices.txt organ_2voices_ref.txt && echo "OK: faust2dummy -midi -nvoices 2 organ.dsp succeeded !" || echo "ERROR: faust2dummy -midi -nvoices 2 organ.dsp failed"

    faust2dummy -midi organ.dsp > /dev/null
    ./organ > organ_8voices.txt
    diff organ_8voices.txt organ_8voices_ref.txt && echo "OK: faust2dummy -midi organ.dsp succeeded !" || echo "ERROR: faust2dummy -midi organ.dsp failed"

    faust2dummymem waveform4.dsp > /dev/null
    ./waveform4 > waveform4.txt
    diff waveform4.txt waveform4_ref.txt && echo "OK: faust2dummymem waveform4.dsp succeeded !" || echo "ERROR: faust2dummymem waveform4.dsp failed"

	for scriptname in $OSX; do
    	($scriptname good.dsp &> LOG)&&(echo "OK: $scriptname succeeded !")||(echo "ERROR: $scriptname failed")
 	done

    for scriptname in $OSXMIDI; do
        ($scriptname -midi good.dsp &> LOG)&&(echo "OK: $scriptname -midi succeeded !")||(echo "ERROR: $scriptname -midi failed")
    done

    for scriptname in $OSXPOLY; do
        ($scriptname -nvoices 8 good.dsp &> LOG)&&(echo "OK: $scriptname -nvoices 8 succeeded !")||(echo "ERROR: $scriptname -nvoices 8 failed")
        rm -rf good
    done

    for scriptname in $OSXPOLYMIDI; do
        ($scriptname -nvoices 8 -midi good.dsp &> LOG)&&(echo "OK: $scriptname -nvoices 8 -midi succeeded !")||(echo "ERROR: $scriptname -nvoices 8 -midi failed")
        rm -rf good
    done

    for scriptname in $OSXPOLY2MIDI; do
        ($scriptname -nvoices 8 -effect good.dsp -midi good.dsp &> LOG)&&(echo "OK: $scriptname -nvoices 8 -effect good.dsp -midi succeeded !")||(echo "ERROR: $scriptname -nvoices 8 -effect good.dsp -midi failed")
        rm -rf good
    done
    
    for scriptname in $OSXSOUND; do
        ($scriptname -soundfile -lang ocpp sound.dsp &> LOG)&&(echo "OK: $scriptname -soundfile -lang ocpp succeeded !")||(echo "ERROR: $scriptname -soundfile -lang ocpp failed")
        rm -rf good
    done

    # Remove sound folder
    rm -rf sound
    
    for scriptname in $OSXSOUND; do
        ($scriptname -soundfile sound.dsp &> LOG)&&(echo "OK: $scriptname -soundfile succeeded !")||(echo "ERROR: $scriptname -soundfile failed")
        rm -rf good
    done
    
    # Remove sound folder
    rm -rf sound
    
    for scriptname in $OSXSOUNDSR; do
        ($scriptname -soundfile -resample sound.dsp &> LOG)&&(echo "OK: $scriptname -soundfile -resample succeeded !")||(echo "ERROR: $scriptname -soundfile -resample failed")
        rm -rf good
    done
    
    (faust2juce -soundfile -standalone sound.dsp &> LOG)&&(echo "OK: faust2juce -soundfile -standalone succeeded !")||(echo "ERROR: faust2juce -soundfile -standalone failed")
    rm -rf good

    for scriptname in $OSXHTTPD; do
        ($scriptname -httpd good.dsp &> LOG)&&(echo "OK: $scriptname -httpd succeeded !")||(echo "ERROR: $scriptname -httpd failed")
    done

    for scriptname in $OSXOSC; do
        ($scriptname -osc good.dsp &> LOG)&&(echo "OK: $scriptname -osc succeeded !")||(echo "ERROR: $scriptname -osc failed")
    done

    for scriptname in $OSXOSCHTTPD; do
        ($scriptname -osc -httpd good.dsp &> LOG)&&(echo "OK: $scriptname -osc -httpd succeeded !")||(echo "ERROR: $scriptname -osc -httpd failed")
    done
    
    for scriptname in $OSXPRESET; do
        ($scriptname -preset auto good.dsp &> LOG)&&(echo "OK: $scriptname -preset auto succeeded !")||(echo "ERROR: $scriptname -preset auto failed")
    done
    
    (faust2api -jack good.dsp &> LOG && unzip dsp-faust.zip && cd dsp-faust && c++ -std=c++11 DspFaust.cpp -DBUILD -ljack -o DspFaust)&&(echo "OK: faust2api -jack succeeded !")||(echo "ERROR: faust2api -jack failed")
    
    (faust2api -jack -midi -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2api -jack -midi -nvoices 8 succeeded !")||(echo "ERROR: faust2api -jack -midi -nvoices 8 failed")

    (faust2api -jack -nvoices 8 -effect effect.dsp -midi good.dsp &> LOG)&&(echo "OK: faust2api -jack -nvoices 8 -effect effect.dsp -midi succeeded !")||(echo "ERROR: faust2api -jack -nvoices 8 -effect effect.dsp -midi failed")

    (faust2api -jack -soundfile -lang ocpp sound.dsp &> LOG)&&(echo "OK: faust2api -jack -soundfile -lang ocpp succeeded !")||(echo "ERROR: faust2api -jack -soundfile -lang ocpp failed")

    (faust2nodejs -coreaudio good.dsp &> LOG)&&(echo "OK: faust2nodejs -coreaudio succeeded !")||(echo "ERROR: faust2nodejs -coreaudio failed")
	rm -rf good-faustnode
	
    (faust2nodejs -coreaudio -midi good.dsp &> LOG)&&(echo "OK: faust2nodejs -coreaudio -midi succeeded !")||(echo "ERROR: faust2nodejs -coreaudio -midi failed")
	rm -rf good-faustnode
	
    (faust2nodejs -coreaudio -midi -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2nodejs -coreaudio -midi -nvoices 8 succeeded !")||(echo "ERROR: faust2nodejs -coreaudio -midi -nvoices 8 failed")
	rm -rf good-faustnode
	
    (faust2nodejs -coreaudio -nvoices 8 -effect effect.dsp -midi good.dsp &> LOG)&&(echo "OK: faust2nodejs -coreaudio -nvoices 8 -effect effect.dsp -midi succeeded !")||(echo "ERROR: faust2nodejs -coreaudio -nvoices 8 -effect effect.dsp -midi failed")
	rm -rf good-faustnode
	
    (faust2smartkeyb -android good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -android succeeded !")||(echo "ERROR: faust2smartkeyb -android failed")

    (faust2smartkeyb -android -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -android -midi -nvoices 8 succeeded !")||(echo "ERROR: faust2smartkeyb -android -midi -nvoices 8 failed")

    (faust2smartkeyb -android -effect effect.dsp -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -android -effect effect.dsp -nvoices 8 succeeded !")||(echo "ERROR: faust2smartkeyb -android -effect effect.dsp -nvoices 8 failed")

    (faust2smartkeyb -ios good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -ios succeeded !")||(echo "ERROR: faust2smartkeyb -ios failed")

    (faust2smartkeyb -ios -osc good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -ios -osc succeeded !")||(echo "ERROR: faust2smartkeyb -ios -osc failed")

    (faust2smartkeyb -ios -osc -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -ios -osc -nvoices 8 succeeded !")||(echo "ERROR: faust2smartkeyb -ios -osc -nvoices 8 failed")

    (faust2smartkeyb -ios -effect effect.dsp -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2smartkeyb -ios -effect effect.dsp -nvoices 8 succeeded !")||(echo "ERROR: faust2smartkeyb -ios -effect effect.dsp -nvoices 8 failed")

    # Test of -exp10
    (faust2jack -exp10 exp10.dsp) &&(echo "OK: faust2jack -exp10 succeeded !")||(echo "ERROR: faust2jack -exp10 failed")

    (faust2android -exp10 exp10.dsp)&&(echo "WARNING: faust2android -exp10 succeeded ?")||(echo "OK: faust2android -exp10 correctly failed !")

elif [ "$system" = "Linux" ]; then
    echo "Test $LINUX"
    echo "Test -midi $LINUXMIDI"
    echo "Test -nvoices 8 $LINUXPOLY"
    echo "Test -nvoices 8 -midi $LINUXPOLYMIDI"
    echo "Test -osc -httpd $LINUXOSCHTTPD"

    faust2dummy good.dsp > /dev/null
    ./good > good.txt
    diff good.txt good_ref.txt && echo "OK: faust2dummy good.dsp succeeded !" || echo "ERROR: faust2dummy good.dsp failed"

    faust2dummy -midi good.dsp > /dev/null
    ./good > good.txt
    diff good.txt good_ref.txt && echo "OK: faust2dummy -midi good.dsp succeeded !" || echo "ERROR: faust2dummy -midi good.dsp failed"

    faust2dummy -midi -nvoices 2 organ.dsp > /dev/null
    ./organ > organ_2voices.txt
    diff organ_2voices.txt organ_2voices_ref.txt && echo "OK: faust2dummy -midi -nvoices 2 organ.dsp succeeded !" || echo "ERROR: faust2dummy -midi -nvoices 2 organ.dsp failed"

    faust2dummy -midi organ.dsp > /dev/null
    ./organ > organ_8voices.txt
    diff organ_8voices.txt organ_8voices_ref.txt && echo "OK: faust2dummy -midi organ.dsp succeeded !" || echo "ERROR: faust2dummy -midi organ.dsp failed"

    faust2dummymem waveform4.dsp > /dev/null
    ./waveform4 > waveform4.txt
    diff waveform4.txt waveform4_ref.txt && echo "OK: faust2dummymem waveform4.dsp succeeded !" || echo "ERROR: faust2dummy waveform4.dsp failed"

	for scriptname in $LINUX; do
    	($scriptname good.dsp &> LOG)&&(echo "OK: $scriptname succeeded !")||(echo "ERROR: $scriptname failed")
	done

    for scriptname in $LINUXMIDI; do
        ($scriptname -midi good.dsp &> LOG)&&(echo "OK: $scriptname -midi succeeded !")||(echo "ERROR: $scriptname -midi failed")
    done

    for scriptname in $LINUXPOLY; do
        ($scriptname -nvoices 8 good.dsp &> LOG)&&(echo "OK: $scriptname -nvoices 8 succeeded !")||(echo "ERROR: $scriptname -nvoices 8 failed")
        rm -rf good
    done

    for scriptname in $LINUXPOLYMIDI; do
        ($scriptname -nvoices 8 -midi good.dsp &> LOG)&&(echo "OK: $scriptname -nvoices 8 -midi succeeded !")||(echo "ERROR: $scriptname -nvoices 8 -midi failed")
        rm -rf good
    done

    for scriptname in $LINUXOSCHTTPD; do
        ($scriptname -osc -httpd good.dsp &> LOG)&&(echo "OK: $scriptname -osc -httpd succeeded !")||(echo "ERROR: $scriptname -osc -httpd failed")
    done

    (faust2api -jack good.dsp &> LOG && unzip dsp-faust.zip && cd dsp-faust && c++ -std=c++11 DspFaust.cpp -DBUILD -ljack -o DspFaust)&&(echo "OK: faust2api -jack succeeded !")||(echo "ERROR: faust2api -jack failed")

    (faust2api -android good.dsp &> LOG)&&(echo "OK: faust2api -android succeeded !")||(echo "ERROR: faust2api -android failed")

    (faust2api -android -midi good.dsp &> LOG)&&(echo "OK: faust2api -android -midi succeeded !")||(echo "ERROR: faust2api -android -midi failed")

    (faust2api -android -midi -nvoices 8 good.dsp &> LOG)&&(echo "OK: faust2api -android -midi -nvoices 8 succeeded !")||(echo "ERROR: faust2api -android -midi -nvoices 8 failed")

    (faust2api -android -nvoices 8 -effect effect.dsp -midi good.dsp &> LOG)&&(echo "OK: faust2api -android -nvoices 8 -effect effect.dsp -midi succeeded !")||(echo "ERROR: faust2api -android -nvoices 8 -effect effect.dsp -midi failed")

elif [ "$system" = "MINGW" ]; then
	echo "not yet implemented"

else
	echo
	echo "Test all Faust2xxx to work"
	echo
	for S in ../../tools/faust2appls/faust2*; do
		scriptname=`basename $S .in`
		if notinlist  $S "$XLIST1"; then
			($scriptname good.dsp &> LOG)&&(echo "OK: $scriptname succeeded !")||(echo "ERROR: $scriptname failed")
		fi
	done
fi
