#!/bin/bash

ant -version &> /dev/null
if [ $? -ne 0 ]; then
    echo 'Requires Apache Ant (see http://ant.apache.org/)'
    exit 1
fi

NAME=$(basename -- "$0")

# find ol home
if [ -z "$0" ]; then
    # as a last recourse, use the present working directory
    OL_HOME=$(pwd)
else
    # save original working directory
    ORIG_PWD="$(pwd -P)"

    # get the absolute path of the executable
    SELF_PATH=$(
        cd -P -- "$(dirname -- "$0")" \
        && pwd -P
    ) && SELF_PATH=$SELF_PATH/$(basename -- "$0")

    # resolve symlinks
    while [ -h "$SELF_PATH" ]; do
        DIR=$(dirname -- "$SELF_PATH")
        SYM=$(readlink -- "$SELF_PATH")
        SELF_PATH=$(cd -- "$DIR" && cd -- $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
    done

    OL_HOME=$(dirname -- "$(dirname -- "$SELF_PATH")")

    # restore original working directory
    cd "$ORIG_PWD"

fi

ANT_ARGS=""
if [ $# -gt 1 ]; then
    ANT_ARGS=-Dbuild.json="$2"
fi

ant -e -f "$OL_HOME/bin/build.xml" -Dbasedir=. -Dol.home="$OL_HOME" $1 $ANT_ARGS
