#!/bin/sh

. /etc/PG.conf

CRONTAB_CONF="${PGETC}/App/sac/crontab.conf"
LOGDIR="/usr/ramdisk/tmp/sac/crontab"
CRONTAB_STAT="${LOGDIR}/sac_crontab.stat"

FLOWEYE="/usr/ramdisk/bin/floweye"

QUERY="/usr/ramdisk/app/webui/bin/sac_query_stat"


save_log()
{
	row="id=${sch_id}"
	content="id=${sch_id} exec_time=`date +%s` task_stat=${1} task_cnt="
	
	if [ ! -f ${CRONTAB_STAT} ]; then
		echo "${content}" > ${CRONTAB_STAT}
		return
	fi

	exist=`cat ${CRONTAB_STAT} | grep "${row} "`
	if [ "${exist}" = "" ]; then
		echo "${content}" >> ${CRONTAB_STAT}
		return
	fi

	awk -v bak="${CRONTAB_STAT}.bak" -v row="${row}" -v content="${content}" \
	'{
		if($1 == row) {
			print content >> bak;
			next;
		} else
			print $0 >> bak;
	}' ${CRONTAB_STAT}

	mv ${CRONTAB_STAT}.bak ${CRONTAB_STAT}
}


handle_ssid()
{
	for val in `echo "${sch_cmdstr}" | tr "&" " "`
	do
		eval "s_${val}"
	done
	
	if [ "${sch_cmd}" = "ssid_add" ]; then
		# "op=addwlan&ridx=0&ssid=ABCD&amode=0&vlan=0&hide=0
		${FLOWEYE} sacssid add name=${s_ssid} vlan=${s_vlan} mode=${s_amode} pwd=${s_key} hide=${s_hide} maxsta=${s_maxstat}
	else
		# op=delwlan&ssidstr=,ABCD_0
		for ssid in `echo "${s_ssidstr} | tr '," " "`
		do
			${FLOWEYE} sacssid remove name=${ssid}
		done
	fi
}


handle_apid()
{
	for mac in `echo ${sch_apmac} | tr "," " "`
	do
		${FLOWEYE} sac list json=0 | awk -v mac=${mac} \
		'{
			if($18 == mac)
				printf ",%s", $3;
		}'
	done
}


task_commit()
{
	if [ "${sch_cmd}" = "ssid_add" -o "${sch_cmd}" = "ssid_rmv" ]; then
		handle_ssid
		cmdstr="${sch_cmdstr}"
	else
		cmdstr="op=${sch_cmd}"
	fi

	if [ "${sch_apid}" != ",0" -a "${sch_apmac}" != "" ]; then
		sch_apid=`handle_apid`
	fi

	errmsg=`${FLOWEYE} sactask add apidstr=${sch_apid} cmdstr=${cmdstr}`

	if [ $? -ne 0 ]; then
		errmsg=`echo "${errmsg}" | tr " " "_"`
		save_log "${errmsg}"
	else
		taskid=`echo ${errmsg} | cut -d'=' -f2`
		save_log "RUN"
		# Avoid blocking scripts when querying task status
		${QUERY} ${taskid} ${sch_id} &
	fi
}


mkdir -p ${LOGDIR}

while true
do
	sleep 50

	cur_month=`date +%m | sed 's/^0//g'`
	cur_day=`date +%d | sed 's/^0//g'`
	cur_hour=`date +%H | sed 's/^0//g'`
	cur_min=`date +%M | sed 's/^0//g'`

	cat ${CRONTAB_CONF} | while read line
	do
	    hour_value=$(echo "$line" | grep -o 'hour=[^ ]*' | cut -d= -f2)
		min_value=$(echo "$line" | grep -o 'min=[^ ]*' | cut -d= -f2)
		arr_hour=(${hour_value//,/ })
        arr_min=(${min_value//,/ })
		sch_id=""
		sch_day=""
		sch_hour=""
		sch_min=""
		sch_cmd=""
		sch_cmdstr=""
		

	      for val in ${line}
		  do
		  if [[ ! "$val" =~ "hour" ]] && [[ ! "$val" =~ "min" ]];then
              eval "sch_${val}"
		  fi
		  for ((i=0; i<${#arr_hour[@]}; i++)); do
		    eval "sch_hour=${arr_hour[$i]}"
			eval "sch_min=${arr_min[$i]}"
             case "${sch_month}" in
			"day")	# Every day
				if [ "${cur_hour}" = "${sch_hour}" -a "${cur_min}" = "${sch_min}" ]; then
					task_commit
				fi
				;;

			"week")	# Every week
				cur_week=`date +%w`
				[ ${cur_week} -eq 0 ] && cur_week=7
				if [ "${cur_week}" = "${sch_day}" -a "${cur_hour}" = "${sch_hour}" -a "${cur_min}" = "${sch_min}" ]; then
					task_commit
				fi
				;;

			"month") # Every month
				if [ "${cur_day}" = "${sch_day}" -a "${cur_hour}" = "${sch_hour}" -a "${cur_min}" = "${sch_min}" ]; then
					task_commit
				fi
				;;

			*) # 1 - 12 month
				if [ "${cur_month}" = "${sch_month}" -a "${cur_day}" = "${sch_day}" -a "${cur_hour}" = "${sch_hour}" -a "${cur_min}" = "${sch_min}" ]; then
					task_commit
				fi
				;;
		    esac
          done
		done
	done
done