#!/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"


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}\"}"
}


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

	awk 'BEGIN{
		dot="";
		printf "[";
	}{
		if($1 !~ /interface/)
			next;
		split($2, val, "=");
		printf "%s\"%s\"", dot, val[2];
		if(dot=="") dot=",";
	}END{
		printf "]";
	}' ${1}
}


get_local_interface()
{
	${FLOWEYE} if list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s\"%s\"", dot, $1;
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


upload_config()
{
	mkdir -p ${DATAPATH}/config
	tag=`grep "ENDCONFIG" "${CGI_file}"`

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


inport_yes()
{
	ostype=`uname`
	import_conf="${DATAPATH}/config/panabit.conf"

	for item in ${CGI_interface}
	do
		[ "${item}" = "" ] && continue
		inetif=`echo ${item} | cut -d"," -f1`
		lnetif=`echo ${item} | cut -d"," -f2`
		[ "${lnetif}" = "" ] && continue

		if [ "${ostype}" = "FreeBSD" ]; then
			sed -i '' "s/=${inetif} /=${lnetif} /g" ${import_conf}
			sed -i '' "s/=${inetif}\$/=${lnetif}/g" ${import_conf}
		else
			sed -i "s/=${inetif} /=${lnetif} /g" ${import_conf}
			sed -i "s/=${inetif}\$/=${lnetif}/g" ${import_conf}
		fi
	done
	
	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()
{
	if [ "${CGI_type}" = "arm_reset" ]; then
		[ "`uname -m`" != "aarch64" ] && retjson 1 "INV_SYSTEM"

		rm -rf /overlay/*
		${WEBUICTL} set_power reboot >/dev/null 2>&1 &

		retjson 0 "ù̼ɹϵͳ"
	fi

	# 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}/hasync.conf"
		rm -rf "${PGETC}/App"
		rm -rf "${PGETC}/web"
		rm -rf /etc/standby
		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
	rm -rf ${DATAPATH}/App/
	
	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()
{
	sync_module="${DEFAULT_SYNC_MODULE}"

	if [ ! -f "${SYNC_CONF}" ]; then
		printf "{"
		printf "\"sync_module\":\"${sync_module}\""
		printf "}"
		return
	fi

	. ${SYNC_CONF}

	printf "{"
	printf "\"enable\":\"${enable}\""
	printf ",\"type\":\"${type}\""
	printf ",\"int\":\"${int}\""
	printf ",\"key\":\"${key}\""
	printf ",\"sync_module\":\"${sync_module}\""
	printf "}"
}


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

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

	echo "enable=${enable}" > ${SYNC_CONF}
	echo "int=${CGI_int}" >> ${SYNC_CONF}
	echo "key=${CGI_key}" >> ${SYNC_CONF}
	echo "sync_module=\" ${CGI_sync_module} \"" >> ${SYNC_CONF}

	${WEBUICTL} start confsync 2>&1 >/dev/null &
	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 "\"port\":\"${port}\","
		printf "\"sync\":${sync}"
		printf "}"
		[ "${dot}" = "" ] && dot=","
	done

	printf "]"
}


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

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

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

	${WEBUICTL} start confsync 2>&1 >/dev/null &
	retjson 0 "ɹ"
}


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

	${WEBUICTL} start confsync 2>&1 >/dev/null &
	retjson 0 "ɹ"
}


rmv_cmd_reciver()
{
	grep -v "^ip=${CGI_ip} " ${SYNC_LIST} > ${SYNC_LIST}.bak
	mv ${SYNC_LIST}.bak ${SYNC_LIST}
	
	${WEBUICTL} start confsync 2>&1 >/dev/null &
	retjson 0 "ɹ"
}


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

	awk \
	'BEGIN{
		printf "[";
	} {
		printf "%s", $0;
	} END{
		printf "]";
	}' ${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

