#!/bin/sh
#

#PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin

. /etc/default/rcS

CheckMediaSda()
{
# if the USB memory stick is left in the system & it's powered down and then
# the USB stick is removed and the system powered on again, /media/sda1 exists
# and the player menus will attempt to export config/logs to that directory 
# Attempt to detect the condition if /media/sda1 exist and remove it
# removed attempts logged to the file /var/log/media_sda , because it puts the file at /
#date >> /var/log/media_sda

# check is the /dev/sda1 and /media/sda1 exist 
if test ! -p /dev/sda1
then
	echo /dev/sda1 exists
else
	echo /dev/sda1 does not exist
fi
if test ! -p /media/sda1
then
	echo /media/sda1 exists
else
	echo /media/sda1 does not exist
fi

if ! mount /dev/sda1 /media/sda1
then
  	echo "mount to /media/sda1 failed" 
	if ! ls -la /media/sda* > /dev/null 
	then 
		echo "ls -la /media/sda* failed" 
		if ! rmdir /media/sda1
		then
			echo "rmdir /media/sda1 failed" 
		else
			echo "rmdir /media/sda1 passed" 
		fi
	else
		echo "ls -la /media/sda* passed" 
		if ! rmdir /media/sda1 
		then
			echo "rmdir /media/sda1 failed" 
		else
			echo "rmdir /media/sda1 passed" 
		fi
	fi
else
  	echo "mount to /media/sda1 passed" 
fi
}

case "$1" in
  start)
	CheckMediaSda
    ;;
  stop)
    ;;
  restart)
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start}"
    exit 1
    ;;
esac

exit 0