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

install_dos () {
  if [ -n "$INSTALL" -o \
    ! -e $HOME/.dosemu/drives/c -o \
    ! -f $HOME/.dosemu/disclaimer ]; then

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

    # we assume this is a fresh unpacked dosemu-freedos distribution
    # In this case this script has to be called _from_ this directory.

    if [ ! -x $BOOT_DIR_PATH/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
  via the symbolic link ~/.dosemu/drives/c and you can call dosemu from 
  everywhere.
"
      exit 1
    fi
    mkdir -p $HOME/.dosemu/drive_c/tmp $HOME/.dosemu/drives
    rm -f $HOME/.dosemu/drives/c $HOME/.dosemu/drives/d
    ln -sf $HOME/.dosemu/drive_c $HOME/.dosemu/drives/c
    ln -sf $BOOT_DIR_PATH/drive_z $HOME/.dosemu/drives/d
    cp -fp $BOOT_DIR_PATH/conf/autoexec.bat $BOOT_DIR_PATH/freedos/autoexec.bat
    cp -fp $BOOT_DIR_PATH/conf/config.sys $BOOT_DIR_PATH/freedos/config.sys
    # the below two cp's are usually also done by dosemu.bin but are done here
    # in case DOSEMU terminates leaving a disclaimer file behind, or
    # if ~/.dosemu/disclaimer exists but ~/.dosemu/drives/c does not.
    cp -fp $BOOT_DIR_PATH/conf/autoexec.bat $HOME/.dosemu/drive_c
    cp -fp $BOOT_DIR_PATH/conf/config.sys $HOME/.dosemu/drive_c
  fi
  unset INSTALL
}

get_binary() {
  install_dos
  BOOT_DIR_PATH=`cd $HOME/.dosemu/drives && ls -l d | sed 's/.* -> //'`/..
  if [ ! -x $BOOT_DIR_PATH/bin/dosemu.bin ]; then
    #older situation
    BOOT_DIR_PATH=`cd $HOME/.dosemu/drives && ls -l c | sed 's/.* -> //'`/..
  fi
  BINARY=$BOOT_DIR_PATH/bin/dosemu.bin

  if [ -n "$DISPLAY" -a -z "$DUMBTERM" ]; then
    if [ ! -f $BOOT_DIR_PATH/Xfonts/fonts.dir ]; then
      (cd $BOOT_DIR_PATH/Xfonts; mkfontdir)
    fi
  fi
  SUFFIX="$SUFFIX --Flibdir $BOOT_DIR_PATH"
}

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