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

UCI_CONFIG="app_guest_group"
CGI_ERROR_MSG="/tmp/cgi_error_msg"

boot() {
	start
}

start() {
	return
}
stop() {
	return
}

del_guest_in_group()
{
	for guest in $1 ;do
		uci delete app_guest.$guest
	done
	uci commit app_guest
}

apply() {
	##### NEW GROUP
	new_grp=`uci fchanges new $UCI_CONFIG`
	[ -n "$new_grp" ] && {
		for elem in $new_grp; do
			valid_period=`uci get $UCI_CONFIG."$elem".valid_period`
			if [ "$valid_period" = "enable" ] ;then
				start_date=`uci get $UCI_CONFIG."$elem".start_date`
				end_date=`uci get $UCI_CONFIG."$elem".end_date`
				[ "$start_date" != "--" ] && startdate=`echo "$start_date" |sed 's/-//g'` || startdate="--"
				[ "$end_date" != "--" ] && enddate=`echo "$end_date" |sed 's/-//g'` || enddate="--"
				if [ "$start_date" != "--" -a "$end_date" != "--" ] ;then
					#shell can not compare out of range number
					start_pre_part=`echo $startdate |cut -c1-8`
					end_pre_part=`echo $enddate |cut -c1-8`
					start_aft_part=`echo $startdate |cut -c9-12`
					end_aft_part=`echo $enddate |cut -c9-12`
					if [ $end_pre_part -lt $start_pre_part ] ;then
						echo -n "ERROR: End Time must be larger than Start Time" >$CGI_ERROR_MSG
						uci revert $UCI_CONFIG.$elem
						exit 101
					elif [ $end_pre_part -eq $start_pre_part ] ;then
						if [ $end_aft_part -lt $start_aft_part ] ;then
							echo -n "ERROR: End Time must be larger than Start Time" >$CGI_ERROR_MSG
							uci revert $UCI_CONFIG.$elem
							exit 101
						fi
					fi
				fi
				uci set $UCI_CONFIG."$elem".startdate="$startdate"
				uci set $UCI_CONFIG."$elem".enddate="$enddate"
			else
				uci set $UCI_CONFIG."$elem".start_date="--"
				uci set $UCI_CONFIG."$elem".end_date="--"
				uci set $UCI_CONFIG."$elem".startdate="--"
				uci set $UCI_CONFIG."$elem".enddate="--"
			fi
		done
	}
	
	##### MODIFY GROUP
	mod_grp=`uci fchanges modify $UCI_CONFIG`
	[ -n "$mod_grp" ] && {
		for elem in $mod_grp; do
			valid_period=`uci get $UCI_CONFIG."$elem".valid_period`
			if [ "$valid_period" = "enable" ] ;then
				start_date=`uci get $UCI_CONFIG."$elem".start_date`
				end_date=`uci get $UCI_CONFIG."$elem".end_date`
				[ "$start_date" != "--" ] && startdate=`echo "$start_date" |sed 's/-//g'` || startdate="--"
				[ "$end_date" != "--" ] && enddate=`echo "$end_date" |sed 's/-//g'` || enddate="--"
				if [ "$start_date" != "--" -a "$end_date" != "--" ] ;then
					#shell can not compare out of range number
					start_pre_part=`echo $startdate |cut -c1-8`
					end_pre_part=`echo $enddate |cut -c1-8`
					start_aft_part=`echo $startdate |cut -c9-12`
					end_aft_part=`echo $enddate |cut -c9-12`
					if [ $end_pre_part -lt $start_pre_part ] ;then
						echo -n "ERROR: End Time must be larger than Start Time" >$CGI_ERROR_MSG
						uci revert $UCI_CONFIG.$elem
						exit 101
					elif [ $end_pre_part -eq $start_pre_part ] ;then
						if [ $end_aft_part -lt $start_aft_part ] ;then
							echo -n "ERROR: End Time must be larger than Start Time" >$CGI_ERROR_MSG
							uci revert $UCI_CONFIG.$elem
							exit 101
						fi
					fi
				fi
				uci set $UCI_CONFIG."$elem".startdate="$startdate"
				uci set $UCI_CONFIG."$elem".enddate="$enddate"
			else
				uci set $UCI_CONFIG."$elem".start_date="--"
				uci set $UCI_CONFIG."$elem".end_date="--"
				uci set $UCI_CONFIG."$elem".startdate="--"
				uci set $UCI_CONFIG."$elem".enddate="--"
			fi
		done
	}
	
	##### DELETE GROUP and its Members
	del_grp=`uci fchanges delete $UCI_CONFIG`
	[ -n "$del_grp" ] && {
		#old_nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
		for elem in $del_grp; do
			/sbin/uci_fast_filter_delete "app_guest" "userprofile" "profile" "$elem"
		done
		#nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
		#echo "DEL WHOLE GROUP($del_grp), EXEC TIME=$(($nowtime - $old_nowtime))" >/dev/console
	}
	uci commit $UCI_CONFIG
}
