#! /bin/bash
#
# (C) 1997 under GPL, Hans Lermen <lermen@fgan.de>
#
# Make a direct executable (bootable) hdimage getting the DOS from
# your _existing_ native boot partition.

PROMPTMODE="-o confirm"
COMCOM="command.com"

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

cd $DDIR

echo "checking you partitions, wait..."
PARTS=`fdisk -l 2>&1| awk '/DOS 16/ || /DOS 12/ {print $1 " " $2}'|tr '* ' 'B:'| grep ':B'`
echo "...done"

if [ "$PARTS" = "" ]; 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
  echo "I've seen the following bootable DOS partitions:"
  echo ""
  for i in $PARTS; do
    echo "     `echo $i |cut -d: -f1`"
  done
  echo ""
  echo "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.first -b $DEVICE -i $IOSYS -m $MSDOSSYS -C $COMCOM -o noapp $PROMPTMODE)

echo " ... done"

