#!/bin/sh

# cross-platform substitute for readlink -f
realpath() {
  OURPWD="$PWD"
  cd "$(dirname "$1")"
  LINK="$(readlink "$(basename "$1")")"
  while [ "$LINK" ]; do
    cd "$(dirname "$LINK")"
    LINK="$(readlink "$(basename "$1")")"
  done
  REALPATH="$PWD/$(basename "$1")"
  cd "$OURPWD"
  echo "$REALPATH"
}

basedir="$(dirname "$(realpath "$0")")"

env_prefix="$(dirname $(dirname $basedir))"
loggingConfiguration="$basedir/config/logback.xml"
for var in "$@" ; do
    if [ "$var" = "-v" -o "$var" = "-verbose" ]
    then
        loggingConfiguration="$basedir/config/logback-verbose.xml"
    fi
done

command="$env_prefix/bin/java -Dlogback.configurationFile='${loggingConfiguration}' -jar '$basedir/lib/rabix-cli.jar'"

args=""

# quote non-parameter arguments
for var in "$@"; do
 parameter=`echo $var|grep -o "^-"`
 if [ "$parameter" != "" ]; then
    args="$args $var"
 else
   args="$args \"$var\""
 fi
done

eval $command $args
