#!/bin/sh
# Copyright (c) 2016 xCloud

. /lib/functions.sh
. /lib/functions/service.sh

. /lib/xcloud/xc-common.sh
. /lib/xcloud/xc-notify.sh
. /lib/xcloud/xc-opt-utils.sh

# export VERBOSE=1

device=$1

usb_unmount() {
	umount -l /mnt/$1*
	rm -rf /dev/$1*
	rm -rf /mnt/$1*
	vs "umount usb mount-point /mnt/$1*"
}

if [ -n "$device" ];then
	device_has_opt=`awk -F "/" 'NR==1 {print $3}' /tmp/usbdir 2>/dev/null`
	if [ "${device_has_opt:0:3}" = "${device:0:3}" ];then
		
        #[ -x /etc/init.d/nxdownload ] && /etc/init.d/nxdownload restart
		
		#xcloud_service stop
		#[ "${device:0:2}" = "sd" ] && xcloud_notify_usb_cdn $device_has_opt umount

		sync
		[ -d /opt ] && opt_cleanup
		usb_unmount "$device"

		while read j; do
		mount_device=$(echo $j | awk -F" " '{print $1}')
		dev=`basename $mount_device`
			case "$dev" in
				sd* | md* | hd* | mmcblk*) [ -b /dev/$dev ] && opt_mount $dev;;
				*) continue;;
			esac
		done < /proc/mounts
	else
		usb_unmount "$device"
	fi
fi
