#!/usr/bin/env bash
#
# Author: Tim Desjardins
# Copyright (c) 2008 Agwego Enterprises Inc.
#
# Feel free to use or abuse this code anyway you wish, without warranty of course.
#
# Unless your set MAA_ROOT this script will only work when the current working directory
# is the same as that of build.bash
#

norm_path=""
if [ "$(uname -o)" == "Cygwin" ] ; then
    norm_path=" cygpath -d "
fi

export MAA_ROOT=${MAA_ROOT:-$(${norm_path} $(pwd))/..}
export MAA_SOURCE_ROOT=${MAA_SOURCE_ROOT:-${MAA_ROOT}/java}
export MAA_BUILD=${MAA_BUILD:-${MAA_ROOT}/build}
export MAA_BUILD_CLASSES=${MAA_BUILD_CLASSES:-${MAA_BUILD}/classes}
export MAA_WEB=${MAA_WEB:-${MAA_ROOT}/web}

function build()
{
    mkdir -p ${MAA_BUILD_CLASSES}
    javac -d ${MAA_BUILD_CLASSES} ${MAA_SOURCE_ROOT}/MacAddressApplet.java
}

function install()
{
    build
    pushd ${MAA_BUILD_CLASSES}
    jar cvf macaddressapplet.jar MacAddressApplet.class
    popd
    cp ${MAA_BUILD_CLASSES}/macaddressapplet.jar ${MAA_WEB}
}

function package()
{
    install
    pushd ${MAA_ROOT}
    jar cvfM ${MAA_BUILD}/macaddressapplet_web.jar web
    popd
}

function release()
{
    install
	clean
    pushd ${MAA_ROOT}/..
	tar cvf macaddressapplet.tar --exclude=.svn --exclude=personal macaddressapplet
    popd
}

function clean()
{
    rm -rf ${MAA_BUILD_CLASSES}
    rm -f ${MAA_BUILD_CLASSES}/macaddressapplet.jar
}

function spotless()
{
    clean
    rm -f ${MAA_WEB}/macaddressapplet.jar
    rm -f ${MAA_BUILD}/macaddressapplet_web.jar
}

function usage_help()
{
    echo "build.bash [command*]"
    echo "command:"
    echo "    build"
    echo "    install"
    echo "    clean"
    echo "    spotless"
    echo "    package"
    echo "    --help"
}

while [ $# -gt 0 ] ; do
    case "$1" in
        --)
            break
            ;;
        --help)
            usage_help
            exit 0
            ;;
        build)
            build
            ;;
        install)
            install
            ;;
        clean)
            clean
            ;;
        spotless)
            spotless
            ;;
        package)
            package
            ;;
        release)
            release
            ;;
        *)
            echo "$0: unknown argument '$1'"
            exit 1
    esac
    shift
done
