#!/bin/bash

# Mount/unmount the microdrive (CF card)
# Invoke as "cfmount -m", or "cfmount -u"
# It deals with creating symlink, and mounting.
# N.B. We must find out which /dev/sdX is the right one!
# This is very similar to the 1394mount script
# Written on 2003-06-26 by Richard Neill <cfmount AT richardneill.org> and released under the terms of the GNU GPL.
# 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/cf"					#This is the mountpoint (must be in /etc/fstab already, and must exist in /mnt, modes 777)
MODEL="SanDisk ImageMate 6 in 1"                        #This identifies the physical device - it's the output of scsi_info /dev/sdX
							#The symlink to create is defined in /etc/fstab: /dev/microda1  /mnt/cf  vfat  rw,user,noauto    0 0
DEVICES_TO_CHECK="/dev/sda /dev/sdb /dev/sdc /dev/sdd"  #These are the possibilities where it could be.

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 DEVICE in $DEVICES_TO_CHECK; do			#Now check the possible devices which could be our CF 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" ; 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

	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 'cfmount -m' (to mount) or 'cfmount -u' (to unmount)"
fi
exit 1


#----------------------------
#The original version of this script used disk geometry, i.e. this line:
#GEOMETRY="522/64/63"			#This identifies the physical device...
#instead of this:
#MODEL="SanDisk ImageMate 6 in 1"        #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.
