Admin/MacOS/App1/script
author berghofe
Mon, 23 Jul 2012 18:52:10 +0200
changeset 48453 2421ff8c57a5
parent 45095 bf7a8906c0cb
child 48631 81c81f13d152
permissions -rw-r--r--
set_vcs now derives prefix from fully qualified procedure / function name

#!/bin/bash
#
# Author: Makarius
#
# Isabelle application wrapper

THIS="$(cd "$(dirname "$0")"; pwd)"
THIS_APP="$(cd "$THIS/../.."; pwd)"
SUPER_APP="$(cd "$THIS/../../.."; pwd)"


# sane environment defaults

cd "$HOME"
if [ -x /usr/libexec/path_helper ]; then
  eval $(/usr/libexec/path_helper -s)
fi

[ -z "$LANG" ] && export LANG=en_US.UTF-8


# settings support

function choosefrom ()
{
  local RESULT=""
  local FILE=""

  for FILE in "$@"
  do
    [ -z "$RESULT" -a -e "$FILE" ] && RESULT="$FILE"
  done

  [ -z "$RESULT" ] && RESULT="$FILE"
  echo "$RESULT"
}


# Isabelle

ISABELLE_TOOL="$(choosefrom \
  "$THIS/Isabelle/bin/isabelle" \
  "$SUPER_APP/Isabelle/bin/isabelle" \
  "$HOME/bin/isabelle" \
  isabelle)"


# Proof General / Emacs

PROOFGENERAL_EMACS="$(choosefrom \
  "$THIS/Emacs.app/Contents/MacOS/Emacs" \
  "$SUPER_APP/Emacs.app/Contents/MacOS/Emacs" \
  /Applications/Emacs.app/Contents/MacOS/Emacs \
  "")"

declare -a EMACS_OPTIONS=()
if [ -n "$PROOFGENERAL_EMACS" ]; then
  EMACS_OPTIONS=(-p "$PROOFGENERAL_EMACS")
fi


# enforce fonts

if [ ! -f "$HOME/Library/Fonts/STIXGeneral.ttf" -a ! -f "$HOME/Library/Fonts/STIXGeneral.otf" ]
then
  cp -f "$THIS/STIXv1.0.0/Fonts"/STIXGeneral* "$HOME/Library/Fonts/"
  sleep 3
fi

EMACS_OPTIONS["${#EMACS_OPTIONS[@]}"]="-x"
EMACS_OPTIONS["${#EMACS_OPTIONS[@]}"]="true"


# run interface with error feedback

ISABELLE_INTERFACE_CHOICE="$("$ISABELLE_TOOL" getenv -b ISABELLE_INTERFACE_CHOICE)"
if [ "$ISABELLE_INTERFACE_CHOICE" != emacs -a "$ISABELLE_INTERFACE_CHOICE" != jedit ]
then
  declare -a CHOICE
  CHOICE=($("$THIS/CocoaDialog.app/Contents/MacOS/CocoaDialog" dropdown \
    --title Isabelle \
    --text "Which Isabelle interface?" \
    --items "Emacs / Proof General" "Isabelle/jEdit PIDE" \
    --button2 "OK, do not ask again" --button1 "OK"))
  if [ "${CHOICE[1]}" = 0 ]; then
    ISABELLE_INTERFACE_CHOICE=emacs
  else
    ISABELLE_INTERFACE_CHOICE=jedit
  fi
  if [ "${CHOICE[0]}" = 2 ]; then
    ISABELLE_HOME_USER="$("$ISABELLE_TOOL" getenv -b ISABELLE_HOME_USER)"
    mkdir -p "$ISABELLE_HOME_USER/etc"
    ( echo; echo "ISABELLE_INTERFACE_CHOICE=$ISABELLE_INTERFACE_CHOICE"; ) \
      >> "$ISABELLE_HOME_USER/etc/settings"
    "$THIS/CocoaDialog.app/Contents/MacOS/CocoaDialog" ok-msgbox \
      --title Isabelle \
      --text Note \
      --informative-text "ISABELLE_INTERFACE_CHOICE stored in $ISABELLE_HOME_USER/etc/settings" \
      --no-cancel
  fi
fi

OUTPUT="/tmp/isabelle$$.out"

if [ "$ISABELLE_INTERFACE_CHOICE" = emacs ]; then
  ( "$ISABELLE_TOOL" emacs "${EMACS_OPTIONS[@]}" "$@" ) > "$OUTPUT" 2>&1
  RC=$?
else
  ( "$ISABELLE_TOOL" jedit "$@" ) > "$OUTPUT" 2>&1
  RC=$?
fi

if [ "$RC" != 0 ]; then
  echo >> "$OUTPUT"
  echo "Return code: $RC" >> "$OUTPUT"
fi

if [ $(stat -f "%z" "$OUTPUT") != 0 ]; then
  "$THIS/CocoaDialog.app/Contents/MacOS/CocoaDialog" textbox \
    --title "Isabelle" \
    --informative-text "Isabelle output" \
    --text-from-file "$OUTPUT" \
    --button1 "OK"
fi

rm -f "$OUTPUT"

exit "$RC"