#! /bin/sh

# Script parameters are passed in the following environment variables:
#   GAMBDOC_GAMBITDIR_BIN
#   GAMBDOC_GAMBITDIR_DOC
#   GAMBDOC_ARG1
#   GAMBDOC_ARG2
#   GAMBDOC_ARG3
#   GAMBDOC_ARG4
#   ...

# echo GAMBDOC_GAMBITDIR_BIN = "${GAMBDOC_GAMBITDIR_BIN}"
# echo GAMBDOC_GAMBITDIR_DOC = "${GAMBDOC_GAMBITDIR_DOC}"
# echo GAMBDOC_ARG1 = "${GAMBDOC_ARG1}"
# echo GAMBDOC_ARG2 = "${GAMBDOC_ARG2}"
# echo GAMBDOC_ARG3 = "${GAMBDOC_ARG3}"
# echo GAMBDOC_ARG4 = "${GAMBDOC_ARG4}"

find_in_path() # exe-name, sets `$exe'
{
  save_IFS="${IFS}"; IFS=":"
  for dir in $PATH; do
    if test -x "$dir/$1" -a ! -d "$dir/$1"; then
      exe="$dir/$1"; IFS="$save_IFS"; return 0
    fi
  done
  exe=""; IFS="$save_IFS"; return 1
}

find_browser() # sets `$exe'
{
  if [ "@HELP_BROWSER@" != "" ]; then
    browser_list="@HELP_BROWSER@"
  else
    browser_list="lynx firefox mozilla netscape osascript chrome chromium chromium-browser"
  fi

  browser_list="${GAMBDOC_ARG3} $browser_list"

  for b in $browser_list; do
    if find_in_path $b; then
      browser=$b
      return 0
    fi
  done
  return 1
}

operation_help() # sets `$exe'
{
  if find_browser; then
    url="file://${GAMBDOC_GAMBITDIR_DOC}/gambit.html#${GAMBDOC_ARG4}"
    case "$browser" in
      osascript ) $exe <<EOF ;;
tell application "Safari"
    open location "$url"
end tell
EOF
              * ) $exe $url ;;
    esac
  else
    echo "*** WARNING -- none of these browsers can be found to view the documentation:"
    echo "***            $browser_list"
    exit 1
  fi
}

if [ "${GAMBDOC_ARG1}" = "help" ]; then
  operation_help
else
  echo "*** WARNING -- unsupported operation: ${GAMBDOC_ARG1}"
  exit 1
fi
