#!/bin/bash
# This script mounts the sdcard on startup, if available 

me="[sdcard]"
sddev="/dev/mmcblk1p1"
sdmount="/opt/card"
blacklistfirst="/dev/mmcblk1p1"
blacklistany="/dev/mmcblk1p\*"

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars

function mount_scard
{
	echo -n "${me} Check for available sdcard ... "
	if [ -b ${sddev} ]
	then
		if [ "$(cat /proc/mounts | grep ${sdmount})" != "" ]
		then
			echo "already mounted."
		else
			if grep -xq "$blacklistany" /etc/udev/mount.blacklist || grep -xq "$blacklistfirst" /etc/udev/mount.blacklist
			then
				echo "sdcard is blacklisted, abort."
			else
				echo -n "mounting ... "
				${MOUNT} -t vfat -o rw,suid,sync,noatime ${sddev} ${sdmount}
				if [ $? -ne 0 ]
				then
					echo "failed! Maybe not FAT32?"
				else
					echo "done."
				fi
			fi
		fi
	else
		echo "none found!"
	fi
}

function unmount_sdcard
{
	if [ "$(cat /proc/mounts | grep ${sdmount})" != "" ]
	then
		echo -n "${me} Unmounting sdcard ... "
		sync
		$UMOUNT ${sdmount}
		if [ $? -ne 0 ]
		then
			echo "failed!"
		else
			echo "done."		
		fi
	else
		echo "${me} No sdcard mounted!"
	fi
}

command=$1
case ${command} in
start)
 mount_scard
 ;;
stop)
 unmount_sdcard
 ;;
restart)
  unmount_sdcard
  mount_scard
 ;;
*)
 echo "usage: $(basename $0) start|stop|restart"
 exit 1
 ;;
esac
