#! /bin/bash
# Wrapper script for launching the instant apps command line tool in POSIX environments

if ! command -v java >/dev/null 2>&1; then
  echo "ERROR - The java command was not found."
  echo "Ensure that you have installed the Java Runtime Environment, and"
  echo "that its location has been added to your PATH."
  exit 1
fi

if command -v readlink >/dev/null 2>&1 && readlink -f $0 >/dev/null 2>&1; then
  scriptdir="$(dirname -- "$(command -v -- "$(readlink -f $0)")")"
else
  if command -v perl >/dev/null 2>&1 && perl -MCwd -e 1 2>/dev/null; then
    scriptdir="$(dirname -- "$(perl -MCwd -e 'print Cwd::abs_path shift' "$0")")"
  else
    echo "WARNING - Utilities to resolve symbolic links not found. If issues"
    echo "arise finding the JAR file, please ensure that the working directory"
    echo "of this script does not contain a symlink."
    scriptdir="$(cd -P -- "$(dirname -- "$(command -v -- "$0")")" && pwd -P)"
  fi
fi

# If possible, detect terminal width and pass it on to the JAR
cols="$(tput cols 2>/dev/null)"
colspec=
if [[ -n "$cols" ]]; then
  colspec="-w $cols"
fi

java -jar "$scriptdir/tools/ia.jar" $colspec "$@"
