#! /bin/bash -e

#####################################################################
#                                                                   #
#           Compiles Faust programs to Dplug plugin                 #
#           (c) Grame, 2020                                         #
#                                                                   #
#####################################################################

. faustpath
. faustoptflags
. usage.sh

DEBUG=false

# MIDI not yet supported in this file.  Needs to be connected in minimal-dplug.d architecture file
IS_MIDI="0"
VENDOR="Faust"
VENDORID="FaUs"
PLUGINID="PlUg"
HOMEPAGE="https://faust.grame.fr/"
EFFECTTYPE="effectOther"

while [ $1 ]
do
    p=$1

    if [ $p = "--help" ] || [ $p = "-help" ] || [ $p = "-h" ]; then
        usage faust2dplug "[options] [Faust options] <file.dsp>"
        require Dplug
        echo "Compiles Faust programs to Dplug plugins."
        option
        options "-vendor <My Company Name>"
        options "-vendorid <FaUs>"
        options "-pluginid <PlUg>"
        options "-homepage <https://faust.grame.fr/>"
        options "-effectType <effectDynamics>"
        exit
    fi

    if [ "$p" = -debug ]; then
    	DEBUG=true
    elif [ $p = "-midi" ]; then
        IS_MIDI="1"
    elif [ $p = "-vendor" ]; then
        shift
        VENDOR=$1
    elif [ $p = "-vendorid" ]; then
        shift
        VENDORID=$1
    elif [ $p = "-pluginid" ]; then
        shift
        PLUGINID=$1
    elif [ $p = "-homepage" ]; then
        shift
        HOMEPAGE=$1
    elif [ $p = "-effectType" ]; then
        shift
        EFFECTTYPE=$1
	elif [[ -f "$p" ]]; then
	    FILES="$FILES $p"
    else
	    OPTIONS="$OPTIONS $p"    
    fi

shift

done

if [ $IS_MIDI -eq 1 ]; then
    echo "MIDI not currently supported in Dplug architecture file."
    exit
fi

DUBFILE="dub.json"
PLUGFILE="plugin.json"
    
for p in $FILES; do
    CUR=$(pwd)
    f=$(basename "$p")
    SRCDIR=$(dirname "$p")

    # creates the dir 
    dspName="${f%.dsp}"
    SUB_TYPE=$(shasum $p)
    SUB_TYPE=${SUB_TYPE:0:4}
    rm -rf "$SRCDIR/$dspName"
    mkdir $SRCDIR/$dspName
    mkdir $SRCDIR/$dspName/source

    sed -e"s?@vendor@?$VENDOR?g" -e"s?@vendorid@?$VENDORID?g"  -e"s?@name@?$dspName?g" -e"s?@plugid@?$PLUGINID?g" -e"s?@homepage@?$HOMEPAGE?g" -e"s?@effecttype@?$EFFECTTYPE?g" > $SRCDIR/$dspName/$PLUGFILE <<EOF
{
    "\$schema": "https://raw.githubusercontent.com/AuburnSounds/dplug/master/plugin-schema.json",

    "vendorName": "@vendor@",
    "vendorUniqueID": "@vendorid@",
    "vendorSupportEmail": "support@wittyaudio.com",
    "pluginName": "@name@",
    "pluginHomepage": "@homepage@",
    "pluginUniqueID": "@plugid@",
    "publicVersion": "1.0.0",
    "CFBundleIdentifierPrefix": "com.@vendorid@",
    "hasGUI": false,
    "isSynth": false,
    "receivesMIDI": false,
    "category": "@effecttype@"
}
EOF

    sed -e"s?@name@?$dspName?g" > $SRCDIR/$dspName/$DUBFILE <<EOF
{
    "\$schema": "https://raw.githubusercontent.com/Pure-D/code-d/master/json-validation/dub.schema.json",

    "name": "@name@",

    "license": "public domain",
    "importPaths": [ "." ],
    "sourcePaths": [ "." ],
    "stringImportPaths": ["gfx", "fonts", "."],

    "copyright": "none",

    "dflags-linux-dmd": ["-defaultlib=libphobos2.a"],
    "dflags-osx-ldc": ["-static"],
    "dflags-linux-ldc": ["-link-defaultlib-shared=false"],
    "dflags-linux-x86_64-ldc": ["-fvisibility=hidden"],
    "dflags-windows-ldc": ["-mscrtlib=libcmt"],

    "comment-WARNING-READ-THIS-IS-IMPORTANT": [
        "    When making your own plug-in you have to CHANGE THESE DEPENDENCY    ",
        "    SPECIFICATIONS below from path-based to ~>MAJOR.MINOR               ",
        "      Example: ~>7.0                                                    ",
        "    See also the DUB documentation:                                     ",
        "         https://code.dlang.org/package-format?lang=json#version-specs  "],
    "dependencies":
    {
        "dplug:dsp": "~>10",
        "dplug:vst3": "~>10",
        "dplug:vst": "~>10",
        "dplug:au": "~>10",
        "dplug:lv2": "~>10",
        "dplug:gui": "~>10",
        "dplug:pbr-widgets": "~>10"
    },

    "versions": ["futureMouseOver", "futurePBRNormals"],

    "configurations": [
        {
            "name": "VST3",
            "versions": ["VST3"],
            "targetType": "dynamicLibrary",
            "lflags-osx-ldc": [ "-exported_symbols_list", "module-vst3.lst", "-dead_strip" ]
        },
        {
            "name": "VST",
            "versions": ["VST"],
            "targetType": "dynamicLibrary",
            "lflags-osx-ldc": [ "-exported_symbols_list", "module-vst.lst", "-dead_strip" ]
        },
        {
            "name": "AU",
            "versions": ["AU"],
            "targetType": "dynamicLibrary",
            "lflags-osx-ldc": [ "-exported_symbols_list", "module-au.lst", "-dead_strip" ]
        },
        {
            "name": "LV2",
            "versions": ["LV2"],
            "targetType": "dynamicLibrary",
            "lflags-osx-ldc": [ "-exported_symbols_list", "module-lv2.lst", "-dead_strip" ]
        }
    ]
}
EOF

    cat > $SRCDIR/$dspName/module-au.lst << EndOfFile
_dplugAUEntryPoint
_dplugAUComponentFactoryFunction

EndOfFile

    cat > $SRCDIR/$dspName/module-lv2.lst << EndOfFile
_GenerateManifestFromClient
_lv2_descriptor
_lv2ui_descriptor

EndOfFile

    cat > $SRCDIR/$dspName/module-vst3.lst << EndOfFile
_InitDll
_ExitDll
_GetPluginFactory
_bundleEntry
_bundleExit

EndOfFile

    faust -i -lang dlang -a $FAUSTARCH/minimal-dplug.d $OPTIONS "$SRCDIR/$f" -o "$SRCDIR/$dspName/source/main.d"

done