#!/bin/bash

. usage.sh

#USAGE : faustremote <servurl> ==> <targets>
#USAGE : faustremote [<servurl>] <platform> <arch> <srcfile> ==> <binary.zip>

URL="34.76.156.75"

function targetrequest {
    curl  "$1/targets"
}

function compilation {
    curl -F"file=@\"$4\";filename=\"$4\""  "$1/compile/$2/$3/binary.zip" --output binary.zip
}


if [ "$1" == "-h" ]; then
    echo "Access the remote complation service to compile DSP programs"
    echo "Usage: faustremote [<servurl>] [<platform> <arch> <srcfile>]"
    echo "faustremote <servurl> ==> <targets>"
    echo "faustremote <servurl> <platform> <arch> <srcfile> ==> <binary.zip>"
    echo "When no <servurl> is defined, the default GRAME Faust URL service is used"
elif [ "$#" == "0" ]; then
    targetrequest "$URL"
elif [ "$#" == "1" ]; then
    targetrequest "$1"
elif [ "$#" == "3" ]; then
    if [ -f "$3" ]; then
        rm -rf binary.zip
        compilation $URL $1 $2 $3
    else 
        echo "ERROR: file $3 doesn't exist"
        exit 1
    fi
elif [ "$#" == "4" ]; then
    if [ -f "$4" ]; then
        rm -rf binary.zip
        compilation $1 $2 $3 $4
    else 
        echo "ERROR: file $4 doesn't exist"
        exit 1
    fi
fi 

