#!/bin/bash

###########################################################################
#
#	Shell program to format a floppy diskette.
#
#	Copyright 2000-2002, William Shotts <bshotts@users.sourceforge.net>.
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of the
#	License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful, but
#	WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#	General Public License for more details.
#
#	This software is part of the LinuxCommand.org project, a site for
#	Linux education and advocacy devoted to helping users of legacy
#	operating systems migrate into the future.
#
#	You may contact the LinuxCommand.org project at:
#
#		http://www.linuxcommand.org
#
#	Description:
#
#	This program formats floppy diskettes and creates either VFAT
#	(MSDOS with long file support) or ext2 (Linux native) file systems.
#
#	Usage:
#
#		format_floppy -h | --help | [-f type] -d drive | a: | b:
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#		-f  type        filesystem type - msdos (default) or ext2
#		-d  drive       drive (a or b)
#
#
#	Revisions:
#
#	04/23/2000	Program created
#	02/17/2002	Extensive modernizing (1.0.1)
#
#	$Id: format_floppy,v 1.2 2002/02/18 14:18:38 bshotts Exp $
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION='1.0.1'
TEMP_FILE=/tmp/${PROGNAME}.$$

# Drives defined for this system.  You may need to edit this for your
# own system.

DRIVE_A=/dev/fd0 # a: is drive 0 (3.5 inch HD)
DRIVE_A_SIZE=1440
DRIVE_B=/dev/fd1 # b: is drive 1 (5.25 inch HD)
DRIVE_B_SIZE=1200


###########################################################################
#	Functions
###########################################################################


function clean_up
{

	#####
	#	Function to remove temporary files and other housekeeping
	#	No arguments
	#####

	rm -f ${TEMP_FILE}
}


function graceful_exit
{
	#####
	#	Function called for a graceful exit
	#	No arguments
	#####

	clean_up
	exit
}


function error_exit
{
	#####
	# 	Function for exit due to fatal program error
	# 	Accepts 1 argument
	#		string containing descriptive error message
	#####


	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	#####
	#	Function to perform exit if interrupt signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	#####
	#	Function to display usage message (does not exit)
	#	No arguments
	#####

	echo "Usage: ${PROGNAME} -h | --help | [-f type] -d drive | a: | b:"
}


function helptext
{
	#####
	#	Function to display help message for program
	#	No arguments
	#####
	
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

	${PROGNAME} ver. ${VERSION}	
	This is a program to format a floppy diskette.
	
	$(usage)
	
	Options:

	-h, --help	Display this help message and exit.
	-f  type        filesystem type - msdos (default) or ext2
	-d  drive       drive (a or b)
		
	-EOF-
}	


function format_drive
{
	#####
	#	formats drive and creates file system
	#	Arguments:
	#		1	device (required)
	#		2	filesystem type (required)
	#####

	# Fatal error if required arguments are missing

	if [ "$1" = "" ]; then 
		error_exit "format_drive: missing argument 1"
	fi
	if [ "$2" = "" ]; then 
		error_exit "format_drive: missing argument 2"
	fi

	case $2 in
		msdos|vfat )	fdformat $1 || error_exit "format failed"
				/sbin/mkfs.vfat $1 || error_exit "filesystem creation failed"
				;;
		ext2 )		fdformat $1 || error_exit "format failed"
				/sbin/mkfs.ext2 $1 || error_exit "filesystem creation failed"
				;;
		* )		usage
				exit 1
	esac

}	# end of format_drive


###########################################################################
#	Program starts here
###########################################################################

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT

# Process command line arguments

if [ "$1" = "--help" ]; then
	helptext
	graceful_exit
fi

# Process arguments

fs_type=vfat
device=

# Display usage message if no options or arguments are given

if [ "$1" = "" ]; then
	usage
	exit 1
fi

# Process possible option/argument combinations

while getopts ":hf:d:" opt; do
	case $opt in
		f )	case $OPTARG in
				msdos)	fs_type=vfat
					;;
				vfat)	fs_type=vfat
					;;
				ext2)	fs_type=ext2
					;;
				*)	error_exit "unknown filesystem"
					;;
			esac
			;;
		d )	case $OPTARG in
				a | a:)	device=$DRIVE_A
					;;
				b | b:)	device=$DRIVE_B
					;;
				* )	usage
					exit 1
					;;
			esac
			;;
		h )	helptext
			graceful_exit
			;;
		* )	usage
			exit 1
			;;
	esac
done
shift $(($OPTIND - 1))

# Allow for the simple case of a drive letter alone

case $1 in
	a | a:)	device=$DRIVE_A
		;;
	b | b:)	device=$DRIVE_B
		;;
esac


if [ "$device" = "" ]; then
	usage
	exit 1
fi
if [ ! -b $device ]; then
	error_exit "device: $device does not exist"
fi
if [ ! -w $device ]; then
	error_exit "cannot write to device: $device, permission denied"
fi

echo -e "\nFormatting device: $device with file system: $fs_type ...\n"
format_drive $device $fs_type

graceful_exit


syntax highlighted by Code2HTML, v. 0.9.1