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

# the below line gets patched, when a systemwide install is done
SYSTEM_INSTALL_PATH=NOT_SYSTEM_WIDE
SYSTEM_CONF_PATH=NOT_SYSTEM_WIDE
SYSTEM_XFONTS_PATH=NOT_SYSTEM_WIDE
test "$SYSTEM_INSTALL_PATH" != "NOT_SYSTEM_WIDE" && IS_SYSTEM_WIDE=1

function usage {
  echo "
USAGE:

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

function install_from_template {
# $1 = path to install to
  (cd $1
    tar -xzf $SYSTEM_INSTALL_PATH/dosemu-bin.tgz
  )
  if [ -z "$PROPRIETARY" ]; then
    (cd $1
      cat $SYSTEM_CONF_PATH/dosemu.conf | \
       sed -e "s#\#\([[:space:]]*\$_hdimage[[:space:]]*=[[:space:]]*\"\)freedos#\1$1/dosemu/freedos#" \
	  > $HOME/.dosemurc
      tar -xzf $SYSTEM_INSTALL_PATH/dosemu-freedos-bin.tgz
    )
  else
    (cd $1
      mv dosemu/freedos/dosemu dosemu/commands
      rmdir dosemu/freedos
      cat $SYSTEM_CONF_PATH/dosemu.conf | \
          sed -e "s#\#\([[:space:]]*\$_hdimage[[:space:]]*=[[:space:]]*\"\)freedos#\1$PROPRIETARY#" \
	  > $HOME/.dosemurc
    )
    echo "
  The DOSEMU part of the proprietary installation has been done.

  It is now your part to install the bootable part '$PROPRIETARY' to
  $1/dosemu/$PROPRIETARY.
  Read the docs for details on what it must contain. You also should install a
  symlink to $1/dosemu/commands in order
  to make the DOSEMU support commands available within $PROPRIETARY.

  After you finished installing $PROPRIETARY (which simply may be a symlink
  to your mounted DOS partition) _and_ have checked ~/.dosemurc,
  you can rerun [x]dosemu again (without the -install option) to boot this DOS.

  Good luck
"
    exit 0
  fi
}

THISDIR=$PWD
ARG0="$0"

unset DUMBTERM STRING_I LREDIRHOME PROPRIETARY 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
      ;;
    -install)
      if [ -z "$2" -o "${2-*}" != "$2" ]; then usage; fi
      PROPRIETARY=$2
      shift
      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

function 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
}

BPATH_FILE=~/.dosemu/bindist_path

# check whether this user has already got his private installation
if [ ! -f $HOME/.dosemurc ]; then

  # This either is the first time this user calls 'dosemu'
  # or the user cd'ed here and we forced a 'take over'

  if [ -z "$IS_SYSTEM_WIDE" ]; then
    # we assume this is a fresh unpacked dosemu-freedos distribution
    # In this case this script has to be called _from_ this directory.

    if [ ! -x bin/dosemu.bin ]; then
      echo "
  You are starting this script from the wrong directory.
  e.g. if you unpacked dosemu-freedos-bin.tgz within
  $HOME/mydos, you have to 'cd' into
  $HOME/mydos/dosemu before running this script.
  Once you have successfully booted, this directory will be remembered
  in the file ~/.dosemu/bindist_path and you can call dosemu from everywhere.
"
      exit 1
    fi
    BOOT_DIR_PATH=$THISDIR
    mkdir -p ~/.dosemu
    cat $BOOT_DIR_PATH/conf/dosemurc.example | \
       sed -e "s#\#\([[:space:]]*\$_hdimage[[:space:]]*=[[:space:]]*\"\)freedos#\1$BOOT_DIR_PATH/freedos#" \
	  > $HOME/.dosemurc
    echo $BOOT_DIR_PATH > $BPATH_FILE

  else

    # This script was called from a systemwide installation

    if [ ! -d $SYSTEM_INSTALL_PATH ]; then
      echo "
  There is something wrong with your systemwide DOSEMU installation:
  $SYSTEM_INSTALL_PATH is not existing, but was configured to contain
  the DOSEMU distribution templates.
"
      exit 1;
    fi

    if [ -f $BPATH_FILE ]; then
      BOOT_DIR_PATH=`cat $BPATH_FILE`
      echo "
  Upgrading an existing dosemu configuration: your local configuration is now
  in ~/.dosemurc instead of $BOOT_DIR_PATH/conf/dosemu.conf
  and the boot.log file is in $HOME/.dosemu.
  Please adapt ~/.dosemurc to your situation; the old settings are not kept.
      "
      cat $SYSTEM_CONF_PATH/dosemu.conf | \
       sed -e "s#\#\([[:space:]]*\$_hdimage[[:space:]]*=[[:space:]]*\"\)freedos#\1$BOOT_DIR_PATH/freedos#" \
	  > $HOME/.dosemurc
    else
      # We copy the binaries from the directory SYSTEM_INSTALL_PATH
      # to the users $HOME.
      echo "
  Going to install your private DOSEMU files into the directory
  $HOME/dosemu
  Enter an empty string to confirm, a new path, or "none" (without
  the quotes) if you don't want this:"
      read BOOT_DIR_PATH
      if [ "$BOOT_DIR_PATH" == "none" ]; then
	if [ ! -f $HOME/.dosemu/disclaimer ]; then
          echo "
  Please put the paths of your DOSEMU DOS drives in \$_hdimage in
  $SYSTEM_CONF_PATH/dosemu.conf or copy that file to $HOME/.dosemurc and
  adjust it there if you haven't already done so.
          "
          touch ~/.dosemurc
        fi
      elif [ "$BOOT_DIR_PATH" != "" ]; then
        if [ "${BOOT_DIR_PATH#/*}" != "$BOOT_DIR_PATH" ]; then
          echo "  You gave an absolute path, type ENTER to confirm or Ctrl-C"
          read x
        fi
        echo "
  Ok, will install into $BOOT_DIR_PATH/dosemu
  type ENTER to confirm or Ctrl-C to abort"
        read x
        if ! mkdir -p $BOOT_DIR_PATH 2>/dev/null; then
          echo "  unable to create $BOOT_DIR_PATH, giving up"
          exit 1
        fi
        install_from_template $BOOT_DIR_PATH
        BOOT_DIR_PATH="$BOOT_DIR_PATH/dosemu"
      else
        install_from_template $HOME
        BOOT_DIR_PATH="$HOME/dosemu"
      fi
    fi
  fi
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
  
  FONT=`grep '$_X_font' $HOME/.dosemurc | \
        sed -e 's|[#[:space:]]*\$_X_font[[:space:]]*=[[:space:]]*\"\(.*\)\".*|\1|'`

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

  if [ -z "$IS_SYSTEM_WIDE" ]; then
     BOOT_DIR_PATH=`cat $BPATH_FILE`
     if [ "`xlsfonts -fn $FONT 2>/dev/null`" = "" ]; then
        if [ ! -f $BOOT_DIR_PATH/Xfonts/fonts.dir ]; then
           (cd $BOOT_DIR_PATH/Xfonts; mkfontdir)
        fi
        xset +fp $BOOT_DIR_PATH/Xfonts 2>/dev/null
     fi
  else
     if [ "`xlsfonts -fn $FONT 2>/dev/null`" = "" ]; then
        xset +fp $SYSTEM_XFONTS_PATH 2>/dev/null
     fi
  fi
  if [ "`xlsfonts -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" ]; 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 -e "\n   Hint: if you want \$HOME as DOS drive D:, then use the '-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 -e "\n   Hint: if you want \$HOME as DOS drive D:, use '-home' option"
    fi
    echo -e "\n   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

if [ $UID = 0 ]; 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

if [ -n "$IS_SYSTEM_WIDE" ] ; then
  BINARY=dosemu.bin
else
  BINARY=$BOOT_DIR_PATH/bin/dosemu.bin
fi

if [ -n "$STRING_I" ] ; then
  $BINARY $XFLAG "-I $STRING_I" "${par[@]}"
else
  $BINARY $XFLAG "${par[@]}"
fi

umask 077
exit 0
