PACKAGE=searduino

FUNC_FILE=$(dirname $0)/functions
if [ ! -f $FUNC_FILE ] || [ "$FUNC_FILE" = "" ]
then
    echo "Can't find the file: '$FUNC_FILE'"
    echo "... bailing out"
    exit 1
fi

. $FUNC_FILE



check()
{
    make check
    exit_on_failure $? "make check"

    SAVED_DIR=$(pwd)

    cd ${TMP_INST}/share/searduino/example/digpins/ && make -f Makefile.digpins clean prog  && make -f Makefile.digpins clean shlib
    exit_on_failure $? "make digpins in install dir"
    
    cd ${TMP_INST}/share/searduino/example/python-digcounter && make -f Makefile.digcounter clean prog && make -f Makefile.digcounter clean shlib
    exit_on_failure $? "make digcounter in installed dir"

    cd ${SAVED_DIR}
}


packit()
{
    START_DIR=$(pwd)
    TAR_FILE=${START_DIR}/$PACKAGE-bin-$SEARD_VERSION-${MY_OS}-${CPU}.tar
    cd $TMP_INST
    cd ..
    rm -f   ${TAR_FILE}*
    echo TAR
    tar cvf ${TAR_FILE} $PACKAGE
    RET=$?
    echo "TAR: $RET"
    exit_on_failure $? "archive: tar cvf ${TAR_FILE} $PACKAGE   (in $(pwd)) "
    gzip    ${TAR_FILE}
    RET=$?
    echo "GZIP: $RET"
    exit_on_failure $? "gzip tar file ${TAR_FILE} $PACKAGE   (in $(pwd)) "
}

setup() 
{
    MACH=$(uname -m)
    
    if [ "$MACH" = "i686" ]
    then
	ARCH=i386
    else
	ARCH=amd64
    fi

    if [ "$DIST" = "darwin" ]
    then
	MAC_JAVA="/System/Library/Frameworks/JavaVM.framework//Versions/Current"
	MAC_CONF=" --prefix=/opt/searduino --disable-python-extension  --disable-pearduino $CON\
F_ARGS "
	MAC_CONF="$MAC_CONF CFLAGS=-I$MAC_JAVA/Headers/"
	MAC_CONF="$MAC_CONF CPPFLAGS=-I$MAC_JAVA/Headers/"
	MAC_CONF="$MAC_CONF CXXFLAGS=-I$MAC_JAVA/Headers/"
	#           MAC_CONF="$MAC_CONF --enable-ltdl-convenience --no-create --no-recursion "
	CONF_ARGS="$CONF_ARGS  $MAC_CONF"
    fi

}


setup_gnulinux_c_flags()
{
    export CFLAGS="-I${DEFAULT_JAVA_PATH} -I${DEFAULT_JAVA_PATH}/linux/"
    export CXXFLAGS="-I${DEFAULT_JAVA_PATH} -I${DEFAULT_JAVA_PATH}/linux/"
}


setup_c_flags()
{
    if [ "$1" = "fedora" ]
    then
        DEFAULT_JAVA_PATH=/usr/lib/jvm/java-1.7.0/include/
	setup_gnulinux_c_flags
    elif [ "$1" = "debian" ]
    then
        DEFAULT_JAVA_PATH=/usr/lib/jvm/java-1.6.0-openjdk/include
	setup_gnulinux_c_flags
    elif [ "$1" = "ubuntu" ]
    then
        DEFAULT_JAVA_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/
        export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64/
        export JAVA_HOME=/usr/lib/jvm/java/
        
	setup_gnulinux_c_flags
    fi

    
}

setup_arduino_sources()
{
    if [ "$1" = "fedora" ]
    then
	CONF_ARGS="$CONF_ARGS --enable-fedora-sources"
    elif [ "$1" = "debian" ]
    then
	CONF_ARGS="$CONF_ARGS --enable-debian-sources"
    elif [ "$1" = "ubuntu" ]
    then
	# yup, debian-sources  (not ubuntu-sources)
	CONF_ARGS="$CONF_ARGS --enable-debian-sources"
    fi
}

parse() 
{
    while [ "$1" != "" ]
    do
	if [ "$1" = "--arduino-sources" ]
	then
	    setup_arduino_sources $DIST
	elif [ "$1" = "--bin-dist" ]
	then	
	    BUILD_ARGS="true"
	elif [ "$1" = "--cflags" ]
		then
	    setup_c_flags $DIST
	fi
	shift
    done	
}


prepare()
{
   make -f Makefile.git $MAKE_GIT_ARGS && \
       CFLAGS=${CFLAGS} CXXFLAGS=${CXXFLAGS} ./configure $CONF_ARGS --prefix=/opt/searduino --disable-python-extension  --disable-pearduino  
	
}

build_bin_dist()
{
    log "Building and checking"
    
    init_logging
    
    log_and_exec prepare
    log_and_exec build
    #log_and_exec check
    log_and_exec packit

    close_logging
}


build()
{
    if [ "$DIST" = "darwin" ]
    then
	MAKE_GIT_ARGS="mac-setup"
    fi
    
    make clean && make 
    return $?
}

install()
{
    sudo make install
    return $?
}


verify()
{
    bash /opt/searduino/share/searduino/scripts/verify-install.sh && \
    return $?
}

dobuild() 
{
    if [ "$DIST" = "darwin" ]
    then
	. ~/.bashrc
    fi
    parse $*
    setup

    if [ "$BUILD_ARGS" = "true" ]
    then
	build_bin_dist
    else
        prepare     
        build       
    fi
}
