#
# Functions for hotplugable block device
#					Shuu Yamaguchi <shuu@dotAster.com>
# $Id: block_functions,v 1.2 2003/09/22 23:31:18 shuu Exp shuu $
#
. /etc/murasaki/bin/func_debug
. /etc/murasaki/bin/func_sysfs

# SIZE: size of device
# PARENT_DIR: if a block device does not mean a whole device buf 
#	      a partition, this is sysfs dir of a whole device.
# PARENT_SIZE: size of whole device if exists
get_size() {
	read SIZE < ${SYSFS_DIR}/${DEVPATH}/size
	LEN=`expr ${DEV} : '.*[1-9][0-9]*'`
	if [ $LEN -ne 0 ];then	# $DEV is child
		PARENT_DIR=`dirname ${SYSFS_DIR}${DEVPATH}`
		decho PARENT_DIR:$PARENT_DIR
		read PARENT_SIZE < ${PARENT_DIR}/size
		decho PARENT_SIZE:$PARENT_SIZE
		if [ $SIZE -gt $PARENT_SIZE ];then
			STATUS=1
		fi
	fi
}

# DEV: device name
# STATUS: 0: good !0: fail
initialize(){
	STATUS=0
	DEV=`basename $DEVPATH`
	decho DEV:$DEV
	if [ "$ACTION" != "add" ];then
		return
	fi
	get_sysfs
	decho SYSFS_DIR:$SYSFS_DIR
	SYSFS_DIR=/tmp/SYS
	decho SYSFS_DIR:$SYSFS_DIR

	get_size
	decho SIZE:$SIZE
}

# $DEV is mounted or not?
# return: $MOUNTED	0: not mounted	1: mounted
check_mount() {
	MOUNTED=0
	while read dev tmp
	do
		if [ "$dev" = "/dev/${DEV}" ];then
			MOUNTED=1;
			break;
		fi
	done < /etc/mtab
}

# mount onto apropriate mount point
bossy_mount() {
	TYPE=""
	while read dev mnt type tmp
	do
		if [ "$dev" = "/dev/${DEV}" ];then
			TYPE=$type
			MNT=$mnt
			break;
		fi
	done < /etc/fstab
	if [ "$TYPE" != "" ];then
		mount -t $TYPE /dev/${DEV} $MNT
	else
		mkdir -p /mnt/${DEV}
		mount /dev/${DEV} /mnt/${DEV}
	fi
}
