#!/bin/sh

. ../common/ajax_common

#APP CONF
CONFDIR="${PGETC}/App/sac"
APPCONF="${CONFDIR}/sac.conf"
CRONTAB_CONF="${CONFDIR}/crontab.conf"

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


if [ "${PALANG}" = "en" ]; then
	LANG_001="Success"
fi


add_crontab()
{
	mkdir -p ${PGETC}/App/sac/
	mkdir -p ${LOGDIR}

	id=`awk '{split($1, id, "="); print id[2]}' ${CRONTAB_CONF} | sort -nk1 | tail -1`

	if [ "${id}" = "" ]; then
		id=1
	else
		id=$((${id} + 1))
	fi
	
	cmdstr=""
	if [ "${CGI_cmd}" = "ssid_rmv" ]; then
		cmdstr="\"op=delwlan&ssidstr=${CGI_ssidstr}\""
	fi

    
	echo "id=${id} month=${CGI_month} day=${CGI_day} hour=${CGI_hour} min=${CGI_min} cmd=${CGI_cmd} cmdstr=${cmdstr} apid=${CGI_apid} apmac=${CGI_apmac} birth=`date +%s`" >> ${CRONTAB_CONF}
	
	rm ${LOGDIR}/*
	WEB_LOGGER "SACƻ" "id=${CGI_id}"
	retjson 0 "${LANG_001:=ɹ}"
}


rmv_crontab()
{   

    id_str="${CGI_id}"
    while IFS=',' read -ra array; do
      for item in "${array[@]}"; do

    row="id=$item"
	touch ${CRONTAB_CONF}.bak
	touch ${CRONTAB_STAT}.bak

	# delete crontab conf
	awk -v row="${row}" -v bak="${CRONTAB_CONF}.bak" \
	'{
		if($1 == row)
			next;
		print $0 >> bak;
	}' ${CRONTAB_CONF}

	mv ${CRONTAB_CONF}.bak ${CRONTAB_CONF}

	# delete crontab log
	awk -v row="${row}" -v bak="${CRONTAB_STAT}.bak" \
	'{
		if($1 == row)
			next;
		print $0 >> bak;
	}' ${CRONTAB_STAT}

	mv ${CRONTAB_STAT}.bak ${CRONTAB_STAT}
	
	# delete ap log
	rm -rf "${LOGDIR}/crontab_${item}_ap.stat"
	
	WEB_LOGGER "ɾSACƻ" "id=${item}"
 
      done
   done <<< "$id_str"

   retjson 0 "${LANG_001:=ɹ}"	
}


edit_crontab()
{
	cmdstr=`echo "${CGI_cmdstr}" | tr " " "&"`

	grep -v "^id=${CGI_id} " ${CRONTAB_CONF} > ${CRONTAB_CONF}.bak
	echo "id=${CGI_id} month=${CGI_month} day=${CGI_day} hour=${CGI_hour} min=${CGI_min} cmd=${CGI_cmd} cmdstr=\"${cmdstr}\" apid=${CGI_apid}  apmac=${CGI_apmac} birth=${CGI_birth}" >> ${CRONTAB_CONF}.bak

	mv ${CRONTAB_CONF}.bak ${CRONTAB_CONF}

	rm ${LOGDIR}/*
	WEB_LOGGER "༭SACƻ" "id=${CGI_id}"
	retjson 0 "${LANG_001:=ɹ}"
}


list_crontab()
{
	dot=""

	printf "["
	
	while read line
	do
		[ "${line}" = "" ] && continue
		
		exec_time=""
		task_cnt=""
		task_stat=""

		for item in ${line}
		do
			eval ${item}
		done

		[ "${CGI_cmd}" != "" -a "${CGI_cmd}" != "${cmd}" ] && continue

		if [ -f ${CRONTAB_STAT} ]; then
			# id= exec_time= task_stat= task_cnt=
			line=`grep -E "^id=${id}" ${CRONTAB_STAT}`
			for item in ${line}
			do
				export ${item}
			done
		fi

		printf "${dot}{"
		printf "\"id\":\"${id}\","
		printf "\"birth\":\"${birth}\","
		printf "\"exec_time\":\"${exec_time}\","
		printf "\"task_cnt\":\"${task_cnt}\","
		printf "\"task_stat\":\"${task_stat}\","
		printf "\"month\":\"${month}\","
		printf "\"day\":\"${day}\","
		printf "\"hour\":\"${hour}\","
		printf "\"min\":\"${min}\","
		printf "\"cmd\":\"${cmd}\","
		printf "\"cmdstr\":\"${cmdstr}\","
		printf "\"apmac\":\"${apmac}\","
		printf "\"apid\":\"${apid}\""
		printf "}"

		[ "${dot}" = "" ] && dot=","
	done << EOF
`cat ${CRONTAB_CONF} | grep -i "${CGI_keyword}" `
EOF

	printf "]"
}


list_apstat()
{
	dot=""
	AP_STAT="${LOGDIR}/crontab_${CGI_id}_ap.stat"

	printf "["

	if [ ! -f ${AP_STAT} ]; then
		for apid in `echo ${CGI_apid} | tr "," " "`
		do
			apname=`${FLOWEYE} sac list json=0 | awk -v apid=${apid} '{if($3==apid) print $4}'`
			
			printf "${dot}"
			printf "{"
			printf "\"apname\":\"`utf8togb2312 ${apname}`\","
			printf "\"cmdstat\":\"8\","
			printf "\"reply\":\"\""
			printf "}"

			[ "${dot}" = "" ] && dot=","
		done
	else
		cat ${AP_STAT} | while read line
		do
			for val in ${line}
			do
				eval ${val}
			done

			printf "${dot}"
			printf "{"
			printf "\"apname\":\"${apname}\","
			printf "\"cmdstat\":\"${cmdstat}\","
			printf "\"reply\":\"${reply}\""
			printf "}"

			[ "${dot}" = "" ] && dot=","
		done
	fi

	printf "]"
}


case "${CGI_action}" in
	"add_crontab")
		action_check
		add_crontab
		;;

	"rmv_crontab")
		action_check
		rmv_crontab
		;;

	"edit_crontab")
		action_check
		edit_crontab
		;;
	
	"list_crontab")
		retjson 0 "OK" "`list_crontab`"
		;;
		
	"list_apstat")
		retjson 0 "OK" "`list_apstat`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
