#!/bin/sh
#
# usb
#

# Date: 2010-04-14
# CR: crms00225116
# Author: Damien Couderc
# Label: access control of fab partition


# start the usb modules 
. /etc/init.d/rc.config
STATUS=/usr/sbin/upgd_status
mountLockFile="/tmp/.mountedFlash"
MOUNT_POINT="/mnt/removable/usb_disk"
launch="yes"

# crms00225116 BEGIN
USB_FLAG='/tmp/usb_started'
# crms00225116 END

# do the required action
case "$1" in
    start|"")
	if [ -x $STATUS ]; then
		$STATUS >/dev/null 2>&1
		if [ $? -eq 1 ]; then 
			launch="no"
			echo "upgrade in progress : will not load usb modules"
		fi	
	fi
	if [ "$launch" = "yes" ]; then
        	# Start the modules
        	if [ -x /etc/init.d/rc.modules.file ]; then 
        		if [  -f /etc/modules.usb ]; then 
               			/etc/init.d/rc.modules.file /etc/modules.usb
				# crms00225116 BEGIN
				# set the flag file
				touch "$USB_FLAG"
				# crms00225116 END
            		else 
                		echoerror \"Cannot find the file containing modules \"; 
			fi
            	else 
                	echoerror \"Cannot start modules.file script\"; 
        	fi
        fi
    ;; 
    stop)  
        	if [[ -f $mountLockFile ]] ; then 
		   umount $MOUNT_POINT
		fi
        	# Stop the modules
        	if [ -x /etc/init.d/rc.modules.file ]; then 
        		if [  -f /etc/modules.usb ]; then 
               			/etc/init.d/rc.modules.file /etc/modules.usb unload /etc/modules.no-unload
				# clean the flag file
				rm -f "$USB_FLAG"
            		else 
                		echoerror \"Cannot find the file containing modules \"; 
			fi
            	else 
                	echoerror \"Cannot start modules.file script\"; 
        	fi
    ;;

    # crms00225116 BEGIN
    status)
    	if [ -f "$USB_FLAG" ]; then
    		printf "started\n"
    	else
    		printf "stopped\n"
    	fi
    	;;
    # crms00225116 END

    *)
        echo "Usage $scriptname {start|stop}" >&2
        exit 1
    ;;
esac
