#! /bin/sh
#
# Copyright (c) 1996, 1998, 1999, 2000 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# ident	"@(#)disk_clean.sh	7.3	00/01/10 SMI; TSOL 2.x"
#

#
#  This a clean script for the floppy drive and CDROM
# 

USAGE="disk_clean [-s|-f|-i] device cmw_label mode"
PATH="/usr/bin:/usr/sbin"
ALLOCATE="-A"

#
# 		*** Shell Function Declarations ***
#

con_msg() {
   echo "Operator must label disk remaining in $DEVICE with $LABEL" > /dev/console
}

e_con_msg() {
   echo "Error cleaning up $DEVICE, label: $LABEL" > /dev/console
}

floppy_msg() {
   echo "Please label the disk removed from $DEVICE with $LABEL" > /dev/tty
}

cdrom_msg() {
   echo "Please remove the disk from $DEVICE" > /dev/tty
}

e_user_msg() {
   echo "Error cleaning up $DEVICE, label: $LABEL" > /dev/tty
   echo "Please inform system administrator" > /dev/tty
}

alloc_msg() {
	echo "Insert disk into $DEVFILE." > /dev/tty
	echo "Make sure disk is labeled $LABEL." > /dev/tty
}

mk_error() {
   chown bin /etc/security/dev/$1
   chmod 0100 /etc/security/dev/$1
}

confirm() {
	echo "Press RETURN when $DEVICE is ready, or ^C to cancel.. \c"
	echo \\007 > /dev/tty
	read reply
}

#
# get_reply prompt choice ...
#
get_reply() {
	prompt=$1; shift
	while true
	do
		echo $prompt
		read reply
		i=0
		for choice in $*
		do
			if [ "$choice" = "$reply" ]
			then
				return $i
			else
				i=`expr $i + 1`
			fi
		done
	done
}

#
# Find the first disk slice containing a file system
#
find_fs()
{
	for DEVn in $FILES ; do
		x="`fstyp $DEVn 2>/dev/null`"
		if [ $? -eq 0 ]; then
			VOLUME_PATH=$DEVn
			if [ "$x" = hsfs ]; then
				x="`labelit -F hsfs $DEVn 2>&1|grep 'Volume id'`"
				y="`echo $x|cut -f3- -d' '|tr '[A-Z] ' '[a-z]_'`"
				VOLUME_NAME=$VOLUME_USER/$y
				return 0
			fi
		fi
	done
	if [ -n "$VOLUME_PATH" ]; then
		return 0
	else
		return 1
	fi
}

#
# 		*** Begin main program ***
#

# Parse the argumnets

while getopts ifs c
do
   case $c in
   i)   FLAG=$c;;
   f)   FLAG=$c;;
   s)   FLAG=$c;;
   \?)   echo $USAGE
      exit 1 ;;
   esac
done
shift `expr $OPTIND - 1`

# get the map information

DEVICE=$1
LABEL=$2 ; export LABEL
MODE=$3
MAP=`dminfo -v -n $DEVICE`
TYPE=`echo $MAP | cut -f2 -d:`
FILES=`echo $MAP | cut -f3 -d:`

DEVFILE=`echo $FILES | cut -f1 -d" "`

VOLUME_DEVICE=`echo $FILES | cut -f2 -d" "`
VOLUME_FORCEDEJECT=false
VOLUME_MEDIATYPE=`echo $MAP | cut -f1 -d_`
VOLUME_PATH=$DEVFILE
VOLUME_SYMDEV=`echo $DEVICE | sed -e 's/_//'`
VOLUME_USER=`/usr/xpg4/bin/id -u -nr`
VOLUME_NAME=$VOLUME_USER/unnamed_$VOLUME_MEDIATYPE
export VOLUME_ACTION VOLUME_DEVICE VOLUME_FORCEDEJECT VOLUME_MEDIATYPE VOLUME_NAME VOLUME_PATH VOLUME_SYMDEV VOLUME_USER

#
# If this script is being called at device allocate time,
# inform the user to make sure the disk is properly labeled.
# Exit since there is nothing else to do upon allocation.
#
if [ "$MODE" = "$ALLOCATE" ]; then
	alloc_msg
	if [ $VOLUME_MEDIATYPE = floppy ]; then
		a="`eject -q $DEVFILE 2>&1`" # Determine if media is in drive
		z="$?"
		case $z in
		0)			# Media is in drive
			break;;
		1)			# Media is not in drive
			confirm
			break;;
		3)			# Error 
			mk_error $DEVICE
			exit 1;;
		esac

	else
			confirm
	fi

	get_reply "Do you want $DEVICE mounted: (y/n)? \c" y n

	if [ $? = 1 ]; then
		exit 0
	elif [ $? = 0 ]; then
		break
	fi

	chown $VOLUME_USER $VOLUME_PATH
	chmod 755 $VOLUME_PATH
	setlabel " $LABEL " $VOLUME_PATH
	if [ $VOLUME_MEDIATYPE = cdrom ]; then
		find_fs
		chmod 555 $VOLUME_PATH
	fi
	VOLUME_ACTION=insert
	mkdir -p /$VOLUME_MEDIATYPE/$VOLUME_NAME
	chmod 777 /$VOLUME_MEDIATYPE
	setlabel -s "admin_low" /$VOLUME_MEDIATYPE	
	/usr/sbin/rmmount
	chown $VOLUME_USER /$VOLUME_MEDIATYPE/$VOLUME_USER
	chmod 700 /$VOLUME_MEDIATYPE/$VOLUME_USER
	setlabel " $LABEL " /$VOLUME_MEDIATYPE/$VOLUME_USER
	if [ -d /tmp/.removable ]; then
		chmod 777 /tmp/.removable
	fi
	exit 0
fi

#if init then do once and exit

if [ "$FLAG" = "i" ] ; then
	a="`eject -f $DEVFILE 2>&1`"
	b="$?"

	case $b in
	0)				# Media has been ejected 
		con_msg
		exit 0;;
	1)				# Media not ejected
		exit 0;;
	3)			# Error 
		mk_error $DEVICE
		echo `basename $0` error: $a >/dev/tty
		e_con_msg
		exit 1;;
	esac
else
# interactive clean up

	if [ $VOLUME_MEDIATYPE = cdrom ]; then
		find_fs
	fi
	VOLUME_ACTION=eject
	a="`/usr/sbin/rmmount 2>&1`"
	b="$?"
	case $b in
	1)		# still mounted
         	mk_error $DEVICE
		echo `basename $0` error: $a >/dev/tty
         	e_user_msg
         	exit 1;;
	0)		# not mounted

		if [ "$FLAG" = "f" ] ; then
			a="`eject -f $DEVFILE 2>&1`"
		else
			a="`eject $DEVFILE 2>&1`"
		fi
			
		b="$?"
		case $b in
		0)				# Media has been ejected
			"$VOLUME_MEDIATYPE"_msg
			exit 0;;
		1)				# Media didn't eject
			mk_error $DEVICE
			echo `basename $0` error: $a >/dev/tty
			e_user_msg
			exit 1;;
		3)				# Other Error 
			mk_error $DEVICE
			echo `basename $0` error: $a > /dev/tty
			e_user_msg
			exit 1;;
		esac;;
	esac
fi
exit 0
