#!/bin/bash

###########################################################################
#
#	Shell program to copy floppy disks.
#
#	Copyright 2000 - 2001, Jogeir Lindseth <jogll@frisurf.no>.
#
#	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:
#
#	Version 0.7.
#
#	This is a program to copy floppy disks
#	It makes use of the dd command and make an exact floppy copy,
#	including the boot-sector.
#
#
#
#	Usage:
#
#		diskcopy [ -h | --help ]
#               diskcopy [ -v | --version ]
#		diskcopy [ n ] will make n copies of th floppy.  
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#               -v, --version   Display version and exit. 
#
#	Revisions: See CHANGELOG.diskcopy
#
#	06.08.2000	File created.
#              
#	$Id: diskcopy,v 1.1.1.1 2001/03/24 13:50:36 bshotts Exp $ diskcopy
#
###########################################################################


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

PROGNAME=$(basename $0)
VERSION="version 0.7"
IMAGE_FILE=${HOME}/floppy.img
EMAIL="<jog-l@frisurf.no>"

# Change this to your floppy device if needed.
FLOPPY_DRIVE=/dev/fd0
#FLOPPY_DRIVE=/dev/fd1


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

function floppyimage_tohdd
{

        #####
        #       Function that writes floppy.img to hdd.
        #       No arguments.
        #####

	# Writing info on screen.
	echo	
	echo "Insert source diskette in $FLOPPY_DRIVE and press Enter to continue."
	echo "Ctrl-C to quit."

	# Waiting for source diskette.
	read

	# Copying image to hdd.
	dd if=$FLOPPY_DRIVE of=$IMAGE_FILE || error_exit "Cannot read source disk."
}


function hddimage_tofloppy
{

	#####
	#	Function writes hdd floppy.img to floppy device.
	#	No arguments.
	#####

	# Writing info on screen.
	echo
	echo "Insert target diskette in $FLOPPY_DRIVE and press Enter to continue."
	echo "Ctrl-C to quit."

	# Waiting for target diskette.
	read

	# Copying floppy.img to floppy device.
	dd if=$IMAGE_FILE of=$FLOPPY_DRIVE || error_exit "Cannot write to target disk."

}


function onemore
{

	#####
	#	Function that ask if you want to copy onemore floppy.
	#	No arguments.
	#####

	# Write query on the screen.
	echo
	echo "Do you want to make one more copy of this floppy?"
	echo "Enter yes or no."

	# Reading query.
	read q
		case "$q" in
		y | yes | Y | Yes | YES ) hddimage_tofloppy;;
		n | no | N | No | NO ) graceful_exit;;
		* ) echo "Answer yes or no."
		esac
onemore
}


function clean_up
{

	#####	
	#	Function to remove temporary files and other housekeeping.
	#	No arguments.
	#####
	
	rm -f $IMAGE_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]  [-v | --version]"
}

function version
{
	#####
	#	Function to display version.
	#	No arguments.
	#####

echo "${PROGNAME} ${VERSION} by Jogeir Lindseth ${EMAIL}"
}

function multicopy
{

        #####
        #       Function multicopy form command line argument.
        #       No arguments.
        #####

        # Create hd image
        floppyimage_tohdd

		while [ $param -gt 0 ]; do
		hddimage_tofloppy
		param=$((param - 1))
		echo -e "\n$param More to go."
	done

	graceful_exit
}

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

	${PROGNAME} ${VERSION}	
	by Jogeir Lindseth


	This is a program to copy floppy disks.
	
	$(usage)
	
	Options:
	
	-h, --help	Display this help message and exit.

	-v, --version	Display version and exit.
	

	-EOF-
}
	
###########################################################################
#	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
##

# Set param to integer variable.
typeset -i param  

# Set param to command line argument.
param=$1

# Start function multicopy if command line argument is a number.
	if [ "$param" =  "$1" ]; then    
		if [ $param -gt 0 -a $param -lt 100 ]; then
			multicopy
		else
			error_exit "number of copies must be from 1 to 99"
		fi
	fi                   

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

# Print version.
	if [ "$1" = "--version" ]; then
        	version
        graceful_exit
	fi

                                     
###
# Process arguments.
###

while getopts ":h:v" opt; do
	case $opt in

		h | help )	helptext
				graceful_exit ;;
		v | version )	version
				graceful_exit ;;
		* )	usage
			exit 1
	esac
done

   

floppyimage_tohdd

hddimage_tofloppy

onemore

echo Finish copying floppies

# End file diskcopy.


syntax highlighted by Code2HTML, v. 0.9.1