#! /bin/bash
#
# (C) 1997 under GPL version 2, Hans Lermen <lermen@fgan.de>
#
# Make a direct executable (bootable) hdimage getting the DOS from
# your _existing_ native boot partition.
#
# Changes:
#	97/09/10 David Bourgin <dbourgin@wsc.com>
#	- Use /sbin/fdisk, when /sbin not in the path.
#	- Offer non-bootable partitions too.
#	- Move -C option of mdexe to the end of the command to avoid a shell
#	  misunderstanding.

HDIMAGE="hdimage.first"
PROMPTMODE="-o confirm"
COMCOM="command.com"
FDISK=fdisk
if [ -x /sbin/fdisk ]; then
  FDISK=/sbin/fdisk
fi

if [ "$1" = "-n" ]; then
  PROMPTMODE=""
fi

if [ "ISBINRELEASE" = "1" ]; then  # we will have "1" = "1" for bin releases
  DDIR=/var/lib/dosemu
else
  DDIR=`pwd -P`
fi

if ! ${DDIR}/dexe/check-mtools; then
  exit 1
fi

cd $DDIR

echo "checking you partitions, wait..."
PARTS=`$FDISK -l 2>&1| awk '/DOS 16/ || /DOS 12/ {print $1 " " $2}'|tr '* ' 'B:'| grep ':B'`
if [ "$PARTS" = "" ]; then
  PARTS2=`$FDISK -l 2>&1| awk '/DOS 16/ || /DOS 12/ {print $1}'`
fi

echo "...done"

if [ "$PARTS" = "" -a "$PARTS2" = "" ]; then
  echo '
** There seem not to be any bootable DOS partitions
   Do you want to continue any way using a bootable floppy as input?
   If yes then enter the device (e.g. /dev/fd0) else type ENTER
'
  ISFLOPPY="1"
else
 if [ "$PARTS" != "" ]; then
  echo "I've seen the following bootable DOS partitions:"
  PARTS_=$PARTS
 else
  echo "I've seen the following DOS partitions (perhaps non-bootable):"
  PARTS_=$PARTS2
 fi
  echo ""
  for i in $PARTS_; do
    echo "     `echo $i |cut -d: -f1`"
  done
  echo ""
  echo "If none of them are valid, you can continue any way using a bootable"
  echo "floppy as input. Which one do you want to use as input?"
  echo "Type in the full device name or ENTER to cancel"
  echo ""
fi

read DEVICE

#DEVICE=/dev/hda1

if [ "$DEVICE" = "" ]; then exit; fi

if [ "$ISFLOPPY" = "1" ]; then
  echo "Please insert a bootable DOS floppy for $DEVICE and type ENTER"
  read
fi

# we now check what type of DOS we have
set `${DDIR}/dexe/extract-dos $DEVICE`
TYPE=$1
IOSYS=$2
MSDOSSYS=$3

case $TYPE in
  MSDOS | WIN95 | NOVELL | IBMorOpenDos | FreeDos) ;;
  *)
  echo "

Your $DEVICE seems not to contain one of the DOSes that I know."
  echo '
A normal MSDOS system has io.sys and msdos.sys, these are the files that
are needed to boot your system. These apparently are different for your
system, but I need to know their names.

What is the name of the file that corresponds IO.SYS?
'
  read IOSYS

  echo '
What is the name of the file that corresponds MSDOS.SYS?
'
  read MSDOSSYS
  ;;
esac

echo '
A normal MSDOS system has command.com as shell.
If you have a different one, please enter the name,
else just type ENTER
'
read _COMCOM
if [ "" != "" ]; then
  COMCOM=$_COMCOM
fi

echo '
We now try to generate a bootable hdimage into /var/lib/dosemu/hdimage.first
and are calling ./dexe/mkdexe for this.'

if [ "$PROMPTMODE" != "" ]; then
  echo '
You will be prompted to edit the configuration files
(config.sys, autoexec.bat e.t.c).

Enter the name of your favorite editor, if you type just ENTER, the
editor given via the EDITOR enviroment variable will be used.
'

  read MYEDIT
  if [ "$MYEDIT" != "" ]; then
    if hash $MYEDIT >/dev/null 2>&1; then
      EDITOR=$MYEDIT
      export EDITOR
    else
      echo "Sorry, but $MYEDIT seems not to be accessable, using default"
    fi
  fi
fi

echo "Starting ..."


(cd ${DDIR}/dexe; ./mkdexe $HDIMAGE -b $DEVICE -i $IOSYS -m $MSDOSSYS -o noapp $PROMPTMODE -C $COMCOM)

if [ $UID = "0" ] && grep '^dosemu:' /etc/group >/dev/null 2>&1; then
  if [ "${HDIMAGE#/*}" = "$HDIMAGE" ]; then
    HDIMAGE=/var/lib/dosemu/$HDIMAGE
  fi
  chmod 775 $HDIMAGE
  chown root.dosemu $HDIMAGE
fi

echo " ... done"

