#!/bin/sh
# Run Emacs -- really simplified version

case "`machtype -L`" in
    8.[3-9]|9.*|[1-9][0-9].*|debathena*) ;;   # 8.3 and later is OK
    *) echo "warning: Athena release is prior to 8.3!" >&2 ;;
esac

DEBUG=
DO_SCREEN=yes
while [ $# -gt 0 ]; do
    case "$1" in
      --help|--h|--\?) echo "usage: e [--help|--debug]" >&2; shift ;;
      --debug|--d)     DEBUG=yes; shift ;;
      --no-screen)     DO_SCREEN=; shift ;;
      *)               break ;;
    esac
done

if [ -n "$DEBUG" ]; then
    for d in `cd /usr/athena/share/emacs; echo *`; do
	if [ ! -d "/usr/athena/share/emacs/$d" ]; then continue; fi
	case "$d" in
	    [1-9]*) versions="${versions}${sep}$d"; sep=" " ;;
	esac
    done
    case "$versions" in
      "")   echo "No Emacs version found!" >&2 ;;
      *\ *) echo "Multiple Emacs versions found: $versions!" >&2 ;;
      *)    echo "Found Emacs version $versions." >&2 ;;
    esac
fi

if [ -n "$STY" -a -n "$DO_SCREEN" ]; then
    # running under screen -- start a new session.
    [ -n "$DEBUG" ] && echo "Running: screen /usr/athena/bin/emacs $*";
    exec 'screen' '/usr/athena/bin/emacs' "$@";
else
    [ -n "$DEBUG" ] && echo "Running: /usr/athena/bin/emacs $*";
    exec '/usr/athena/bin/emacs' "$@";
fi
echo "error: exec failed!";
exit 1
