#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Import failed. Line"	 					#ʧǰ׺
	LANG002=" contains special characters ($%<>). "
    LANG002="${LANG002}Please modify and re-upload."	#ʧܺ׺
	LANG003="Success"									#ɹ
fi

import_file()
{
	file_path="${1}"
	[ "${file_path}" = "" ] && file_path="${CGI_file}"
	[ "${file_path}" = "" ] && retjson 1 "NO_FILE"

	errmsg=$(check_file ${file_path})
	[ ! -z "${errmsg}" ] && rm -rf "${file_path}" && retjson 1 "${errmsg}"

	errmsg=`${FLOWEYE} pppoeacct load file=${file_path}`

	rm -rf "${file_path}"
}

check_file()
{
	ckfile="${1}"
	prefix="${LANG001:=ʧ}"
	suffix="${LANG002:=аַ$%<>޸ĺϴ}"

	awk -v prefix="${prefix}" -v suffix="${suffix}" -F ' ' '
	{
		str = $3 $4 $14
       		if (str ~ /\$/ || str ~ /%/ || str ~ /</ || str ~ />/ || str ~ // || str ~ //) {
        	    rows = rows (rows ? "" : "") NR
        	}	
	}
	END {
		if (rows) {
        	printf "%s%s%s\n", prefix, rows, suffix
        	exit 1
    	}
	}' ${ckfile}
}


csv_format()
{
	row=1

	file_path="/${TMPDIR}/csv_account.conf"
	
	mkdir -p "${TMPDIR}"
	[ -f "${file_path}" ] && rm -rf "${file_path}"
	touch ${file_path}

	awk -v file="${file_path}" -F"," \
	'{
		if(NR == 1) next;

		split($11, vlan, "/");

		result = sprintf("u g=%s n=%s p=%s b=%s e=%s s=%s maxonline=%s mac=%s ip=%s outvlan=%s outvlan1=%s",
			$1, $3, $4, $5, $6, $7, $8, $9, $10, vlan[1], vlan[2]);

		if($12 != "NULL")
			result = sprintf("%s outif=%s", result, $12);

		result = sprintf("%s m=0;%s", result, $13);

		print result >> file;
	}' ${CGI_file}

	cp ${file_path} /root/import.log

	import_file ${file_path}
}


haizhizhu()
{
	echo "a"
}


ros()
{
	echo "a"
}


wayos()
{
	echo "a"
}


bytevalue()
{
	echo "a"
}


ikuai()
{
	echo "a"
}


bithighway()
{
	echo "a"
}


natshell()
{
	echo "a"
}


lfradius()
{
	echo "a"
}


sxzros()
{
	echo "a"
}


action_check

case "${CGI_file_type}" in
	"local")
		import_file
		;;

	"csv")
		csv_format
		;;
	
	"haizhizhu")
		haizhizhu
		;;
	
	"ros")
		ros
		;;
		
	"wayos")
		;;
	
	"bytevalue")
		;;
	
	"ikuai")
		;;

	"bithighway")
		;;
	
	"natshell")
		;;
		
	"lfradius")
		;;

	"sxzros")
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac

rm ${CGI_file}
retjson 0 "${LANG003:=ɹ}"
