#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Export_System_Configure"	#ļ
	LANG002="INV_FILE"	#ļȷ
	LANG003="Upload_Success"	#ϴɹ
	LANG004="Import_System_Configure"	#ϵͳļ
	LANG005="Restore_System_Configure"	#ָʼ
fi

SYNC_LIST="${PGETC}/web/config_sync.list"
SYNC_CONF="${PGETC}/web/config_sync.conf"
SYNC_LOGS="/usr/ramdisk/tmp/ipe_syncd/ipe_syncd.log"
WEBUI_CTRL="/usr/ramdisk/app/webui/appctrl"


export_config()
{
	mkdir -p ${WEB_DOWNLOAD}

	file_name="system.conf"
	target="${WEB_DOWNLOAD}/${file_name}"

	if [ ! -f "${PGETC}/panabit.conf" ]; then
		echo "ENDCONFIG" > ${target}
	else
		cp "${PGETC}/panabit.conf" ${target}
	fi

	WEB_LOGGER "${LANG001:=ļ}" "name=${file_name}"
	retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


upload_config()
{
	tag=`grep "ENDCONFIG" "${CGI_file}"`

	if [ "${tag}" = "" ]; then
		rm -rf "${CGI_file}"
		retjson 1 "${LANG002:=ļȷ}"
	else
		mv ${CGI_file} ${DATAPATH}/config/panabit.conf
		retjson 0 "${LANG003:=ϴɹ}"
	fi
}


inport_yes()
{
	WEB_LOGGER "${LANG004:=ϵͳļ}"
	echo "import_config yes" >> ${EVENTFILE}
	retjson 0 "OK"
}


inport_cancel()
{
	rm ${DATAPATH}/config/panabit.conf
	retjson 0 "OK"
}


query_inport()
{
	if [ ! -e ${DATAPATH}/config/panabit.conf ]; then
		retjson 0 "done"
	else
		retjson 0 "run"
	fi
}


conf_reset()
{
	# CGI_type=data, ʾNTM汾ֻ
	# ĬȫգNTM, Panabit˺ţԼAPP
	if [ "${CGI_type}" = "data" ]; then
		cp ${PGETC}/panabit.conf ${DATAPATH}/config/panabit.conf
	else
		rm -rf ${PGETC}/"ntm_netassets.conf"
		rm -rf "${PGETC}/panabit.conf"
		rm -rf "${PGETC}/account.conf"
		rm -rf "${PGETC}/App"
		echo "ENDCONFIG" > ${DATAPATH}/config/panabit.conf
	fi

	#ʷ
	killall ipe_datamon >/dev/null 2>&1
	killall ipe_datamon >/dev/null 2>&1

	rm -rf ${DATAPATH}/panabit.rrd
	rm -rf ${DATAPATH}/panabit.raz
	rm -rf ${DATAPATH}/*.log

	WEB_LOGGER "${LANG005:=ָʼ}"
	sync

	# PanaosdatamonȽ
	if [ "${NTM}" = "1" ]; then
		${RAMDISK}/app/ntm/bin/ntm_data_clear >/dev/null 2>&1 &
	else
		echo "import_config yes" >> ${EVENTFILE}
		echo "start_datamon yes" >> ${EVENTFILE}
	fi

	retjson 0 "OK"
}


load_cmdsync_config()
{
	if [ ! -f "${SYNC_CONF}" ]; then
		printf "{}"
		return
	fi

	awk -F"="\
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":\"%s\"", dot, $1, $2;
		if(dot == "") dot = ",";
	}END{
		printf "}";
	}' ${SYNC_CONF}
}


set_cmdsync_config()
{
	mkdir -p "${PGETC}/web/"

	[ "${CGI_enable}" = "on" ] && enable=1 || enable=0

	echo "enable=${enable}" > ${SYNC_CONF}
	echo "type=${CGI_type}" >> ${SYNC_CONF}
	echo "int=${CGI_int}" >> ${SYNC_CONF}
	echo "key=${CGI_key}" >> ${SYNC_CONF}

	${WEBUI_CTRL} start confsync_action
	retjson 0 "ɹ"
}


load_reciver_list()
{
	if [ ! -f "${SYNC_LIST}" ]; then
		printf "[]"
		return
	fi

	printf "["

	cat ${SYNC_LIST} | while read line
	do
		for val in ${line}
		do
			export ${val}
		done

		printf "${dot}{"
		printf "\"ip\":\"${ip}\","
		printf "\"key\":\"${key}\","
		printf "\"sync\":${sync}"
		printf "}"
		[ "${dot}" = "" ] && dot=","
	done

	printf "]"
}


add_cmd_reciver()
{
	[ "${CGI_ip}" = "" ] && retjson 1 "INV_IP"

	exist=`grep -R "^ip=${CGI_ip} " ${SYNC_LIST}`
	[ "${exist}" != "" ] && retjson 1 "IP_EXISTS"

	mkdir -p "${PGETC}/web/"
	echo "ip=${CGI_ip} key=${CGI_key} sync=${CGI_sync}" >> ${SYNC_LIST}

	${WEBUI_CTRL} start confsync_action
	retjson 0 "ɹ"
}


edit_cmd_reciver()
{
	grep -v "^ip=${CGI_oldip} " ${SYNC_LIST} > ${SYNC_LIST}.bak
	mv ${SYNC_LIST}.bak ${SYNC_LIST}
	echo "ip=${CGI_ip} key=${CGI_key} sync=${CGI_sync}" >> ${SYNC_LIST}

	${WEBUI_CTRL} start confsync_action
	retjson 0 "ɹ"
}


rmv_cmd_reciver()
{
	grep -v "^ip=${CGI_ip} " ${SYNC_LIST} > ${SYNC_LIST}.bak
	mv ${SYNC_LIST}.bak ${SYNC_LIST}
	
	${WEBUI_CTRL} start confsync_action
	retjson 0 "ɹ"
}


load_cmdlog_list()
{
	if [ ! -f "${SYNC_LOGS}" ]; then
		printf "[]"
		return
	fi

	cat ${SYNC_LOGS}
}


case "${CGI_action}" in
	"export_config")
		action_check
		export_config
		;;
	
	"upload_config")
		action_check
		upload_config
		;;

	"inport_yes")
		action_check
		inport_yes
		;;

	"inport_cancel")
		action_check
		inport_cancel
		;;

	"query_inport")
		action_check
		query_inport
		;;

	"conf_reset")
		admin_check
		conf_reset
		;;

	"load_cmdsync_config")
		retjson 0 "OK" "`load_cmdsync_config`"
		;;

	"set_cmdsync_config")
		action_check
		set_cmdsync_config
		;;

	"load_reciver_list")
		retjson 0 "OK" "`load_reciver_list`"
		;;

	"add_cmd_reciver")
		action_check
		add_cmd_reciver
		;;

	"edit_cmd_reciver")
		action_check
		edit_cmd_reciver
		;;

	"rmv_cmd_reciver")
		action_check
		rmv_cmd_reciver
		;;

	"load_cmdlog_list")
		retjson 0 "OK" "`load_cmdlog_list`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac

