#!/bin/sh
# ============================================================================
#
# This file is part of the 'RandomTools for zypkgs' package
# 
# This program is free software; you can redistribute it and/or modify it 
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Author: Mijzelf <Mijzelf@live.com>
#
# ============================================================================
#
PKGNAME="exFAT"
PKG_ROOT="/usr/local/zy-pkgs"
CONFIG_DIR="${PKG_ROOT}/config/${PKGNAME}"
CONFIG_FILE="${CONFIG_DIR}/config"
exFATTOOLS="/sbin/exfatfsck /sbin/exfatlabel /sbin/mkexfatfs /sbin/mount.exfat-fuse"

IsUpgrading()
{
	if cat ${PKG_ROOT}/tmp/zypkg.log | grep "\[exFAT\]" | tail | grep "Upgrade Mode" >/dev/null
	then
		return 0
	fi
	return 1
}

Log()
{
	[ -f /tmp/${PKGNAME}.log ] && echo "` date `: $@" >>/tmp/${PKGNAME}.log
}

InsertLine()
{
	local filename=$1
	local action=$2
	local tagline=$3
	local insert=$4
	local all=$5

	if [ "$action" = "before" ] ; then
		action=i
	else
		action=a
	fi

	local linenumbers=` grep -n "$tagline" $filename | sort -r | cut -d : -f 1 `

	[ "" = "$linenumbers" ] && return 1

	if [ "" = "$all" ] ; then
		linenumbers=` echo $linenumbers | tail -n 1 `
	fi

	local ret=0
	for l in $linenumbers
	do
		sed -i "${l}${action} $insert" $filename
		[ $? -ne 0 ] && ret=2
	done

	return $ret
}


Status()
{
	if [ ! -f ${CONFIG_FILE} ] ; then
		mkdir -p ${CONFIG_DIR}
		echo "Disabled" >${CONFIG_FILE}
	fi
	cat ${CONFIG_FILE}
}

Startup()
{
	local STATUS=` Status `
	if [ "$STATUS" != "Enabled" ] ; then 
		return 0
	fi

	local exfat
	for tool in $exFATTOOLS
	do
		ln -s ${PKG_ROOT}${tool} ${tool}
		exfat=${tool}
	done
	
	# Enabling automounting
	local tmpmount=/tmp/.${PKGNAME}
	local hotplug=hotplug_add_storage.sh
	mkdir -p ${tmpmount}
        mount -t tmpfs tmpfs ${tmpmount}
	cp /usr/sbin/${hotplug} ${tmpmount}/
	cp /usr/sbin/mount.tntfs ${tmpmount}/
	InsertLine ${tmpmount}/${hotplug} after "[[:space:]]mount \${devsdxy} /mnt/\${onlysdxy}" "[ \$? -ne 0 ] && ${exfat} \${devsdxy} /mnt/\${onlysdxy}" all
	InsertLine ${tmpmount}/${hotplug} after "[[:space:]]mount \${devsdx} /mnt/\${onlysdx}" "[ \$? -ne 0 ] && ${exfat} \${devsdx} /mnt/\${onlysdx}" all
	mount --bind ${tmpmount}/${hotplug} /usr/sbin/${hotplug}
	echo "[ \$? -ne 0 ] && ${exfat} \$@" >>${tmpmount}/mount.tntfs
	mount --bind ${tmpmount}/mount.tntfs /usr/sbin/mount.tntfs
	
	# check if a exfat device is already inserted:
	[ ! -f /tmp/not_mountable.txt ] && return 0

	for devsdx in ` cat /tmp/not_mountable.txt | cut -d ' ' -f 2 `
	do
		if ! mount | grep $devsdx 
		then
			local onlysdx=` echo ${devsdx} | cut -d '/' -f 3 `
			mkdir -p /mnt/${onlysdx}
			/sbin/mount.exfat-fuse $devsdx /mnt/${onlysdx}
			if [ $? -eq 0 ] ; then
				zyshclient -p 150 -e "storage hotplugadd ${devsdx}"
				zyshclient -p 150 -e "media addextshare ${devsdx}"
			else
				rmdir /mnt/${onlysdx}
			fi
		fi
	done
}

Shutdown()
{
	# Won't umount exFAT devices, as it will also umount ntfs devices. Don't know how to see the difference

	# Disable 'automount'
	local tmpmount=/tmp/.${PKGNAME}
	local hotplug=hotplug_add_storage.sh

	if grep -qw ${tmpmount} /proc/mounts
	then 
		umount /usr/sbin/${hotplug}
		umount /usr/sbin/mount.tntfs
		umount ${tmpmount}
		rm -fr ${tmpmount}
	fi

	for tool in $exFATTOOLS
	do
		[ -h ${tool} ] && [ "` readlink $tool `"  = "${PKG_ROOT}${tool}" ] && rm ${tool}
	done
}

Disable()
{
	Shutdown
	mkdir -p ${CONFIG_DIR}
	echo "Disabled" >${CONFIG_FILE}
}

Enable()
{
	mkdir -p ${CONFIG_DIR}
	echo "Enabled" >${CONFIG_FILE}
	Startup 
}

Log $0 $@

case $1 in
	status)
		Status
		;;
	startup)
		Startup
		;;
	shutdown)
		Shutdown
		;;
	enable)
		Enable
		;;
	disable)
		Disable
		;;
esac

exit 0
