#!/bin/bash
#
# (c) Vector Linux, 2005
# Part of VL-Hot by Joe1962
# Licensed under the GPL v2
#
# vl-hot release 1.0.4
#


# Include configuration file:
. /etc/udev/vl-hot.conf

# Include support functions file:
. /lib/udev/vl-hot_functions


#**************************************************************************
# MAIN:
#**************************************************************************
feedback "Starting vl-hot_mount..."


if [ "$(id -un)" != "root" ]
then
	exec $SUDO $0 "$@"
	exit
fi

# Get device filename and volume number:
DEVNAME=$1
MOUNTBASE=${DEVNAME%%[0123456789]}
MOUNTDEV=$HOTLINKS_PATH/$DEVNAME
VOLNAME=vol${DEVNAME##[a-z]*[a-z]}
if [ $DIAG = "1" ]
then
	feedback "devname: $DEVNAME"
	feedback "volname: $VOLNAME"
fi

# do not mount if the device is excluded (Submitted by Kocil):
if is_excluded $DEVNAME ; then
    [ "$DIAG" = "1" ] && feedback "Device $DEVNAME is excluded, not mounted"
    exit 0
fi

# Go mount:
MOUNTPOINT=$MOUNT_PATH/$MOUNTBASE/$VOLNAME
if [ $DIAG = "1" ]
then
	feedback "Mounting $MOUNTDEV on $MOUNTPOINT"
fi

# Get filesystem type:
PARTMAPCHECK=`$DISKTYPE $MOUNTDEV | awk ' /partition/ { print $1 }'`
if [ -z $PARTMAPCHECK ]
then
	FSTYPE=`$DISKTYPE $MOUNTDEV | awk ' /file system/ { print $1 }'`
	if [ -z $FSTYPE ]
	then
		feedback "$MOUNTDEV not mountable..."
	elif [ $FSTYPE = "UNKNOWN" ]
	then
		feedback "$MOUNTDEV not mountable (UNKNOWN) ..."
	else
		if [ $DIAG = "1" ]
		then
			feedback "$MOUNTDEV Filesystem is $FSTYPE"
		fi
		chooseMountOptions
		mkdir -p $MOUNTPOINT
		if [ $MOUNT_OPTIONS = "UNKNOWN" ]
		then
			feedback "attempting to mount unplanned $FSTYPE filesystem: $MOUNTDEV on $MOUNTPOINT"
			mount "-t" $FSMOUNTTYPEARG -o defaults $MOUNTDEV $MOUNTPOINT
		else
			if [ $FSTYPE = "NTFS" ] ; then
				if [ $NTFS3G = "1" ] ; then
					feedback "mounting NTFS-3G filesystem: $MOUNTDEV on $MOUNTPOINT"
					ntfs-3g $MOUNTDEV $MOUNTPOINT $MOUNT_OPTIONS
				else
					feedback "mounting $FSTYPE filesystem: $MOUNTDEV on $MOUNTPOINT"
					mount "-t" $FSMOUNTTYPEARG $MOUNT_OPTIONS $MOUNTDEV $MOUNTPOINT
				fi
			else
				feedback "mounting $FSTYPE filesystem: $MOUNTDEV on $MOUNTPOINT"
				mount "-t" $FSMOUNTTYPEARG $MOUNT_OPTIONS $MOUNTDEV $MOUNTPOINT
			fi
		fi
		if [ $(mount | grep -c $MOUNTPOINT) = "0" ]
		then
			feedback "mounting $MOUNTDEV failed, not showing desktop icon for $MOUNTPOINT"
		else
			showDesktopIcon
		fi
	fi
else
	feedback "$MOUNTDEV not mountable (MBR) ..."
fi


feedback "Finishing vl-hot_mount..."
#**************************************************************************
