#
# (C) Copyright 1992, ..., 2007 the "DOSEMU-Development-Team".
#
# for details see file COPYING.DOSEMU in the DOSEMU distribution
#
# this file is concatenated behind either dosemu.systemwide (make)
# or dosemu.bindist (mkbindist).
#

usage () {
  echo "
USAGE:

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

THISDIR=$PWD
ARG0="$0"

unset DUMBTERM STRING_I LREDIRHOME QUIET USE_SUDO INSTALL INPUT OPTS

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
      ;;
    -install)
      INSTALL="-i"
      if [ -n "$2" -a -n "${2%%-*}" ]; then
        INSTALL="-i $2"
        shift
      fi
      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 -z "${2%%-*}" ]; then usage; fi
      STRING_I="$STRING_I $2"
      shift
      shift
      ;;   
    -s)
      USE_SUDO=1
      SUFFIX="$SUFFIX \"$1\""
      shift
      ;;   
    *)
      # this should avoid avoid resulting substitution odds
      SUFFIX="$SUFFIX \"$1\""
      shift
      ;;
  esac
done

mkdir -p ~/.dosemu
BPATH_FILE=~/.dosemu/bindist_path

BOOT_DIR_PATH=$THISDIR
# check whether this user has already got his private installation
if [ -f $BPATH_FILE ]; then
  BOOT_DIR_PATH=`cat $BPATH_FILE`
      echo "
  Upgrading an existing dosemu configuration: your local configuration can now
  be stored in ~/.dosemurc instead of $BOOT_DIR_PATH/conf/dosemu.conf
  and the boot.log file is in $HOME/.dosemu.
      "
  rm -f $BPATH_FILE
  test -z "$INSTALL" && INSTALL="-i"
fi

get_binary

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=-X
  fi
fi

if [ -n "$XFLAG" -o -n "$DISPLAY" -a -z "$DUMBTERM" ]; then
  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 -n "$SYSTEM_CONF_PATH" -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
     if ! xset +fp $SYSTEM_XFONTS_PATH 2>/dev/null; then
	# messed up font path -- last resort
        xset fp default
        xset +fp $SYSTEM_XFONTS_PATH 2>/dev/null
     fi
     xset fp rehash
  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
else
  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 -a -z "$DUMBTERM" -a -z "$QUIET" ]; then
      echo "
   Note that DOS needs 25 lines. You might want to enlarge your
   window before continuing.
"
    echo
    echo "   Now type ENTER to start DOSEMU or <Ctrl>C to cancel"
    read dummy
  fi
fi

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

if [ `id -ur` = 0 -a -f $HOME/.dosemurc ]; then
  # User is logged in as root. Need to check whether the config files
  # are safe and not world writeable

  if /bin/ls -l $HOME/.dosemurc 2> /dev/null | /bin/grep ^........w > /dev/null; then
    echo "
  You are logged in as user root. The local configuration within
  this DOSEMU installation is world writeable and may compromise your system.
  Either use a normal unprivileged user installation or protect the following
  directories/files against everybody except root:
	$HOME/.dosemurc
"
    exit 1
  fi
fi

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
  # we should not ignore dosemu.conf if invoked via sudo
  IGNORE_DOSEMU_CONF=""
fi

eval "set -- $SUFFIX"
COMMAND="$SUDO $BINARY $IGNORE_DOSEMU_CONF $XFLAG"
DOSEMU_INVOKED_NAME="$0"
export DOSEMU_INVOKED_NAME
if [ -n "$INSTALL" ] ; then
  if [ -n "$STRING_I" ] ; then
    exec $COMMAND "$INSTALL" "-I $STRING_I" "$@"
  else
    exec $COMMAND "$INSTALL" "$@"
  fi
elif [ -n "$STRING_I" ] ; then
  exec $COMMAND "-I $STRING_I" "$@"
else
  exec $COMMAND "$@"
fi
