#!/bin/bash

# Modprobe and mount the 1394 disk
# Invoke as "1394mount -m", or "1394mount -u"
# It deals with modules, creating symlink, and mounting.
# N.B. We must find out which /dev/sdX is the right one!
# Written on 2003-06-26 by Richard Neill <1394mount AT richardneill.org> and released under the terms of the GNU GPL.
# Update: 2003-07-01  - perhaps see also: http://www.lerhaupt.com/linux.html#devlabel
# Update: 2006-02-08  - If using kernel 2.6, writing a udev rule is a much better way to solve this problem.

#The Disk may be identified by using ide_info => SERIAL, scsi_info => MODEL or the geometry output from hdparm.
#N.B. the device in /etc/fstab must be the symlink created here: see below.

MOUNTPOINT="/mnt/1394"					#This is the mountpoint (must be in /etc/fstab already, and must exist in /mnt, modes 777)
MODEL="LSILogic SYM13FW500-Disk"			#This identifies the physical device - it's the output of scsi_info /dev/sdX
							#The symlink to create is defined in /etc/fstab:  /dev/1394da1 /mnt/1394 reiserfs user,noauto,notail,noatime 1 2
DEVICES_TO_CHECK="/dev/sda /dev/sdb /dev/sdc /dev/sdd"  #These are the possibilities where it could be.
MODULES_TO_REMOVE="raw1394 sbp2 ohci1394 ieee1394"	#These are the modules to remove in REMOVAL order
MODULES_TO_PROBE="ieee1394 ohci1394 sbp2"		#These are the modules to probe in INSERT order.
MODULES_TO_UNPLUG="sbp2"				#This needs removed before the drive can be unplugged/switched off.



if [ `whoami` != "root" ] ;then
	echo "You need to be root to do this." ;  exit 1
fi

if [ ! -w $MOUNTPOINT ]; then
	echo "Mountpoint $MOUNTPOINT does not exist, or is not writeable" ; exit 1
fi

SYMLINK=`grep $MOUNTPOINT /etc/fstab | awk '{print $1}'` 	#This is the symlink to create in /dev to guarantee correspondance with /etc/fstab
if [ -z "$SYMLINK" ];then
	echo "Cannot find device in /etc/fstab to associate with $MOUNTPOINT " ; exit 1
fi

#----------------------------

if [ "$1" == "-m" ]; then

	echo "Attempting to mount $MOUNTPOINT..."		#Mount

	if mount | grep  $MOUNTPOINT > /dev/null ;then		#Are we already mounted?
		echo "$MOUNTPOINT is already mounted"; exit 0
	fi

	for MODULE in $MODULES_TO_REMOVE; do			#Remove modules so they can be reinserted...
		if /sbin/lsmod | grep -e ^$MODULE > /dev/null; then
			echo -ne "Removing module $MODULE..."
			if /sbin/modprobe -r $MODULE ;then
				echo "[OK]"
			else
				echo "FAILED to remove module $MODULE"; exit 1
			fi
		fi
	done

	for MODULE in $MODULES_TO_PROBE; do			#Re-insert modules in order...this ensures that the entry is created in /dev
		echo -ne "Modprobing module $MODULE..."
		if /sbin/modprobe $MODULE ;then
			echo "[OK]"
		else
			echo "FAILED to insert module $MODULE"; exit 1
		fi
	done


	for DEVICE in $DEVICES_TO_CHECK; do			#Now check the possible devices which could be our 1394 disk.
		echo -ne "Checking device $DEVICE..."
		if [ -e $DEVICE ]; then
			if [ "`/sbin/scsi_info $DEVICE | grep MODEL | cut -c 7- | sed 's/\"//g'`" == "$MODEL" ];then	#Check Disk Model.
				echo "Found it"
				THE_DEVICE=$DEVICE;
				break;
			else
				echo "[Not this one]"
			fi
		else
			echo "[Non-existent]"
		fi
	done

	if [ -z "$THE_DEVICE" ];then				#Exit if we haven't found it.
		echo "Disk not found among devices: $DEVICES_TO_CHECK" ; exit 1
	fi

	THE_DEVICE_PARTITION_1=${THE_DEVICE}1			#/dev/sda1 not /dev/sda

	ln -s $THE_DEVICE_PARTITION_1 $SYMLINK			#Create the relevant symlink.
	
	echo -ne "Mounting $MOUNTPOINT..."

	if mount $MOUNTPOINT ;then				#Mount it.
		echo "[OK]" ; exit 0
	else
		echo "[Failed]" ; exit 1
	fi

#----------------------------

elif [ "$1" == "-u" ]; then

	echo  "Attempting to unmount $MOUNTPOINT..."		#Unmount

	if ! mount | grep $MOUNTPOINT > /dev/null ;then		#Are we already mounted?
		echo "$MOUNTPOINT was not mounted"; exit 0
	fi

	if ! umount $MOUNTPOINT ;then				#Unmount
		echo "Failed to unmount $MOUNTPOINT." ;	exit 1
	fi

	for MODULE in $MODULES_TO_UNPLUG; do			#Remove module so the drive may be unplugged and powered off.
		if /sbin/lsmod | grep -e ^$MODULE > /dev/null; then
			echo -ne "Removing module $MODULE..."
			if /sbin/modprobe -r $MODULE ;then
				echo "[OK]"
			else
				echo "FAILED to remove module $MODULE"; exit 1
			fi
		fi
	done

	rm $SYMLINK						#remove the relevant symlink from /dev

	echo "Unmounted $MOUNTPOINT successfully."
	echo "It's NOW OK to switch off and unplug the drive."
	exit 0

#----------------------------

else
	echo "Please invoke as '1394mount -m' (to mount) or '1394mount -u' (to unmount)"
fi
exit 1


#----------------------------
#The original version of this script used disk geometry, i.e. this line:
#GEOMETRY="15017/255/63"		#This identifies the physical device (it's the output of hdparm).
#instead of this:
#MODEL="LSILogic SYM13FW500-Disk"	#This identifies the physical device - it's the output of scsi_info /dev/sdX
#and this line:
#if [ "`/sbin/hdparm $DEVICE | grep geometry | awk  '{print $3}'`" == "$GEOMETRY," ]; then	#Check geometry
#instead of this:
#if [ "`/sbin/scsi_info $DEVICE | grep MODEL | cut -c 7- | sed 's/\"//g'`" == "$MODEL" ];then	#Check Disk Model.

