#!/bin/bash

# https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
  SOURCE=$(readlink "$SOURCE")
  [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )

TOOLS_DIR=${DIR}/../..
DEVROCK_SDK_HOME=${TOOLS_DIR}/..
export REPOSITORY_CONFIGURATION_DEVROCK_ANT_TASKS="${DEVROCK_SDK_HOME}/conf/repository-configuration-devrock.yaml"

if [ -e build.gradle ]; then
    # transform arguments to have stable name prefixing and convenient transform for custom step request properties
    # convienent input: +name value 
    # actual argument: -P.name=value
    transformed_args=()
    next_is_value=false
    key=""

    for arg in "$@"; do
        if $next_is_value; then
            transformed_args+=("-P.$key=$arg")
            next_is_value=false
        else
            if [[ $arg == +* ]]; then
                # Remove '+' and mark the next argument as the value
                key="${arg#+}"
                next_is_value=true
            else
                # Add the argument as is
                transformed_args+=("$arg")
            fi
        fi
    done

    # Call to gradle with transformed arguments
    export GRADLE_HOME="${TOOLS_DIR}/gradle"
    "${GRADLE_HOME}/bin/gradle" "${transformed_args[@]}"

elif [ -e build.xml ]; then
    export ANT_HOME="${TOOLS_DIR}/ant"
    ANT_LIB_DIR="${TOOLS_DIR}/ant-libs"
    "${ANT_HOME}/bin/ant" -lib "${ANT_LIB_DIR}" "$@"
else
    echo "current working dir is neither containing a gradle nor an ant build script"
    exit 1
fi