#! /bin/bash
#
# (C) 1999 under GPL version 2, Hans Lermen <lermen@fgan.de>
#
# Make a direct bootable directory 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.

BOOTDIR="/var/lib/dosemu/bootdir.first"
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 your 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 | FreeDosNoIPL ) ;;
  FreeDos*)
  # When FreeDos can boot correctly, remove the *
  echo "
Sorry, but FreeDos can't install this way, you need to use setup-hdimage
"

  exit 1;
  ;;

  *)
  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

${DDIR}/dexe/extract-dos $DEVICE $BOOTDIR \
  -i $IOSYS -m $MSDOSSYS -C $COMCOM

if [ -f $BOOTDIR/boot.bin ]; then
  mv -f $BOOTDIR/boot.bin $BOOTDIR/boot.blk
  mkdir -p $BOOTDIR/dosemu
  cp -fp commands/* $BOOTDIR/dosemu
  echo -ne "device=c:\\\dosemu\\\ems.sys\r\n" >$BOOTDIR/config.sys
  echo -ne "@echo off\r\necho \"Welcome to dosemu 0.98!\"\r\n" >$BOOTDIR/autoexec.bat  
  echo -ne "path c:\\dosemu\032" >>$BOOTDIR/autoexec.bat
fi

