#!/data/data/com.termux/files/usr/bin/bash -e

#####################################################################
#                                                                   #
#               Compiles Faust programs to a fausgen~ patch         #
#               (c) Grame, 2015-2025                                #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. faustusage.sh

POLY="MONO"
NVOICES=-1
MC="0"
FILES=()

JSFILE_PATH="ui.js"

echoHelp()
{
    status=${1:-0}
    usage faust2gen "[options] <file.dsp>"
    echo "Compiles Faust programs to a fausgen~/mc.faustgen patch"
    option
    echo '   -nvoices <num> : to produce a polyphonic ready patch to be used with MIDI events'
    echo '   -mc : to use a mc.faustgen~ object'
    exit "$status"
}

if [ "$#" -eq 0 ]; then
    echo 'Please, provide a Faust file to process !'
    echo ''
    echoHelp 1
fi

# dispatch command arguments
while [ "$1" ]
do
    p=$1

    if [ "$p" = "-help" ] || [ "$p" = "--help" ] || [ "$p" = "-h" ]; then
       echoHelp
    elif [ "$p" = "-mc" ]; then
        MC="1"
    elif [ "$p" = "-nvoices" ]; then
        shift
        if [[ ! "$1" =~ ^[1-9][0-9]*$ ]]; then
            echo "Invalid voice count '${1:-}' (expected a positive integer)" >&2
            exit 1
        fi
        POLY="POLY1"
        NVOICES=$1
    elif [[ -f "$p" ]] && [ "${p: -4}" = ".dsp" ]; then
        FILES+=("$p")
    else
        echo "Invalid option or DSP file '$p'" >&2
        exit 1
    fi

shift

done

if [ ${#FILES[@]} -eq 0 ]; then
    echo "No DSP file was provided" >&2
    exit 1
fi

#-------------------------------------------------------------------
# compile the *.dsp files
#

for p in "${FILES[@]}"; do

    f=$(basename "$p")

    FILE_POLY=$POLY
    FILE_NVOICES=$NVOICES
    if [ "$FILE_POLY" != "POLY1" ]; then
        DECLARED_NVOICES=$(sed -nE 's/.*declare[[:space:]]+nvoices[[:space:]]+"?([0-9]+)"?[[:space:]]*;.*/\1/p' "$p" | head -n 1)
        if [ -n "$DECLARED_NVOICES" ] && [ "$DECLARED_NVOICES" -gt 0 ]; then
            FILE_POLY="POLY1"
            FILE_NVOICES=$DECLARED_NVOICES
        fi
    fi

    # create Max patch
    if [ "$FILE_POLY" = "POLY1" ]; then
        if [ "$MC" = "1" ]; then
            cat "$FAUSTARCH/max-msp/mc-faustgen-wrapper-poly.maxpat" > "${f%.dsp}-temp1.maxpat"
        else
            cat "$FAUSTARCH/max-msp/faustgen-wrapper-poly.maxpat" > "${f%.dsp}-temp1.maxpat"
        fi
    else
        if [ "$MC" = "1" ]; then
            cat "$FAUSTARCH/max-msp/mc-faustgen-wrapper.maxpat" > "${f%.dsp}-temp1.maxpat"
        else
            cat "$FAUSTARCH/max-msp/faustgen-wrapper.maxpat" > "${f%.dsp}-temp1.maxpat"
        fi
    fi
    sed -e "s/DSP_NAME/${f%.dsp}/g" -e "s/NVOICES/$FILE_NVOICES/g" \
        "${f%.dsp}-temp1.maxpat" > "${f%.dsp}-temp2.maxpat"
    sed -e "s/UI_FILE/$JSFILE_PATH/g" \
        "${f%.dsp}-temp2.maxpat" > "${f%.dsp}.maxpat"

    # collect binary file name for FaustGIDE
    BINARIES="$BINARIES${f%.dsp}.maxpat;"

    # copy JavaScript UI file
    cp "$FAUSTARCH/max-msp/ui.js" .

    rm "${f%.dsp}-temp1.maxpat"
    rm "${f%.dsp}-temp2.maxpat"

done

BINARIES="$BINARIES ui.js"

echo $BINARIES
