#!/bin/sh

# based on script from http://gitorious.org/neutrino-hd/buildsystem-cs

. /etc/init.d/globals

MOUNTBASE=/media
MOUNTPOINT="$MOUNTBASE/$MDEV"
ROOTDEV=$(readlink /dev/root)

# do not add or remove root device again...
[ "$ROOTDEV" = "$MDEV" ] && exit 0

if [ -e /tmp/.nomdevmount ]; then
	LOGINFO "no action on $MDEV -- /tmp/.nomdevmount exists"
	exit 0
fi

create_symlinks() {
	LOGINFO "try to create symlinks for /dev/$MDEV in $MOUNTBASE"
	DEVBASE=${MDEV:0:3} # first 3 characters
	#LOGINFO "create symlinks: DEVBASE=$DEVBASE"
	PARTNUM=${MDEV:3}   # characters 4-
	#LOGINFO "create symlinks: PARTNUM=$PARTNUM"

	if which wakeup.sh > /dev/null; then
		wakeup.sh $MOUNTPOINT
	fi

	if which blkid > /dev/null; then
		# blkid delivers LABEL and UUID
		BLKID=$(blkid -c /dev/null -w /dev/null /dev/$MDEV)
		eval ${BLKID#*:}
	fi
	if [ -e /sys/block/$DEVBASE/device/model ]; then # don't read if blockdevice not present
		read MODEL < /sys/block/$DEVBASE/device/model
	fi
	MODEL=${MODEL// /_} # replace ' ' with '_'

	#LOGINFO "create symlinks: LABEL=$LABEL"
	#LOGINFO "create symlinks: UUID=$UUID"
	#LOGINFO "create symlinks: MODEL=$MODEL"

	OLDPWD=$PWD
	cd $MOUNTBASE
	if [ -n "$LABEL" ]; then
		LINK=${LABEL// /_} # replace ' ' with '_'
		rm -f "${LINK}"
		ln -s $MDEV "${LINK}"
	fi
	if [ -n "$UUID" ]; then
		LINK="${TYPE}${TYPE:+-}${UUID}"
		rm -f "${LINK}"
		ln -s $MDEV "${LINK}"
	fi
	if [ -n "$MODEL" ]; then
		LINK="${MODEL}${PARTNUM:+-}${PARTNUM}"
		rm -f "${LINK}"
		ln -s $MDEV "${LINK}"
	fi
	cd $OLDPWD
}

remove_symlinks() {
	OLDPWD=$PWD
	cd $MOUNTBASE
	for i in *; do
		[ -L "$i" ] || continue
		TARGET=$(readlink "$i")
		if [ "$TARGET" = "$MDEV" ]; then
			rm "$i"
		fi
	done
	cd $OLDPWD
}

case "$ACTION" in
	add)
		if [ ${#MDEV} = 3 ]; then # sda, sdb, sdc => whole drive
			PARTS=$(sed -n "/ ${MDEV}[0-9]$/{s/ *[0-9]* *[0-9]* * [0-9]* //;p}" /proc/partitions)
			if [ -n "$PARTS" ]; then
				LOGINFO "drive $MDEV has partitions ($PARTS); not trying to mount $MDEV"
				exit 0
			fi
		fi
		if grep -q "/dev/$MDEV" /proc/mounts; then
			LOGINFO "/dev/$MDEV already mounted - not mounting again"
			exit 0
		fi
		LOGINFO "[$ACTION] mounting /dev/$MDEV to $MOUNTPOINT"
		# remove old mountpoint symlinks we might have for this device
		rm -f $MOUNTPOINT
		mkdir -p $MOUNTPOINT
		for i in 1 2 3 4 5 6 7 8 9 10; do # retry 10 times for ssslllooowww devices
			#LOGINFO "[$ACTION] mounting /dev/$MDEV to $MOUNTPOINT; try $i/10"
			OUT1=$(mount -t auto /dev/$MDEV $MOUNTPOINT 2>&1 >/dev/null)
			RET1=$?
			[ $RET1 = 0 ] && break
			sleep 1
		done
		if [ $RET1 = 0 ]; then
			create_symlinks
		else
			LOGWARN "mount   /dev/$MDEV $MOUNTPOINT failed with $RET1"
			LOGWARN "        $OUT1"
			rmdir $MOUNTPOINT
		fi
		if which mdev_helper > /dev/null; then
			mdev_helper
		fi
		;;
	remove)
		LOGINFO "[$ACTION] unmounting $MOUNTBASE/$MDEV"
		grep -q "^/dev/$MDEV " /proc/mounts || exit 0 # not mounted...
		umount -lf $MOUNTBASE/$MDEV
		RET=$?
		if [ $RET = 0 ]; then
			rmdir $MOUNTPOINT
			remove_symlinks
		else
			LOGWARN "umount $MOUNTBASE/$MDEV failed with $RET"
		fi
		if which mdev_helper > /dev/null; then
			mdev_helper
		fi
		;;
esac
