#!/bin/bash
# Check for proper arguments. If the arguments aren't valid print the usage.
if [ -z "$2" ] || [ "$2" == "-n" ] || [ "$2" == "-a" ] || [ "$2" == "-t" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then

    echo "Pushes and runs a Connect IQ executable on the Connect IQ simulator. The simulator must be running for this command to succeed."
    echo
    echo "Usage: monkeydo executable device_id [-n] [-a additional_file] [-t | -t test_name]"
    echo "    executable - A Connect IQ executable to run."
    echo "    device_id  - The device to simulate."
    echo "    n          - When provided, the app will be run in Native Pairing mode."
    echo "    additional_file - When providing the additionalFile flag you must specify one or more additional files to send to the simulator in the form of '<source>:<dest>'."
    echo "    test_name  - When providing the test flag you may specify a specific test name."

else

    # Set the required variables
    MB_HOME="$( cd "$( dirname "$0" )" && pwd )"
    PRG_PATH="$1"
    FILE=`echo $(basename "$PRG_PATH" | tr '[:lower:]' '[:upper:]')`
    DEVICE_ID="$2"

    # Create variables for the optional arguments
    TEST_NAME=""
    TEST_FLAG=""
    ADDITIONAL_FILE_FLAG=""
    ADDITIONAL_FILE=""
    RUN_NATIVE_PAIRING_FLAG=""

    # Set the optional arguments
    if [ ! -z "$3" ]; then
        # Get the run native pairing argument
        if [ "$3" == "-n"  ]; then
            RUN_NATIVE_PAIRING_FLAG="-n"
            ADDITIONAL_FILE_FLAG=""
            TEST_FLAG=""
        # Get the addtional file arguments
        elif [ "$3" == "-a"  ]; then
            ADDITIONAL_FILE_FLAG="-a"
            index=4
            numberInputs=$#
            while [ $index -le $numberInputs ];
            do
               if [ "$4" == "-t" ]; then
                   TEST_FLAG="-t"
                   shift
                   shift
                   shift
                   shift
                   TEST_NAME="$@"
                   break;
                else
                   ADDITIONAL_FILE="${ADDITIONAL_FILE} $4"
                   let "index=index+1"
                   shift
                fi
            done
        # Get the test arguments
        elif [ "$3" == "-t"  ]; then
            TEST_FLAG="-t"
            if [ ! -z "$4" ]; then
                shift
                shift
                shift
                TEST_NAME="$@"
            fi
        else
            echo "ILLEGAL ARGUMENT: $3"
        fi
    fi

    # Push the executable to the simulator
    java -classpath "$MB_HOME"/monkeybrains.jar com.garmin.monkeybrains.monkeydodeux.MonkeyDoDeux -f "$PRG_PATH" -d $DEVICE_ID -s "$MB_HOME"/shell $RUN_NATIVE_PAIRING_FLAG $ADDITIONAL_FILE_FLAG $ADDITIONAL_FILE $TEST_FLAG $TEST_NAME
fi
