#!/bin/bash
#
# Author: Murali Krishnan Ganapathy and Carsten Grohmann
#
# Licence: GPL
#
# original version downloaded from
#     http://people.cs.uchicago.edu/~gmurali/gui/files/mkfloppyimg.sh
#
# this version: downloaded from:
#     http://www.carstengrohmann.de/
#
# $Id: mkfloppyimg.sh,v 1.3 2005/07/14 16:56:49 carsten Exp $
#
# History:
#  $Log: mkfloppyimg.sh,v $
#  Revision 1.3  2005/07/14 16:56:49  carsten
#  - add error checking after each command
#
#  Revision 1.2  2004/10/02 16:31:16  carsten
#  - add switch -2880 to use 2880k floppys
#  - add usage
#
#  Revision 1.1  2004/09/30 18:47:48  carsten
# - original version from Murali krishnan GANAPATHY
# - download from http://people.cs.uchicago.edu/~gmurali/gui/files/mkfloppyimg.sh

#set -x

# prints an error text and exits the script
function ERROR {
  echo "$@"
  exit 1
}

# path to mkdosfs
MKDOSFS=$(which mkdosfs 2> /dev/null)

# ---- NO changes below this line required ----

# check syntax
case "$1" in
  -h|--help|-?|/?)
    echo "mkfloppyimg.sh"
    echo "Usage: mkfloppyimg.sh -2880|Size OldImage NewImage "
    echo "Extend the size of a floppy image formated with FAT "
    echo " -2880    - set the size of the new image to 2880 kB"
    echo " Size     - size of the new image in MB"
    echo " OldImage - filename of an existing bootable floppy image"
    echo " NewImage - filename of the new image"
    exit
    ;;
esac

# calculate the size
if [ "$1" -eq "-2880" ]; then
  SIZEKB=2880
else
  ((SIZEKB= ($1 << 10)))
fi
((NUMSECT= SIZEKB << 1))

# check files
if [ -e $2 ]; then
  OLDIMAGE=$2
else
  echo "ERROR: Image $2 does not exist. Please select an existing floppy"
  echo "       image and try again."
  exit 1
fi

if [ "$3" ]; then
  NEWIMAGE=$3
else
  echo "ERROR: Destination image not set. Please set this option and"
  echo "       try again."
  exit 2
fi

# check mkdosfs
if [ -z "$MKDOSFS" ]; then
  echo "ERROR: mkdosfs \"${MKDOSFS}\" not found."
  exit 3
fi
if [ ! -x "$MKDOSFS" ]; then
  echo "ERROR: mkdosfs \"${MKDOSFS}\" not executable."
  exit 4
fi

# Create a mountpoint
MNTNEW=`mktemp -d /tmp/new.XXXXXX` || \
  ERROR "Can't create mountpoint of the new image."

# Create a mountpoint
MNTOLD=`mktemp -d /tmp/old.XXXXXX` || \
  ERROR "Can't create mountpoint of the old image."

# Create the filesystem
OUTLINE=`$MKDOSFS -I -v -C $NEWIMAGE $SIZEKB | grep heads`
HEADS=`echo $OUTLINE | cut -f3 -d' '`
SECTORS=`echo $OUTLINE | cut -f6 -d' '`
((CYLINDERS= NUMSECT / HEADS / SECTORS ))

# Copy contents from old image to new image
mount -o loop $NEWIMAGE $MNTNEW || ERROR "Can't mount the new image."
mount -o loop $OLDIMAGE $MNTOLD || ERROR "Can't mount the old image."
cp -r $MNTOLD/* $MNTNEW || \
  ERROR "during copiing data from the old image to the new image."
umount $MNTNEW  || ERROR "Can't umount new image."
umount $MNTOLD  || ERROR "Can't umount old image."
rmdir $MNTNEW   || ERROR "Can't delete mountpoint of the new image."
rmdir $MNTOLD   || ERROR "Can't delete mountpoint of the old image."

# Create the boot sector of the new image
# copy the first 512 bytes from the OLD image except the bytes 11
# through 61 (inclusive)
dd if=$OLDIMAGE of=$NEWIMAGE bs=1 count=10 conv=notrunc 2>/dev/null || \
  ERROR "Can't copy first part of the bootsector to the new image."
dd if=$OLDIMAGE of=$NEWIMAGE bs=1 skip=61 seek=61 conv=notrunc count=451 2>/dev/null || \
  ERROR "Can't copy second part of the bootsector to the new image."
  
# Print the details
echo "Your floppy image is in $NEWIMAGE."
if [ $SIZEKB -ne 2880 ]; then
  echo "When using it in iso/pxelinux, dont forget to add"
  echo "\"floppy c=$CYLINDERS s=$SECTORS h=$HEADS\" to the argument of memdisk."
fi

