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

#MEMO:
#1.No Appended Generation: group name can not be existed, always create a new group
#2.validate period is only controlled by "group", no individual settings in guest

START=20

UCI_CONFIG="app_guest_setup"
GROUP_CONFIG="app_guest_group"
GUEST_CONFIG="app_guest"
CGI_ERROR_MSG="/tmp/cgi_error_msg"
TASK_FILE="/tmp/app_guest_gen_task"

MAX_GUEST_IN_GRP=255
MAX_GRP=30

boot() {
	mkdir -p /tmp/profile_backup/app_guest_setup
	return
}
start() {
	return
}
stop() {
	return
}
apply() {
	config_load $UCI_CONFIG
	config_get group_name			base group_name
	config_get name_prefix			base name_prefix
	config_get start_index			base start_index
	config_get generate_num			base generate_num
	config_get pwd_len				base pwd_len
	config_get usage_period			base usage_period
	config_get set_usage_time		base set_usage_time
	config_get valid_period			base valid_period
	config_get start_date			base start_date
	config_get end_date				base end_date
	
	######### Check arguments
	#1.mark if group is existed
	existed_group=0
	test_grp=`uci get $GROUP_CONFIG.$group_name`
	if [ -n "$test_grp" ] ;then
		existed_group=1
	else
		#check if add a new group will exceed maximum bumber of group
		group_number=`/sbin/uci_count_section "app_guest_group" "group_profile"`
		[ $group_number -ge $MAX_GRP ] && {
			echo -n "ERROR: the number of groups has reached its maximum" >$CGI_ERROR_MSG
			exit 101
		}
	fi
	
	#2.check remaining slots in group
		#If the group does not exist, return 0 to resident
	resident=`/sbin/uci_count_section "$GUEST_CONFIG" "userprofile" "profile" "$group_name"`
	remain_slot=$(($MAX_GUEST_IN_GRP - $resident))
	
	[ $remain_slot -le 0 ] && {
		echo -n "ERROR:the number of guests in group($group_name) has reached its maximum" >$CGI_ERROR_MSG
		exit 101
	}
	if [ $remain_slot -lt $generate_num ] ;then
		gen_size=$remain_slot
	else
		gen_size=$generate_num
	fi
	
	#3.check that guest should not be duplicated
	#old_nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
	dup_list=""
	dup_list=`/sbin/app_guest_dup_check "$name_prefix" "$start_index" "$gen_size"`
	#nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
	#echo "Check Guest DUP, EXEC TIME=$(($nowtime - $old_nowtime))" >/dev/console
	if [ -n "$dup_list" ] ;then
		duped_name=`echo -n "$dup_list" |cut -d ' ' -f 1`
		in_group=`echo -n "$dup_list" |cut -d ' ' -f 2`
		echo -n "ERROR: Guest Name:($duped_name) is already existed in group($in_group)" >$CGI_ERROR_MSG
		exit 101
	fi
	
	#4.check
	#	a.start_date or end_date can not be empty
	#	b.If start_date and end_date are not both in ignored type:"--", end_date must be larger than start_date
	#	c.If valid_period is disable, set startdate and enddate to "--"
	if [ "$valid_period" = "enable" ] ;then
		[ -z "$start_date" -o -z "$end_date" ] && {	#should not happened, this case is avoided by UI
			echo -n "ERROR: Start Date or End Date can not be empty" >$CGI_ERROR_MSG
			exit 101
		}
		[ "$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
				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
					exit 101
				fi
			fi
		fi
	else
		start_date="--"
		end_date="--"
		startdate="--"
		enddate="--"
	fi
	
	#Create job task file
	echo "existed_group $existed_group" >$TASK_FILE
	echo "group_config $GROUP_CONFIG" >>$TASK_FILE
	echo "group_sectype group_profile" >>$TASK_FILE
	echo "guest_config $GUEST_CONFIG" >>$TASK_FILE
	echo "guest_sectype userprofile" >>$TASK_FILE
	echo "group_name $group_name" >>$TASK_FILE
	echo "name_prefix $name_prefix" >>$TASK_FILE
	echo "start_index $start_index" >>$TASK_FILE
	echo "generate_num $gen_size" >>$TASK_FILE
	echo "pwd_len $pwd_len" >>$TASK_FILE
	echo "usage_period $usage_period" >>$TASK_FILE
	echo "set_usage_time $set_usage_time" >>$TASK_FILE
	echo "valid_period $valid_period" >>$TASK_FILE
	echo "start_date $start_date" >>$TASK_FILE
	echo "startdate $startdate" >>$TASK_FILE
	echo "end_date $end_date" >>$TASK_FILE
	echo "enddate $enddate" >>$TASK_FILE
	
	#old_nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
	/sbin/app_guest_generator "$TASK_FILE" 2>/dev/null
	#nowtime=$(cat /proc/uptime | awk 'FS="[.]+" {print $1}')
	#echo "FINAL Generate $gen_size GUESTs, EXEC TIME=$(($nowtime - $old_nowtime))" >/dev/console
	
	if [ $remain_slot -lt $generate_num ] ;then
		echo -n "Notice: $gen_size guests are generated" >$CGI_ERROR_MSG
		exit 101
	fi
	
	rm -f "$TASK_FILE"
	
	#UI friendly:Prevent (ptype=SECTION) show empty after apply
	uci set app_guest_setup.base.sec_naming="Name Settings"
	uci set app_guest_setup.base.sec_passwd="Random Password Settings"
	uci set app_guest_setup.base.sec_usage="Usage Settings"
}
