#!/usr/bin/env bash

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

# Resolve symlinks - this is the equivalent of "readlink -f", but also works with non-standard OS X readlink.
while [ -h "$SELF_PATH" ]; do
    # 1) cd to directory of the symlink
    # 2) cd to the directory of where the symlink points
    # 3) Get the pwd
    # 4) Append the basename
    DIR="$(dirname -- "$SELF_PATH")"
    SYM="$(readlink "$SELF_PATH")"
    SELF_PATH="$(cd "$DIR" && cd "$(dirname -- "$SYM")" && pwd)/$(basename -- "$SYM")"
done
cd "$ORIGDIR"

# Build the path to the root PHP file
SCRIPT_PATH="$(dirname "$SELF_PATH")/app/src/Core/CLI/etsis-cli.php"
case $(uname -a) in
    CYGWIN*)
        SCRIPT_PATH="$(cygpath -w -a -- "$SCRIPT_PATH")" ;;
esac

# Use the ETSIS_CLI_PHP environment variable if it is available, otherwise use the php found on path
if [ ! -z "$ETSIS_CLI_PHP" ] ; then
    php="$ETSIS_CLI_PHP"
else
    # Note that we need the full path to php here for Dreamhost, which behaves oddly. See http://drupal.org/node/662926
    php="`which php`"
fi

# Pass in the path to php so that YOURLS-CLI knows which one
# to use if it re-launches itself to run other commands.
export ETSIS_CLI_PHP_USED="$php"

exec "$php" $CLI_PHP_ARGS "$SCRIPT_PATH" "$@"

