#!/bin/bash

EX_FILE=""
EX_DIR=""
VCS_BUILD="false"
SEARDUINO_PATH=__INSTALL_DIR__
DEST_DIR=""

export LD_LIBRARY_PATH=__INSTALL_DIR__/lib


TMPL_DIR=${SEARDUINO_PATH}/share/searduino/tmpl

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


parse_args()
{
    export BUILD_TYPE=stub

    while [ "$1" != "" ]
    do
	case $1 in
	    "--shlib")
		BUILD_TYPE=shlib
		;;
	    "--prog")
		BUILD_TYPE=prog
		;;
	    "--uno")
		BUILD_TYPE=uno
		;;
	    "--leonardo")
		BUILD_TYPE=leonardo
		;;
	    "--mega")
		BUILD_TYPE=mega
		;;
	    "--due")
		BUILD_TYPE=due
		;;
	    "--build")
		BUILD=true
		;;
	    "--searduino-path")
		SEARDUINO_PATH=$2
		shift
		;;
	    "--yes")
		YES=true
		;;
	    "--create")
		CREATE=true
		CREATE_DIR=$2
                shift
		;;
	    "--destination-dir")
		DEST_DIR=$2
		shift
		;;
	    "--vcs")
		VCS_BUILD="true"
		TMPL_DIR=${SEARDUINO_PATH}/tmpl
		;;
	    *)
#		echo "PARSE: $1"
		break
		;;
	esac	
	shift
    done
    
    if [ "$SEARDUINO_PATH" = "" ]
    then
	export SEARDUINO_PATH=$1
	export FIRST_CHAR=${SEARDUINO_PATH:0:1}
#	echo " FIRST_CHAR: \"$FIRST_CHAR\""
	if [ "$FIRST_CHAR" = "/" ]
	then
	    SEARDUINO_PATH=$1/../../../../searduino
	else
	    SEARDUINO_PATH=$(pwd)/$1/../../../../../
	fi
    fi

    if [ "$CREATE" = "true" ]
    then
        :
    elif [ "$1" = "" ]
    then
	echo "No args given"
	exit 1
    elif [ -f $1 ]
    then
	EX_FILE=$1
    elif [ -d $1 ]
    then
	EX_DIR=$1
    else
	echo "No dir or file given"
	exit 1
    fi
}


write_c_file()
{
    echo "$*" >> $C_FILE
    return $?
}

ard2c_file()
{
    cat $1 >> $C_FILE
    return $?
}

create_c_file()
{
    ARD_FILE=$1

    if [ "$ARD_DIR" = "" ]
    then
	ARD_DIR=$(basename $ARD_FILE | sed 's,\.ino,,g')
	if [ "$DEST_DIR" != "" ]
	then
	    ARD_DIR=$DEST_DIR/$ARD_DIR
	fi
    fi


    if [ ! -d $ARD_DIR ]
    then
	echo "Creating dir '$ARD_DIR'" 
	mkdir -p $ARD_DIR
    fi


    C_FILE=$(basename $ARD_FILE | sed 's,\.ino,\.cpp,g')
    echo "From $ARD_FILE To $C_FILE"
    
    if [ -f $ARD_DIR/$C_FILE ]
    then
	if [ "$YES" != "true" ] 
	then
	    echo "C file ($C_FILE) exists, do you want to overwrite? [y/N]"
	    read ANS
	    if  [ "$ANS" != "y" ] 
	    then
		echo "bailing out.."
		exit 0
	    fi
	fi
	rm -f $ARD_DIR/$C_FILE
    fi
    
    echo "  * creating $C_FILE  (from $ARD_FILE)" 

    cat $TMPL_DIR/pre_c.tmpl | sed -e  "s,__ARD_FILE__,$ARD_FILE,g" >>  $C_FILE
    exit_on_failure $? "Creating C file 1 ($C_FILE) "

    ard2c_file $ARD_FILE
    exit_on_failure $? "Creating C file 2 ($C_FILE) "

#    ard2c_file $TMPL_DIR/main_c.tmpl
#    exit_on_failure $? "Creating C file 3 ($C_FILE) "

    cat $TMPL_DIR/post_c.tmpl | sed -e  "s,__ARD_FILE__,$ARD_FILE,g" >>  $C_FILE
    exit_on_failure $? "Creating C file 4 ($C_FILE) "

    mv $C_FILE $ARD_DIR/
    exit_on_failure $? "Moving C file ($C_FILE) "
}


create_makefile()
{
    ARD_DIR=$1

    if [ "$ARD_DIR" = "" ]
    then
	ARD_DIR=$(basename $ARD_FILE | sed 's,\.ino,,g')
	if [ "$DEST_DIR" != "" ]
	then
	    ARD_DIR=$DEST_DIR/$ARD_DIR
	fi
    fi

    if [ ! -d $ARD_DIR ]
    then
	echo "Creating dir '$ARD_DIR'" 
	mkdir -p $ARD_DIR
    fi


    export ARDUINO_VALUE=""
    export TO_BUILD=""
    export TO_BUILD_TARGET=""
#    export MAIN_FILE=""
    export BUILD_LD_FLAGS=""

    if [ "$BUILD_TYPE" = "prog" ]
    then
	ARDUINO_VALUE=stub
	TO_BUILD_TARGET="\$(PROG)"
	TO_BUILD="PROG=$(echo $(basename ARD_DIR) | tr [A-Z] [a-z])"
	SHLIB_LIBS=-lpthread
    elif [ "$BUILD_TYPE" = "shlib" ]
    then
	ARDUINO_VALUE=stub
	TO_BUILD_TARGET="\$(SHLIB)"
	TO_BUILD="SHLIB=lib$(echo $(basename $ARD_DIR) | tr [A-Z] [a-z]).so"
	BUILD_LD_FLAGS="\$(SHLIB_LIBS)"
	SHLIB_LIBS=-lpthread
    elif [ "$BUILD_TYPE" = "uno" ] || [ "$BUILD_TYPE" = "due" ] || [ "$BUILD_TYPE" = "mega" ] || [ "$BUILD_TYPE" = "leonardo" ]
    then
	ARDUINO_VALUE=$BUILD_TYPE
	TO_BUILD_TARGET="\$(PROG)"
	TO_BUILD="PROG=ard-code"
#	MAIN_FILE=$C_FILES
	C_FILES=""
	SHLIB_LIBS=""
    fi

#    echo "Makefile - $ARD_DIR"
    echo "  * creating Makefile" 
    cat $TMPL_DIR/Makefile.tmpl | \
          sed "s,__C_FILES__,$C_FILES,g" | \
          sed "s,__CXX_FILES__,$CXX_FILES,g" | \
          sed "s,__MAIN_FILE__,$MAIN_FILE,g" | \
          sed "s,__TO_BUILD__,$TO_BUILD,g" | \
          sed "s,__ALL_DEP__,$TO_BUILD_TARGET,g" | \
          sed "s,__ARDUINO_VALUE__,$ARDUINO_VALUE,g"   | \
          sed "s,__SEARDUINO_PATH__,$SEARDUINO_PATH,g" | \
          sed "s,__SEARDUINO_MK__,\$(SEARDUINO_PATH)//share/searduino/mk/searduino.mk,g" | \
	  sed "s,__BUILD_LD_FLAGS__,$BUILD_LD_FLAGS,g" | \
	  sed "s,__SHLIB_LIBS__,$SHLIB_LIBS,g"  > $ARD_DIR/Makefile
    exit_on_failure $? "Creating Makefile "
#          sed "s,__MAIN_FILE__,$MAIN_FILE,g" | \
    if [ "$VCS_BUILD" = "true" ]
    then
	mv  $ARD_DIR/Makefile $ARD_DIR/Makefile.tmp
	cat $ARD_DIR/Makefile.tmp | \
            sed "s,\/share\/searduino,,g" > $ARD_DIR/Makefile
	mv  $ARD_DIR/Makefile $ARD_DIR/Makefile.tmp
	cat $ARD_DIR/Makefile.tmp | \
            sed "s,searduino\.mk,searduino\-vcs\.mk,g" > $ARD_DIR/Makefile
    fi

}

create_dir()
{
    ARD_DIR_LONG=$1
    ARD_DIR=$(basename $ARD_DIR_LONG)
    
    if [ "$DEST_DIR" != "" ]
    then
	ARD_DIR=$DEST_DIR/$ARD_DIR
    fi


    if [ -d $ARD_DIR ]
	then
	sleep 0
#	echo "Dir exists...handle later ,for now: ignoring"
    fi

    if [ ! -d $ARD_DIR ]
    then
	echo "Creating dir '$ARD_DIR'" 
	mkdir -p $ARD_DIR
    fi


    export C_FILES=""
    for i in $(find $ARD_DIR_LONG -type f -name "*.ino" 2>/dev/null)
    do
#	echo "Create C file from : $i"
	create_c_file $i
	C_FILES="$C_FILES $C_FILE"
    done
    rm -f $ARD_DIR/main.c
    if [ "$CREATE" = "true" ]
    then
        cat $TMPL_DIR/main_tot_c.tmpl | \
            sed -e  "s,__ARD_FILE__,Searduino template file,g"  | \
            sed -e  "s/__SETUP__/void setup(void) {\n  pinMode(13,OUTPUT);\n}/g"  | \
            sed -e  "s/__LOOP__/void loop(void) {\n  digitalWrite(13,HIGH);\n  delay(500);\n  digitalWrite(13,LOW);\n  delay(500);\n}/g"  \
                >  $ARD_DIR/main.c
        exit_on_failure $? "Creating C file 4 ($C_FILES) "
    else
        cat $TMPL_DIR/main_tot_c.tmpl | \
            sed -e  "s,__ARD_FILE__,$ARD_FILE,g"  | \
            sed -e  "s,__SETUP__,,g" | \
            sed -e  "s,__LOOP__,,g"  \
                >  $ARD_DIR/main.c
        exit_on_failure $? "Creating C file 4 ($C_FILES) "
    fi


    export CXX_FILES=${C_FILES}
    C_FILES=
    export MAIN_FILE=main.c

    for i in $(find $ARD_DIR_LONG -type f -name "*.h" 2>/dev/null)
    do
	cp $i $ARD_DIR
    done

    create_makefile $ARD_DIR
}

build_dir()
{
    ARD_DIR_LONG=$1
    ARD_DIR=$(basename $ARD_DIR_LONG)
    
    if [ "$DEST_DIR" != "" ]
    then
	ARD_DIR=$DEST_DIR/$ARD_DIR
    fi

    cd $ARD_DIR
    make
    cd -
}

parse_args $*

if [ "$EX_FILE" != "" ]
then
    create_c_file $EX_FILE
elif [ "$CREATE" = "true" ]
then
    create_dir $CREATE_DIR 
else
    create_dir $EX_DIR
    if [ "$BUILD" = "true" ]
    then
	build_dir $EX_DIR
    fi
fi





exit 0
