#!/bin/bash

SEARDUINO_PATH=__INSTALL_DIR__
export LD_LIBRARY_PATH=__INSTALL_DIR__/lib
TMPL_DIR=${SEARDUINO_PATH}/share/searduino/tmpl
PROJECT_DIR=""
HOME_DIR=${HOME}
SEARDUINO_HOME_DIR=${HOME}/searduino/
ARDUINO_EX2C=${SEARDUINO_PATH}/bin/searduino-arduino-ex2c 
NON_EXIST_MAKEFILE=4
NON_BOARD=5

##

MISSING_DIR=1
NON_EXIST_DIR=2
NON_EXIST_INO=3

exit_on_failure()
{
    if [ $1 -ne 0 ]
    then
	echo "ERROR:  $2"
	exit $1
    fi
}


help_text() {
    echo "searduino-builder - helper tool for Searduino"
    echo ""
    echo "SYNOPSIS"
    echo "  searduino-builder <options>"
    echo ""
    echo "DESCRIPTION"
    echo "  searduino-builder can convert Arduino examples to pure C that you can use with Seaduino. It can build for and upload to Arduino boards and it can create C code (a blink example) from scratch."
    echo ""
    echo "OPTIONS"
    echo "  --ino <FILE>"
    echo "      set the ino file to convert to Searduino"
    echo ""
    echo "  --build"
    echo "      builds code (specified by --ino or --ino-dir)"
    echo ""
    echo "  --upload"
    echo "      uploads code (specified by --ino or --ino-dir)"
    echo ""
    echo "  --ino-dir <DIR>"
    echo "      set the ino file to convert to Searduino files in dir calle"
    echo ""
    echo "  --create <DIR>"
    echo "      create template Searduino files in directory DIR"
    echo ""
    echo "  --board"
    echo "     specify which board to use as default. If not set, stub is set"
    echo ""
    echo "  --help"
    echo "      prints this help text and exists"
    echo ""
}


parse_args()
{
    export BUILD_TYPE=stub

    while [ "$1" != "" ]
    do
	case $1 in
	    "--ino")
		INO_FILE=$2
		shift
		;;
	    "--build")
		PROJECT_DIR=$2
		shift
		;;
	    "--upload")
		UPLOAD=" upload "
		;;
	    "--ino-dir")
		INO_DIR=$2
		shift
		;;
	    "--create")
                CREATE="true"
		CREATE_DIR=$2
		shift
		;;
	    "--board")
		BOARD=$2
		shift
		;;
	    "--help")
                help_text
                exit 0
		;;
	    "--nroff")
                MAN_PAGE=$2
                help_text > /tmp/seard-help.txt
                . ./scripts/man-support
                gen_nroff /tmp/seard-help.txt > $MAN_PAGE
                shift
                exit 0
		;;
	    *)
		PROJECT_DIR="$1"
		break
		;;
	esac	
	shift
    done

    
}

RET=0

parse_args $*

#echo "HOME:        ${HOME_DIR}"
#echo "PROJECT DIR: $PROJECT_DIR"
#echo "INO_FILE:    ${INO_FILE}"
#echo "INO_DIR:     ${INO_DIR}"

MAKE_RULES="clean "
if [ "$BOARD" = "<none>" ] 
then
    echo "Can't handle board: <none>"
    exit $NON_BOARD
fi

if [ "$BOARD" != "" ] && [ "$BOARD" != "stub" ] 
then
    BOARD=$(echo $BOARD | tr [A-Z] [a-z])
    export SEARDUINO_OVERRIDE_ARDUINO=$BOARD
    export SHLIB=""
    MAKE_RULES="$MAKE_RULES all $UPLOAD"
else
    export SEARDUINO_OVERRIDE_ARDUINO=stub
    export SHLIB=""
    MAKE_RULES="$MAKE_RULES shlib"
fi



if [ "$INO_FILE" != "" ]
then
    if [ ! -f $INO_FILE ]
    then
	echo "Ino file ($INO_FILE) does not exist"
	exit $NON_EXIST_INO
    fi
    
    ${ARDUINO_EX2C} --yes --shlib --destination-dir ${SEARDUINO_HOME_DIR} $INO_FILE 2>&1 
    RET=$?

elif [ "$INO_DIR" != "" ]
then
    if [ ! -d $INO_DIR ]
    then
	echo "Directory with ino files ($INO_DIRS) does not exist"
	exit $NON_EXIST_INO
    fi
    
    ${ARDUINO_EX2C} --yes --shlib --destination-dir ${SEARDUINO_HOME_DIR} $INO_DIR 2>&1 
    RET=$?

elif [ "$CREATE" = "true" ]
then
    if [ "$CREATE_DIR" = "" ]
    then
        echo "Missing arguments to --create"
        exit 1
    fi
    ${ARDUINO_EX2C} --yes --shlib --destination-dir ${SEARDUINO_HOME_DIR} --create $CREATE_DIR 2>/dev/null >/dev/null
    RET=$?
    echo "Created files in ${SEARDUINO_HOME_DIR}/$CREATE_DIR"
elif [ "$PROJECT_DIR" != "" ]
then
    if [ ! -d $PROJECT_DIR ]
    then
	echo "Build directory does not exist"
	exit $NON_EXIST_DIR
    fi
    if [ ! -f $PROJECT_DIR/Makefile ]
    then
	echo "No Makefile in the Build directory"
	exit $NON_EXIST_MAKEFILE
    fi
#    echo "Build $PROJECT_DIR"
    cd $PROJECT_DIR
    make $MAKE_RULES  2>&1 
    RET=$?
fi




exit $RET
