#!/usr/bin/env sh
#
# Falcon Scan CLI Startup Script for Unix
#
# Required ENV vars:
#   JAVA_HOME - Location of Java's installation, optional if use_embedded_jre is set
#
# Optional ENV vars:
#   FALCON_SCAN_CLI_OPTS - Parameters passed to the Java VM when running the Falcon Scan CLI
#   FALCON_SCAN_CLI_DEBUG_OPTS - Extra parameters passed to the Java VM for debugging

real_path () {
  target=$1

  (
  while true; do
    cd "`dirname "$target"`"
    target=`basename "$target"`
    test -L "$target" || break
    target=`readlink "$target"`
  done

  echo "`pwd -P`/$target"
  )
}

script_path=`real_path "$0"`
falcon_scan_cli_home=`dirname "$script_path"`/..

# make it fully qualified
falcon_scan_cli_home=`cd "$falcon_scan_cli_home" && pwd -P`

jar_file=$falcon_scan_cli_home/lib/*

use_embedded_jre=true
if [ "$use_embedded_jre" = true ]; then
  export JAVA_HOME="$falcon_scan_cli_home/jre"
fi

if [ -n "$JAVA_HOME" ]
then
  java_cmd="$JAVA_HOME/bin/java"
else
  java_cmd="`which java`"
fi

if [ -z "$java_cmd" -o ! -x "$java_cmd" ] ; then
  echo "Could not find 'java' executable in JAVA_HOME or PATH."
  exit 1
fi

project_home=`pwd`

#echo "Info: Using falcon-scan-cli at $falcon_scan_cli_home"
#echo "Info: Using java at $java_cmd"
#echo "Info: Using classpath $jar_file"
#echo "Info: Using project $project_home"
  # -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG \

exec "$java_cmd" \
  -Djava.awt.headless=true \
  $FALCON_SCAN_CLI_OPTS \
  $FALCON_SCAN_CLI_DEBUG_OPTS \
  -classpath  "$jar_file" \
  -Dscanner.home="$falcon_scan_cli_home" \
  -Dproject.home="$project_home" \
  com.integralzone.falcon.cli.Cli "$@"

