#!/bin/bash
#
# $Id: diskcopy,v 1.3 2001/06/24 09:04:32 jochen Exp $
#
# diskcopy -- copy any number of disks any number of times
# Copyright (C) 1997, 2001 by Jochen Hein <jochen.hein@jochen.org>
#
# diskcopy 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, or (at your option) any
# later version.
#
# diskcopy 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.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA. 
#
# TODO:
# - i18n
#
error()
{
    echo `basename $0`: $*
    exit 2
}

usage()
{
    echo "Syntax: $0 [ -c copies ] [ -n disks ] [ device ]"
    echo ""
    echo "        -c      copy each disk <copies> times, default is 1"
    echo "        -n      copy <disks> number of disks, default is 1"
    echo "        device  use that device for copying, default is /dev/fd0"
    echo ""
    exit 2
}

set -- `getopt n:c: $*`
if test $? != 0 ; then
    usage
fi

copies=1
disks=1
tmpfile=${TMPDIR:-/tmp}/diskcopy.$$
trap "rm -f $tmpfile" SIGINT

for i ; do
    case "$i" in
	-c)
	    copies=$2; shift; shift;;
	-n)
	    disks=$2; shift; shift;;
        --)
	    shift; break;;
    esac
done

device=${1:-/dev/fd0}

# echo Copying $disks Disks $copies times on device $device

disk=0
while [ $disk -lt $disks ] ; do
    disk=$[$disk+1]
    echo "Insert Source-Disk $disk in device $device"
    read
    dd if=$device of=$tmpfile bs=10240
    rc=$?
    if [ $rc -ne 0 ]; then
	rm -F /tmp/diskcopy.$$
	error "IO-error reading source-disk"
    fi
    copy=0
    while [ $copy -lt $copies ] ; do
	copy=$[$copy+1]
	echo "Insert Target-Disk (copy $copy) for Source-Disk $disk in device $device"
	read
	dd if=$tmpfile of=$device bs=10240
	rc=$?
	if [ $rc -ne 0 ]; then
	    rm -f $tmpfile
	    error "IO-error writing target-disk"
	fi
    done
done

rm -f $tmpfile

echo "Copy finished"
exit 0