#!/bin/bash
init()
{
	SYS=`uname`
	# Currently blocksize has to be 1024, because of a bug in 
	# the cygwin version of df ... it cannot count 512 byte blocks
	# It could be possible to get round this by using the NT chkdsk
	# but this would mean putting special code for each system
	# in order to determine the free space on a device ...
	BLKSIZ=1024
	if [ -f $1 ] ; then
		# ensure that all files in backup set have identical timestamp
		NOW=`date +%Y%m%d%H%M.%S`
		FILE=$1
		SIZE=`ls -l $FILE | awk '{print $5}'`
	else
		echo "Can't access file:" $1
		exit 1
	fi
	#########################################################
	# Specify MOUNT UMOUNT and DRV for each system here .... #
	REMAIN=`echo $SIZE $BLKSIZ | \
		awk '{print int( ($1 + ($2 - 1) ) / $2 )}'`
	SIZE=$REMAIN
	MAXFREE=1423
	if [ $SYS"X" = "LinuxX" ] ; then
		DRV=/mnt/floppy
		TGT=/mnt/floppy
		MOUNT="mount /dev/fd0"
		UMOUNT="umount /dev/fd0"
	# elif [ $SYS"X" = "CYGWIN_NT-4.0X" ] ; then
	elif [ $SYS"X" = "CYGWIN_NT-5.0X" ] ; then
		DRV=a:
		TGT=/tmp
		DOSTMP=`cygpath -w $TGT`
		MOUNT='rm -f /tmp/FDCUT.???'
		UMOUNT="xcopy /N $DOSTMP\\FDCUT.??? a:"
		MAXFREE=1422
	elif [ $SYS"X" = "FreeBSDX" ] ; then
		DRV=/fd0
		TGT=/fd0
		MOUNT="mount -t msdos /dev/fd0 /fd0"
		UMOUNT="umount /fd0"
	else
		echo "Unknown System"
		exit 1
	fi
	x=`echo $REMAIN $MAXFREE | awk '{print int(($1+$2-1)/$2)}'`
	echo "Estimated $x disks required (1440K each)."
	#########################################################
}

# ----------------------------------------------------------------------------

ask()
{
	while true ; do
		echo -e $*"? \c"
		read REPLY
		if [ $REPLY"x" = "yx" -o $REPLY"x" = "Yx" ] ; then
			return 0
		elif [ $REPLY"x" = "nx" -o $REPLY"x" = "Nx" ] ; then
			return 1
		fi
	done
}

# ----------------------------------------------------------------------------

chkstat()
{
	if [ $1 -eq 0 ] ; then
		return
	fi
	echo Exiting due to previous error ...
	exit $1
}

# ----------------------------------------------------------------------------

next_disk()
{
	echo $DONE blocks done ... $REMAIN blocks remaining
	echo -e "Insert disk $DISK in $DRV and Press Enter ... \c"
	read NOTHING
	$MOUNT
	if [ $? -ne 0 ] ; then
		return 1
	fi
	rm -Rf $DRV/*
	if [ $? -ne 0 -a $SYS != "CYGWIN_NT-5.0" ] ; then
		return 1
	fi
	FREE=`df -k $DRV | awk 'NR>1{print $4}'`
	FREE=`echo $FREE $MAXFREE | awk '{if($1>$2) print $2; else print $1}'`
	dd if=$FILE of=$TGT/FDCUT.$DISK bs=$BLKSIZ skip=$DONE count=$FREE
	if [ $? -ne 0 ] ; then
		return 1
	fi
	touch -ma -t $NOW $TGT/FDCUT.$DISK
	return $?
}

# ----------------------------------------------------------------------------

# ... Start here

if [ $1"x" = "x" ] ; then
	echo usage: $0 filename
	exit
fi
init $1
chkstat $?
DISK=001
DONE=0
while [ $REMAIN -gt 0 ] ; do
	next_disk
	if [ $? -ne 0 ] ; then
		if ask "Problem with this disk --- Abort" ; then
			$UMOUNT
			exit 1
		fi
	else
		DISK=`echo $DISK | awk '{ printf( "%03d\n", $1 + 1) }'`
		REMAIN=`echo $REMAIN $FREE | \
			awk '{printf("%d",$1 > $2 ? $1 - $2 : 0)}'`
		DONE=`echo $DONE $FREE | awk '{print $1 + $2}'`
		if [ $REMAIN -eq 0 ] ; then
			touch $TGT/FDCUT.ZZZ
		fi
	fi
	$UMOUNT
done
