#!/bin/ksh

EMACS_DIR=""
for DIR in /usr/local/emacs-20.3/src /usr/bin /opt/OpenSource/bin ; do
  if [ -x ${DIR}/emacs ] ; then EMACS_DIR="${DIR}" ; break ; fi
done

NAME="emacs" ; EMACS="${EMACS_DIR}/${NAME}"

# Run through and process the user's arguments.  Append any
# additional X-resources to the list so far.  EMACS only seems to
# support the presence of one -xrm keyword on the command line.
#
# Make sure that the background color matches the one chosen in
# config_template/emacs/default.el.
#
ARGLIST="" ; NOFONT=""

XRMUSER=""
XRMLIST="${NAME}.bitmapIcon:on
         ${NAME}.background:grey88
         ${NAME}.foreground:black
         ${NAME}.pointerColor:black
         ${NAME}.cursorColor:RoyalBlue3
         ${NAME}.geometry:94x38+20+0
         ${NAME}.pane.menubar.marginHeight:0
         ${NAME}.pane.menubar.shadowThickness:1"

while [ ! "$1" = "" ] ; do
  ARG="$1" ; shift
  if [ "${ARG}" = "-xrm" ] ; then
    if [ "$1" = "" ] ; then
      echo "Missing arg to '-xrm'" ; exit 1
    else
      XRMUSER="${XRMUSER}
         $1" ; shift
    fi

  elif [ "${ARG}" = "-nofont" ] ; then
    NOFONT="true"

  else
    ARGLIST="${ARGLIST} ${ARG}"
  fi
done

if [ "${NOFONT}" != "true" ] ; then
  XRMLIST="${XRMLIST}
           ${NAME}.font:*-terminal-medium-r-normal--18-*-iso8859-*"
fi

# Set the COLUMNS variable to something reasonable, and check
# whether we are using a windowed EMACS.
#
export COLUMNS="92"

if [ "${DISPLAY}" != "" ] ; then
  ${EMACS} -xrm "${XRMLIST}
                 ${XRMUSER}" ${ARGLIST}
else
  ${EMACS} -nw $*
fi
