#
# (C) Copyright 1992, ..., 2014 the "DOSEMU-Development-Team".
#
# for details see file COPYING 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] [-cdrom path] [-s] [-install [nameofbootdir]] [args...]
"
  exit 1
}

THISDIR=$PWD
ARG0="$0"

unset STRING_I USE_SUDO INSTALL INPUT OPTS

SUFFIX=""
while [ $# -gt 0 ] ; do
  case $1 in
    -dumb)
      OPTS="$OPTS -td"
      shift
      ;;
    -home)
      export DOSDRIVE_EXTRA="$HOME"
      shift
      ;;
    -cdrom)
      export CDROM_PATH="$2"
      shift
      shift
      ;;
    -install)
      INSTALL="-i"
      if [ -n "$2" -a -n "${2%%-*}" ]; then
        INSTALL="-i $2"
        shift
      fi
      shift
      ;;
    -quiet)
      OPTS="$OPTS -q"
      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
LOG_FILE=~/.dosemu/boot.log
MAX_LOGS=3

if [ -f $LOG_FILE ]; then
  NL=`ls $LOG_FILE.? 2>/dev/null | tail -n 1 | xargs basename 2>/dev/null | cut -d "." -f 3`
  if [ -z "$NL" ]; then
    NL="1"
  elif [ $NL -ge $MAX_LOGS ]; then
    NL="1"
  else
    NL=`expr $NL + 1`
  fi
  mv -f $LOG_FILE $LOG_FILE.$NL
fi

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 $0

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

# for address sanitizer
export ASAN_OPTIONS=handle_segv=0:abort_on_error=1

eval "set -- $SUFFIX"
COMMAND="$SUDO $SUDOOPTS $BINARY $IGNORE_DOSEMU_CONF $OPTS"
DOSEMU_INVOKED_NAME="$0"
export DOSEMU_INVOKED_NAME
if [ -n "$INSTALL" ] ; then
  COMMAND="$COMMAND $INSTALL"
fi
if [ -n "$STRING_I" ] ; then
  COMMAND="$COMMAND -I $STRING_I"
fi

exec 3>&1
# in a non-bash case, $BASHPID won't work and we fail the cleanup - no problem
DPID=$(echo $BASHPID; exec 1<&3; exec $COMMAND "$@")
EC=$?
exec 3>&-
RUNDIR=$HOME/.dosemu/run
STALE=`ls $RUNDIR/*.$DPID 2>/dev/null`
if [ -n "$STALE" ]; then
  echo "removing stale files:"
  echo "$STALE"
  rm -f $STALE
  stty sane
fi

if [ $EC -gt 128 ]; then
  SIG=`expr $EC - 128`
  echo "Terminated with signal $SIG"
fi

exit $EC
