#!/bin/sh
# Modified By RA

. /lib/functions/block.sh

set_devices(){
	set_section=$(uci show fstab | grep "$get_uuid" | awk -F "." '{print $2}')
	old_device=$(uci get fstab.${set_section}.device)
	[ "$old_device" != "/dev/$device" ]&&{
		uci set fstab.${set_section}.device="/dev/$device"
		uci commit fstab
	}
}

set_fstab(){

	my_fstype="`blkid | grep "/dev/$device" | awk -F 'TYPE="' '{print $2}' | sed 's/\" //'`"
	
	[ ! -z "$my_fstype" ] && {
		
		n=$(uci show fstab | grep "fstab.@mount" | grep -c "=mount")
		m=$((n+1))

		uci add fstab mount
		uci set fstab.@mount[$n]="mount"
		uci set fstab.@mount[$n].enabled=1
		uci set fstab.@mount[$n].device="/dev/$device"
		uci set fstab.@mount[$n].uuid="${get_uuid}"
		uci set fstab.@mount[$n].target="/mnt/sda${m}"
		uci set fstab.@mount[$n].fstype="$my_fstype"

		case "$my_fstype" in
			'ext3')
				uci set fstab.@mount[$n].options="noatime,barrier=0,data=writeback";;
			'ext4')
				uci set fstab.@mount[$n].options="noatime,data=writeback,barrier=0";;
			'ntfs')
				uci set fstab.@mount[$n].options="noatime,nls=utf8";;
			'exfat')
				uci set fstab.@mount[$n].options="noatime";;
			'vfat')
				uci set fstab.@mount[$n].options="iocharset=utf8,utf8=1,umask=000,dmask=000,fmask=000";;
		esac

		uci commit fstab
	}
}

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

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

    case "$ACTION" in
	add)
		get_uuid=`blkid | grep "/dev/${device}" | awk -F "UUID=" '{print $2}'| awk -F "\"" '{print $2}'`
		[ -n "$get_uuid" ] && {
			have_uuid=$(uci show fstab | grep -c "$get_uuid")
			[ "$have_uuid" = "0" ] && set_fstab
			[ "$have_uuid" != "0" ] && set_devices
		}
		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_options="$5"
			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/mounts || {
						mkdir -p "$mds_mount_target"
						case "$mds_mount_fstype" in
							'ntfs')
								mount.ntfs-3g -o "$mds_mount_options" /dev/$device $mds_mount_target 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab';;
							'exfat')
								mount.exfat -o "$mds_mount_options" /dev/$device $mds_mount_target 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab';;	
							*)
								mount -t "$mds_mount_fstype" -o "$mds_mount_options" /dev/$device $mds_mount_target 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab';;
						esac										
					}
				else
					logger -t 'fstab' "Mount enabled for $mds_mount_device but it doesn't have a defined mountpoint (target)"
				fi
			}
			pgrep aria2c >/dev/null 2>&1 || /etc/init.d/aria2 start
		}

		[ -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/mounts || {
			[ "$anon_mount" -eq 1 -a -z "$use_device" ] && {
				case "$device" in
					mtdblock*) ;;
					*)
						( mkdir -p /mnt/$device && mount /dev/$device /mnt/$device ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab'
					;;
				esac
			}
		}
		reset_dev_section_cb
		;;
	remove)
		umount /dev/$device
		umount $mountpoint
		;;
    esac	

fi
