#!/bin/sh /etc/rc.common

START=99
USE_PROCD=1
LOGGER="logger -t [Webd]"

start_service() {
	local basic_list="enable webd_conf webd_bin webd_port webd_root enable_recyclebin enable_anonymous anonymous_perm enable_ipv6"
	local users_list="enable_read enable_read_list enable_upload enable_move enable_showhide"
	for i in $(echo $basic_list);do
		local eval $i="$(uci_get_by_type webd 0 $i)"
	done;unset i
	if [ "$enable" == 1 ]
	then
		[ ! -r "$webd_root" -o ! -d "$webd_root" ] && EXIT "Unable to access $webd_root,exit ..."
		[ ! -x "$webd_bin" ] && EXIT "Unable to access $webd_bin,exit ..."
		if [ "$enable_recyclebin" == 1 -a ! -d "$webd_root/.Trash" ]
		then
			${LOGGER} "Creating Recycle Bin directory ..."
			mkdir -p $webd_root/.Trash || EXIT "Failed to create Recycle Bin directory,exit ..."
		fi
		${LOGGER} "Removing old config file ..."
		rm -f $webd_conf
		touch -a $webd_conf || EXIT "Failed to create config,exit ..."
		[ "$enable_ipv6" == 1 ] && webd_port="[::]:${webd_port}"
		if [ "$enable_anonymous" != 0 ]
		then
			if [ -n "$anonymous_perm" ]
			then
				
				unset enable_anonymous
				for i in $(echo $anonymous_perm);do
					enable_anonymous="$enable_anonymous$i"
				done
				unset i
			else
				enable_anonymous=0
				uci set webd.@webd[0].enable_anonymous=0
				uci commit webd
			fi
		fi
		echo "Webd.Listen $webd_port" >> $webd_conf
		echo "Webd.Root $webd_root" >> $webd_conf
		echo "Webd.Guest $enable_anonymous" >> $webd_conf
		
		for u in 0 1;do
			for i in $(echo $users_list);do
				eval ${i}=$(uci_get_by_type users $u $i 0)
				echo "$users_list" | grep -q $i
				[ "$?" == 0 ] && eval perm_bin=$(eval echo '$'perm_bin)$(uci_get_by_type users $u $i)
			done
			unset i
			username=$(uci_get_by_type users $u username)
			password=$(uci_get_by_type users $u password)
			if [ -n "$username" ]
			then
				eval perm=$(perm_converter $(eval echo '$'perm_bin) | tail -n 1)
				if [ -n "$(eval echo '$'perm)" ]
				then
					${LOGGER} "Creating account for User $username ..."
					echo "Webd.User $(eval echo '$'perm) $username $password" >> $webd_conf
				else
					${LOGGER} "Removing excessive user config ..."
					uci delete webd.@users[$u]
					uci commit webd
				fi
				unset perm_bin
			fi
		done
		unset u
		ps -efww | grep "$webd_bin" | awk '{print $1}' | xargs kill -9 2> /dev/null
		${LOGGER} "Starting Webd Service ..."
		procd_open_instance
		procd_set_param command $webd_bin -c $webd_conf
		procd_set_param respawn
		procd_close_instance
	else
		stop_service
		${LOGGER} "Webd Service is now disabled ..."
	fi
}

stop_service() {
	${LOGGER} "Stopping Webd Service ..."
}

service_triggers() {
	procd_add_reload_trigger "webd"
}

uci_get_by_type() {
	local ret=$(uci get webd.@$1[$2].$3 2>/dev/null)
	echo ${ret:=$4}
}

EXIT() {
	${LOGGER} $*
	exit
}

perm_converter() {
	local u i=1
	echo $1 | egrep -o [0-1] | while read X
	do
		if [ "$X" == 1 ]
		then
			case $i in
			1)u=r;;
			2)u=l;;
			3)u=u;;
			4)u=m;;
			5)u=S;;
			esac
			[ -n "$u" ] && a="$a$u"
			echo "$a"
		fi
		i=$(($i + 1))	
	done
}

reload_service() {
	restart
}
