#! /bin/sh
#
# (C) Copyright 1992, ..., 2004 the "DOSEMU-Development-Team".
# (C) Copyright 2001-2004 Herbert Xu <herbert@debian.org>
#
# for details see file COPYING in the DOSEMU distribution
#

SYSTEM_CONF_PATH=/etc/dosemu

usage () {
  echo "
USAGE:

 [x]dosemu [-dumb] [-home] [-quiet] [-input] [-s] [args...]
"
  exit 1
}

ARG0="$0"

unset DUMBTERM STRING_I LREDIRHOME QUIET USE_SUDO INPUT

SUFFIX=""
while [ $# -gt 0 ] ; do
  case $1 in
    -dumb)
      DUMBTERM=1
      OPT_I=-I
      STRING_I=" video{none}"
      shift
      ;;
    -home)
      #doing -home) by default now; leave it here for compatibility
      shift
      ;;
    -quiet)
      QUIET=1
      shift
      ;;
    -input)
      INPUT=1
      if [ -n "$2" -a -n "${2##-*}" ]; then
        STRING_I="$STRING_I keystroke "'"'"$2"'"'
        shift
      fi
      shift
      ;;   
    -I)
      if [ -z "$2" -o "${2-*}" != "$2" ]; then usage; fi
      STRING_I="$STRING_I $2"
      shift
      shift
      ;;   
    -s)
      USE_SUDO=1
      shift
      ;;   
    *)
      # this should avoid avoid resulting substitution odds
      SUFFIX="$SUFFIX \"$1\""
      shift
      ;;
  esac
done

#echo "$PROPRIETARY $LREDIRHOME $DUMBTERM $*"; exit

w4l_check () {
  if grep mki_install_hook /proc/ksyms >/dev/null 2>/dev/null; then
    echo "
   Note: your kernel is patched for Win4Lin, this will most likely
   crash DPMI applications within DOSEMU. ... be warned!
"
  fi
}

mkdir -p ~/.dosemu

unset XFLAG
if [ "`basename $ARG0`" = "xdosemu" ]; then
  if [ -n "$DUMBTERM" ]; then
    echo "
  You requested a dumb terminal together with X!
  ... falling back to non-X
"
  else
    XFLAG=1
  fi
fi

if [ -n "$XFLAG" ]; then
  XFLAG=-X
  if [ -z "$DISPLAY" ]; then
    echo "
  You do not have the DISPLAY variable set, but want to run DOSEMU
  in its own X-window. Set the DISPLAY variable such as

      DISPLAY=:0.0; export DISPLAY

  if running X locally or

      DISPLAY=host:0.0; export DISPLAY

  when running remotely ('host' being the machine where you are typing at)

  After this run xdosemu again.
"
    exit 1
  fi
  
  if [ -f $HOME/.dosemurc ]; then
    FONT=`grep '$_X_font' $HOME/.dosemurc | \
         sed -e 's|[#[:space:]]*\$_X_font[[:space:]]*=[[:space:]]*\"\(.*\)\".*|\1|'`
  fi

  if [ "$FONT" = "" -a -f SYSTEM_CONF_PATH/dosemu.conf ]; then
     FONT=`grep '$_X_font' $SYSTEM_CONF_PATH/dosemu.conf | \
        sed -e 's|[#[:space:]]*\$_X_font[[:space:]]*=[[:space:]]*\"\(.*\)\".*|\1|'`
  fi

  if [ "$FONT" = "" ]; then
     FONT="vga"
  fi

  if [ "`xlsfonts -o -fn $FONT 2>/dev/null`" = "" ]; then
     echo "
   You do not have the DOSEMU $FONT font installed and are running
   remote X. You need to install the $FONT font on your _local_ Xserver.
   Look at the readme for details. For now we start with an fixed font,
   which does not display all national characters correctly.
   ... be warned
"
  fi
  w4l_check
  if [ -z "$QUIET" -a ! -f $HOME/.dosemu/stamp-xdosemu ]; then
    echo "
   DOSEMU will run in its own X window.
   To terminate it you have three choices:
   - type <Ctrl>-C _here_ (not in the dosemu window)
   - type <Ctrl><Alt><PgDn> in the dosemu window
   - execute 'exitemu' at the DOS prompt"
    touch ~/.dosemu/stamp-xdosemu
  fi
else
  if [ -z "$QUIET" -a ! -f $HOME/.dosemu/stamp-dosemu ]; then
    echo "
   DOSEMU will run on _this_ terminal.
   To exit you need to execute 'exitemu' from within DOS,
   because <Ctrl>-C and 'exit' won't work!"
  fi
  if test -z "$LINES" -o -z "$COLUMNS" ; then
    eval `stty size 2>/dev/null | (read L C; \
          echo LINES=${L:-24} COLUMNS=${C:-80})`
  fi
  test $LINES   -eq 0 && LINES=24
  test $COLUMNS -eq 0 && COLUMNS=80
  if [ $LINES -lt 25 ]; then
      echo "
   Note that DOS needs 25 lines. You might want to enlarge your
   window before continuing.
"
  fi
  w4l_check
  if [ $LINES -lt 25 -o -z "$QUIET" -a ! -f $HOME/.dosemu/stamp-dosemu ]; then
    echo
    echo "   Now type ENTER to start DOSEMU or <Ctrl>C to cancel"
    read dummy
    touch ~/.dosemu/stamp-dosemu
  fi
fi

if [ -z "$DOSDRIVE_D" ]; then
  DOSDRIVE_D="$HOME"
  export DOSDRIVE_D
fi

DSIG=
exittrap='if [ $DSIG ]; then trap - "$DSIG"; kill -s $DSIG $$; fi'
trap '$exittrap' EXIT
for i in HUP INT TERM; do
  trap "DSIG=$i; exit" $i
done

workdir=$(mktemp -u ${TMPDIR:-/tmp}/dosemu.XXXXXX | tr A-Z a-z)
eval $(
  trap '' HUP INT TERM
  if ! mkdir $workdir; then
    echo "
    ERROR:
    $workdir: Failed to create temporary directory.
"
    echo exit 1
    exit
  fi
  echo trap "'rm -r $workdir; $exittrap'" EXIT
)

export DOSTMP=$workdir/tmp
mkdir $DOSTMP

BINARY=dosemu.bin

SUDO=""
# set sudo_uid/sudo_gid so that somebody who does "sudo dosemu"
# really gets root
if [ -n "$SUDO_UID" ] ; then
  SUDO_UID=0
fi
if [ -n "$SUDO_GID" ] ; then
  SUDO_GID=0
fi
if [ -n "$USE_SUDO" ] ; then
  SUDO=sudo
fi

eval "set -- $SUFFIX"
if [ -n "$STRING_I" ] ; then
  $SUDO $BINARY $XFLAG "-I $STRING_I" "$@"
else
  $SUDO $BINARY $XFLAG "$@"
fi
