#!/usr/bin/env bash
#
# Author: Makarius
#
# DESCRIPTION: print Isabelle system options


## diagnostics

PRG="$(basename "$0")"

function usage()
{
  echo
  echo "Usage: isabelle $PRG [OPTIONS] [MORE_OPTIONS ...]"
  echo
  echo "  Options are:"
  echo "    -b           include \$ISABELLE_BUILD_OPTIONS"
  echo "    -g OPTION    get value of OPTION"
  echo "    -l           list options"
  echo "    -x FILE      export options to FILE in YXML format"
  echo
  echo "  Report Isabelle system options, augmented by MORE_OPTIONS given as"
  echo "  arguments NAME=VAL or NAME."
  echo
  exit 1
}

function fail()
{
  echo "$1" >&2
  exit 2
}


## process command line

declare -a BUILD_OPTIONS=()
GET_OPTION=""
LIST_OPTIONS="false"
EXPORT_FILE=""

while getopts "bg:lx:" OPT
do
  case "$OPT" in
    b)
      eval "BUILD_OPTIONS=($ISABELLE_BUILD_OPTIONS)"
      ;;
    g)
      GET_OPTION="$OPTARG"
      ;;
    l)
      LIST_OPTIONS="true"
      ;;
    x)
      EXPORT_FILE="$OPTARG"
      ;;
    \?)
      usage
      ;;
  esac
done

shift $(($OPTIND - 1))

[ -z "$GET_OPTION" -a "$LIST_OPTIONS" = "false" -a -z "$EXPORT_FILE" ] && usage


## main

isabelle_admin_build jars || exit $?

exec "$ISABELLE_TOOL" java isabelle.Options \
  "$GET_OPTION" "$EXPORT_FILE" "${BUILD_OPTIONS[@]}" "$@"

