
#	ATT:#ident	"sadmin:shell/disklabel	2.13"

#ident	"@(#)sadmin/shell:disklabel	25.1"

#	Write the label to the standard output if it is present.

#!	chmod +x ${file}

#test if yellow page is up
ps -e > /tmp/t1
if grep ypbind /tmp/t1 > /dev/null
then
# if so, is this the yp master server?
	host=`uname -n`					
	master=`ypwhich`
	if test $host = $master; then
		flag=`cat /tmp/t1`
	else
		echo "Must be YP master server to execute disklabel\n"
		rm /tmp/t1
		exit 1
	fi
fi
rm /tmp/t1

usage="Usage:  $0 [ -y |  -q'question' ] [ -w | -x ] parent-pid filename
-q'question'	Provide alternative question.
-w		Check for writability.
-x		No checks or reading of label; for before formatting.
-y		Return the label without asking questions."
unset getit question writechk nochecks mounted

while [ $# -gt 0 ]
do
	case "$1" in
	-q?* )
		question=`expr "$1" : '-q\(.*\)'`
		;;
	-q )
		question=$2
		shift
		;;
	-w )
		writechk=yes
		;;
	-x )
		nochecks=yes
		;;
	-y )
		getit=yes
		;;
	* )
		break
	esac
	shift
done

if [ $# -ne 2 ]
then
	echo >&2 "${usage}"
	exit 1
fi

pid=$1
ddrive=$2

case ${ddrive} in
/dev/r* )
	#  WARNING:  This depends on the common, but not universal, naming
	#  convenetion that all character and block special devices are under
	#  /dev and if /dev/THING is a block device then /dev/rTHING is the
	#  corresponding character (aka "raw") device.  Note that THING may
	#  have directories as part of the name.
	#	if ddrive is raw, bddrive == block device for a raw device
	bddrive=/dev/`expr ${ddrive} : '/dev/r\(.*\)'`
	;;
* )
	#	otherwise they are the same.
	bddrive=${ddrive}
esac

trap 'exit 1' 1 2
trap "	trap '' 1 2
	kill ${pid};  exit 0" 15
flags="-q q -k $$"

#	Determine, with some effort, whether the device is already mounted.
#	Since the same major/minor number of a block device may have several
#	names, we must use them to determine if the drive is mounted.
#	See what the major/minor number of the requested block device is, then
#	look for that among the mounted devices.
majmin=`ls -l ${bddrive} 2>/dev/null  |
	sed -n 's/.*\( [0-9]\{1,\}, *[0-9]\{1,\} \).*/\1/p'`
#	It is possible there is no corresponding block device for a character
#	device.
if [ -n "${majmin}" ]
then
	mounted=`/etc/mount  |  cut -d' ' -f3  |  xargs ls -l 2>/dev/null  |
		sed -n "/${majmin}/s/.* \\(.*\\)/\\1/p"`
fi
if [ -n "${mounted}" ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${mounted} ;s;^\\([^ ]*\\) .*;\\1;p"`
	echo >&2 "\n${ddrive} is already mounted on \"${filesys}\".
This command cannot run with it mounted."
	if [ ${getit} ]
	then
		kill ${pid}
		exit 0
	fi
	if  checkyn ${flags} -H"
	${ddrive} is already in use; it is mounted on ${filesys}.
	Before you can use the drive for some other purpose, you
	must unmount it." \
		-f "Do you want to unmount it?"
	then
		if  diskumount ${mounted} ${filesys}
		then
			:
		else
			echo >&2 "This command cannot complete because \"${filesys}\" is still mounted.
Unmount it and try again."
			kill ${pid}
			exit 0
		fi
	else
		echo >&2 "${ddrive} is still mounted on ${filesys}."
		kill ${pid}
		exit 0
	fi
fi

failchk() {
	if [ ${getit} ]
	then
		kill ${pid}
		exit 0
	fi
}

while true
do
	if [ -n "${question}" ]
	then
		checklist ${flags} -f -D '' "${question}" '<RETURN>' ''
	fi
	if [ ${nochecks} ]
	then
		exit 0
	fi
	#	dd checks that the file is openable for reading.
	if  dd count=1 if=${ddrive} of=/dev/null 2>/dev/null
	then
		:
	else
		echo >&2 "
	${ddrive} can not be read.  Either the drive is not ready,
	or a cable is loose, or there is some other problem.  Check
	it and try again.
"
		failchk
		continue
	fi
#	This writability check WRITES ON THE MEDIUM!!! if it can.
#	(Not pretty, but the only way to do it right now.)
#	if [ ${writechk} ]  
#	then
#		if  dd count=1 if=/etc/passwd of=${ddrive} 2>/dev/null
#		then
#			:
#		else
#			echo >&2 '
#	The medium is not writable.  It may be write-protected,
#	inherently unwritable, or there is some other problem.
#	Check it and try again.
#'
#			failchk
#			continue
#		fi
#	fi
	break
done
if [ -x /etc/fsstat ]
then
	/etc/fsstat ${bddrive} >/dev/null 2>&1
	if [ $? = 3 ]
	then
		exit 0
	fi
fi
/etc/labelit ${ddrive} 2>/dev/null
exit 0
