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

usage() {
  echo "
USAGE:

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

ARG0="$0"

unset DUMBTERM STRING_I LREDIRHOME QUIET

i=0;
while [ $(( $# > 0 )) = 1 ] ; do
#NOTE, this won't work on older BASHes: while (( $# )); do
  case $1 in
    -dumb)
      DUMBTERM=1
      OPT_I=-I
      STRING_I=" video{none}"
      shift
      ;;
    -home)
      LREDIRHOME=1
      shift
      ;;
    -quiet)
      QUIET=1
      shift
      ;;
    -input)
      if [ -z "$2" -o "${2-*}" != "$2" ]; then usage; fi
      STRING_I="$STRING_I keystroke "'"'"$2"'"'
      shift
      shift
      ;;   
    -I)
      if [ -z "$2" -o "${2-*}" != "$2" ]; then usage; fi
      STRING_I="$STRING_I $2"
      shift
      shift
      ;;   
    *)
      # process and accumulate remaining dosemu.bin args into an array
      # in order to avoid resulting substitution odds
      par[$i]=$1
      if [ -z "par[$i]" ]; then par[$i]="''"; fi
      i=$(( $i + 1 ))
      shift
      ;;
  esac
done


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

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

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

      export DISPLAY=:0.0

  if running X locally or

      export DISPLAY=host:0.0

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

  After this run xdosemu again.
"
    exit 1
  fi
  if [ "`xlsfonts -fn vga 2>/dev/null`" = "" ]; then
    echo "
   You do not have the DOSEMU vga font installed and are running
   remote X. You need to install the vga 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" ]; 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"
    if [ -n "$LREDIRHOME" ]; then
      echo "   Redirecting \$HOME to DOS drive D:"
    else
      echo
      echo "   Hint: if you want \$HOME as DOS drive D:, use '-home' option"
    fi
  fi
else
  if [ -z "$QUIET" ]; 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!"
    if [ "$TERM" = "xterm" ]; then
      echo "
   Note, that DOS needs 25 lines and xterm per default has only 24,
   so you might want to enlarge it before continuing.
"
    fi
  fi
  w4l_check
  if [ -z "$QUIET" ]; then
    if [ -n "$LREDIRHOME" ]; then
      echo "   Redirecting \$HOME to DOS drive D:"
    else
      echo
      echo "   Hint: if you want \$HOME as DOS drive D:, use '-home' option"
    fi
    echo
    echo "   Now type ENTER to start DOSEMU or <Ctrl>C to cancel\n"
    read dummy
  fi
fi

if [ -n "$LREDIRHOME" ]; then
  export DOSDRIVE_D="$HOME"
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
LOG=$workdir/boot.log
MAINARGS="$XFLAG -O"

if [ -z "$STRING_I" ]; then
  $BINARY 2>$LOG $MAINARGS "${par[@]}"
else
  $BINARY 2>$LOG $MAINARGS -I "$STRING_I" "${par[@]}"
fi

if [ -n "XFLAG" ]; then
 if grep -E '^Xlib.*refused by server' $LOG>/dev/null 2>&1; then
   echo "
    ERROR:
    You seem not to have enough rights to connect to your Xserver.
    Force brut counter mesure (not recommended):

       xhost +

    on the machine you just are sitting in front of ...
    ... but better read the X documentation for a more secure solution.
 "
   exit 1
 fi
 if grep -E "^ERROR: X: Can't open display" $LOG>/dev/null 2>&1; then
   echo "
    ERROR:
    There is something wrong with the contents of your DISPLAY variable,
    the defined X server cannot be reached. Please re-check it.
 "
   exit 1
 fi
fi

exit 0
