#! /bin/sh
#
# (C) Copyright 1992, ..., 2007 the "DOSEMU-Development-Team".
#
# for details see file COPYING.DOSEMU in the DOSEMU distribution
#

# the below lines get patched, when a systemwide install is done
SYSTEM_BIN_PATH=/usr/bin

get_binary() {
  BINARY=$SYSTEM_BIN_PATH/dosemu.bin
  if [ -u $BINARY ] ; then
    # we should not ignore dosemu.conf if suid root
    IGNORE_DOSEMU_CONF=""
  fi
}

# ignore dosemu.conf by default for non-systemwide installations
IGNORE_DOSEMU_CONF=""

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

if [ -z "$QUIET" ]; then
  SUFFIX="$SUFFIX \"-p\""
fi

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 [ `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
