#!/bin/sh
# Copyright (C) 2009-2012 OpenWrt.org
# Copyright (C) 2010 Vertical Communications
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

. /lib/functions/block.sh

usbshare_svr_work() {
	local act_opt=$1
	local smb_worker=`uci -c /etc/profile.d get profile.usbshare.smb_worker`
	local ftp_worker=`uci -c /etc/profile.d/get profile.usbshare.ftp_worker`
	local dlna_worker=`uci -c /etc/profile.d/get profile.usbshare.dlna_worker`

	[ -z "$smb_worker" ] && smb_worker="samba"
	[ -z "$ftp_worker" ] && ftp_worker="proftpd"
	[ -z "$dlna_worker" ] && dlna_worker="minidlna"

	/etc/init.d/"$smb_worker" $act_opt
	/etc/init.d/"$ftp_worker" $act_opt
	/etc/init.d/"$dlna_worker" $act_opt
}

usbshare_svr_restart() {
	usbshare_svr_work restart
}

usbshare_svr_stop() {
	usbshare_svr_work stop
}

time_machine_stop() {
	/etc/init.d/afpd stop
	/etc/init.d/avahi-daemon stop
}

blkdev=`dirname $DEVPATH`

if [ `basename ${blkdev%/*}` != "block" ]; then
	device=`basename $DEVPATH`
	case "$ACTION" in
	add)
		blkid -s UUID -o value /dev/$device 1>/dev/null 2>&1
		if [ $? -eq 0 ]; then
		    blkdev=$DEVPATH
		fi
		;;
	remove)
		mountpoint=`sed -ne "s|^[^ ]*/$device ||; T; s/ .*//p" /proc/self/mounts`
		if [ -n "$mountpoint" ]; then
			blkdev=$DEVPATH
		fi
		;;
	esac
fi

if [ `basename $blkdev` != "block" ]; then

    # for led control
    local portnum=`echo $DEVPATH | cut -d "-" -f 2 | cut -d ":" -f 1 | cut -d "/" -f 1`
    local usb
    case "$portnum" in
        1)
            usb="USB3"
            ;;
        2)
            usb="USB1"
            ;;
    esac

    device=`basename $DEVPATH`

    if [ ${device:0:2} == "sd" ]; then
        local devnum=${device:3}
        if [ $devnum -gt 15 ];then
            return
        fi
    fi

    mountpoint=`sed -ne "s|^[^ ]*/$device ||; T; s/ .*//p" /proc/self/mounts`

    case "$ACTION" in
	add)

		local from_fstab
		local anon_mount
		local anon_swap
		local anon_fsck
		local mds_mount_target
		local mds_mount_device
		local mds_mount_fstype
		local sds_swap_device
		local use_device
		local do_fsck=0
		local fsck_type

		local autoswap_from_fstab
		local automount_from_fstab

	    mount_dev_section_cb() {
    		mds_mount_target="$2"
			mds_mount_device="$3"
			mds_mount_fstype="$4"
			mds_mount_enabled="$6"
	    }

	    swap_dev_section_cb() {
			sds_swap_device="$2"
			return 0
	    }

		config_get_automount
		automount_from_fstab="$from_fstab"
		[ "$automount_from_fstab" -eq 1 ] && {
			config_get_mount_section_by_device "/dev/$device"
			use_device="$mds_mount_device"
			[ "$mds_mount_enabled" -eq 1 ] && {
				if [ -n "$mds_mount_target" ]; then
					grep -q "/dev/$device" /proc/swaps || grep -q "/dev/$device" /proc/mounts || {
						( mkdir -p "$mds_mount_target" && mount "$mds_mount_target" ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab'
					}
				else
					logger -t 'fstab' "Mount enabled for $mds_mount_device but it doesn't have a defined mountpoint (target)"
				fi
			}
		}

		[ -z "$use_device" ] && {
			config_get_autoswap
			autoswap_from_fstab="$from_fstab"

			[ "$autoswap_from_fstab" -eq 1 ] && {
				config_get_swap_section_by_device "/dev/$device"
				use_device="$sds_swap_device"
			}
		}

		grep -q "/dev/$device" /proc/swaps || grep -q "/dev/$device" /proc/mounts || {
			# [ "$anon_mount" -eq 1 -a -z "$use_device" ] && {
			[ "$anon_mount" -eq 1 ] && {
				case "$device" in
					mtdblock*) ;;
					*)
						# check /dev/$device whether a valid mountable partition
						blkid -s UUID -o value /dev/$device 1>/dev/null 2>&1
						if [ $? -eq 0 ]; then
							# ( mkdir -p /mnt/$device && mount -o umask=0,force,rw /dev/$device /mnt/$device ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab'
							mkdir -p -m 777 /mnt/$device
							mount -t tntfs -o umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							if [ $? -ne 0 ]; then
								mount -t tfat -o umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								mount -t texfat -o umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								mount -t thfsplus -o nomode,umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1 && chmod 777 /mnt/$device
							fi
							if [ $? -ne 0 ]; then
								mount -t vfat -o umask=0000,utf8,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								mount -t exfat -o umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								mount -t ntfs-3g -o umask=0000,utf8,force,rw,big_writes /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								fsck.hfsplus /dev/$device 1>/dev/null 2>&1
								mount -t hfsplus -o umask=0000,force,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
							if [ $? -ne 0 ]; then
								mount -t ntfs -o umask=0000,rw /dev/$device /mnt/$device 1>/dev/null 2>&1
							fi
						fi
					;;
				esac
			}
		}
		reset_dev_section_cb
		rm -f /etc/config/sharecfg

		local dev_root=${device:0:3}
		local rotational=`cat /sys/block/$dev_root/queue/rotational`
		local queue_depth=`cat /sys/block/$dev_root/device/queue_depth`
		if [ ${dev_root:0:2} = "sd" ]; then
			if [ "$rotational" = "0" ]; then
				if [ "$queue_depth" = "1" ]; then
					# An SSD with queue_depth 1, increase the nr_requests for high performance.
					echo "30" > /sys/block/$dev_root/queue/nr_requests
				else
					# For normal SSD, "none" will lead to better performance.
					echo "none" > /sys/block/$dev_root/queue/scheduler
				fi
				echo 2048 > /sys/block/$dev_root/queue/read_ahead_kb
			fi
		fi

		usbshare_svr_restart

		#add by ccx, sync periodically
		ps w | grep -v grep | grep -q usb_sync || /usr/sbin/usb_sync &

		uci set ledctrl.$usb.ledon='1'
		ledcli $usb
		;;
	remove)
		usbshare_svr_stop
		local enable=`uci get time_machine.settings.enable`
		if [ x"$enable" == x"on" ]; then
			local time_machine=`uci get time_machine.settings.mntdir`
			for i in $mountpoint
			do
				if [ x"$time_machine" == x"$i" ]; then
					time_machine_stop
				fi
			done
		fi
		umount $mountpoint
		umount /dev/$device
		#add by fjs, remove mountpoint folders when umount disks.
		if [ ! -e "/dev/$device" ]; then
			rmdir $mountpoint
		fi
		rm -f /etc/config/sharecfg
		usbshare_svr_restart

		uci set ledctrl.$usb.ledon='0'
		[ x"$(uci get profile.@usbshare[0].single_usb_port -c "/etc/profile.d")" != x"1" ] && ledcli $usb || {
			[ x"$(uci get ledctrl.USB1.ledon)" == x"0" ] && [ x"$(uci get ledctrl.USB3.ledon)" == x"0" ] && {
				ledcli USB1
				ledcli USB3
			}
		}
		;;
	esac
else
    case "$ACTION" in
	add)
		[ -f /lib/usb/usb.sh ] && {
			. /lib/usb/usb.sh
			usb_storsize $DEVPATH
		}
		;;
    esac
fi

