#	START NEW ARIX SCCS HEADER
#
#	@(#) cpdisk: version 25.1 created on 12/2/91 at 16:55:47
#
#	Copyright (c) 1990 by Arix Corporation
#	All Rights Reserved
#
#	@(#)cpdisk	25.1	12/2/91 Copyright (c) 1990 by Arix Corporation
#
#	END NEW ARIX SCCS HEADER
#

#	ATT: ident	"sadmin:admin/menu/diskmgmt/cpdisk	2.12"

#ident	"@(#)sadmin/admin/menu/diskmgmt:cpdisk	1.1"

#menu# make a copy of a logical disk drive
#help#	If there are two or more logical disks of the same type on the
#help#	system, you can use this subcommand to copy the contents of one
#help#	disk to another.  When copying between different sized drives,
#help#	the smaller one dominates.

trap 'exit 0' 1 2  15

unset patterns
lspatterns=/dev/rdsk
dir=${0}.d
if [ -d ${dir} ]
then
	patterns=`/bin/ls ${dir} 2>/dev/null`
	lspatterns=`echo "${patterns}"  |  sed 's;.*;/dev/rdsk/&*;'`
fi

# This script calls dcopy which prefers source to be character, and
# dest to be a block device.

sdargs_source="-c $$ /dev/rdsk ${patterns}"
sdargs_dest="-b $$ /dev/dsk ${patterns}"
cpcmd=/etc/dcopy

ddrive_source=`seldevice -q 'Select the drive you wish to copy from:' ${sdargs_source}`
ddrive_dest=`seldevice -q 'Select the drive you wish to copy to:' ${sdargs_dest}`

if [ ${ddrive_source} = ${ddrive_dest} ]
then
	echo 'You cannot copy to and from the same drive.
Try again.'
	exit 0
fi
#	Make sure neither drive has a file system mounted on it
#	To do fsstat, the device must be a block device.

ddrive_tmp=`echo ${ddrive_source} | sed -e 's/rdsk/dsk/'`

/etc/fsstat ${ddrive_tmp} > /dev/null
exit=$?

if [ ${exit} -eq 3 ]
then
	echo >&2 "\n/etc/fsstat: unexpected error for ${ddrive_source}. Contact your System Administrator."
	exit 1
fi

if [ ${exit} -eq 2 ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${ddrive_source} ;s;^\\([^ ]*\\) .*;\\1;p"`
	if [ -z "${filesys}" ]
	then
		echo >&2 "${ddrive_tmp} is mounted. It needs to be unmounted before it can be checked."
		exit 1
	fi
fi

/etc/fsstat ${ddrive_dest} > /dev/null
exit=$?

if [ ${exit} -eq 3 ]
then
	echo >&2 "\n/etc/fsstat: unexpected error for ${ddrive_dest}. Contact your System Administrator."
	exit 1
fi

if [ ${exit} -eq 2 ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${ddrive_dest} ;s;^\\([^ ]*\\) .*;\\1;p"`
	if [ -z "${filesys}" ]
	then
		echo >&2 "${ddrive_dest} is mounted. It needs to be unmounted before it can be checked."
		exit 1
	fi
fi

if ${cpcmd} ${ddrive_source} ${ddrive_dest}
then	echo 'Copy completed.'
	exit 0
else	echo 'Copy failed.'
	exit 1
fi
