#!/bin/sh
# Copyright (C) 2006 OpenWrt.org

stop_process(){
              logger -s "stop_process!!!!!!!!!!!!!!!!!!!"
              check=$(ls -l /mnt | grep "/mnt/usb_${node}" | awk '{printf $9 }')
			  	[ "$check" = "usb_disk1" ] && {	
				  	rm /mnt/usb_disk1		
              		enable=$(uci -q get transmission.@transmission[0].enable)			    
              		if [ "$enable" = "1" ]; then
                    	killall -9 transmission-daemon
                    	sleep 5 
			    	fi  
                	enable=$(uci -q get itunes_server.@itunes_server[0].enabled)
					if [ "$enable" = "1" ]; then
                    	killall -9 mt-daapd
                    	killall -9 mDNSResponder
			    	fi
					enable=$(uci -q get ushare.@ushare[0].enabled)
					if [ "$enable" = "1" ]; then
                    	killall -9 ushare 
			    	fi
			    }
		      }

# skip Wimax dongle
# Samsung SWC U200 : 4e8/6761/1
if [ "$PRODUCT" = "4e8/6761/1" ]; then
	exit 1
fi

case "$ACTION" in
	add)
		[ -n "${INTERFACE}" ] &&
		[ "$(expr substr ${INTERFACE} 1 2)" == "8/" ]  && {
			/sbin/usb-storage &
		}
		;;
	remove)
		lock /var/lock/remove-${DEVICENAME}
		for node in `cat /var/run/usb/${DEVICENAME}.*`; do
		    if [ -f /var/run/usb/${DEVICENAME}.${node} ]; then  
                stop_process				
				logger "Unmounting ${node}"
                umount /dev/${node}
                rm -f /var/run/usb/*.${node}                             
                rmdir /mnt/usb_${node}
            fi
        done
		lock -u /var/lock/remove-${DEVICENAME}
	    ;;
esac