#!/bin/sh
#This script is created by ssparser automatically. The parser first created by ZhangXiao
printf "Content-type: text/html
Cache-Control: no-cache
Connection: close

"
echo -n "";

RAMDISK="/usr/ramdisk"
if [ -f ${RAMDISK}/PG.conf ]; then
	. ${RAMDISK}/PG.conf
	PGPATH="${RAMDISK}"
else
	. /etc/PG.conf
fi

FLOWEYE=${PGPATH}/bin/floweye
MD5_ENCRYPT=/sbin/md5
URLDECODE=${PGPATH}/bin/cfg_urldecode
HTPASSWD=${PGPATH}/admin/.htpasswd
PANABIT_INF=${PGPATH}/etc/panabit.inf
CUR_CONF=${PGETC}/panabit.conf

#obtain my device information
my_admin=`sed -n '1p' ${HTPASSWD} | cut -d ':' -f1`
my_pwd=`sed -n '1p' ${HTPASSWD} | cut -d ':' -f2`

temp=`grep "BUILDREL" ${PANABIT_INF}`
my_buildrel=${temp#*=};

temp=`grep "BUILDDATE" ${PANABIT_INF}`
my_builddate=${temp#*=}

my_bsdver=`uname -r | cut -d '-' -f1`

temp=`${FLOWEYE} jflow stat | grep "jflow_ipe_version"`
my_jflow_ipe_version=${temp#*=}

my_data_ports=`${FLOWEYE} if list | awk '{printf "%s ", $1}'`
my_data_ports_num=`echo "${my_data_ports}" | awk '{print NF}'`

my_key=`cat ${CUR_CONF} | ${MD5_ENCRYPT}`

#obtain device information from client
replace=`echo ${QUERY_STRING}`
temp=`echo ${replace} | cut -d '&' -f1`
pridev_admin=${temp#*=}
pridev_admin=`${URLDECODE} ${pridev_admin}`

temp=`echo ${replace} | cut -d '&' -f2`
pridev_pwd=${temp#*=}
pridev_pwd=`${URLDECODE} ${pridev_pwd}`

temp=`echo ${replace} | cut -d '&' -f3`
buildrel=${temp#*=}
buildrel=`${URLDECODE} ${buildrel}`

temp=`echo ${replace} | cut -d '&' -f4`
builddate=${temp#*=}
builddate=`${URLDECODE} ${builddate}`

temp=`echo ${replace} | cut -d '&' -f5`
bsdver=${temp#*=}

temp=`echo ${replace} | cut -d '&' -f6`
jflow_ipe_version=${temp#*=}

temp=`echo ${replace} | cut -d '&' -f7`
data_ports=${temp#*=}
data_ports=`${URLDECODE} ${data_ports}`
data_ports_num=`echo "${data_ports}" | awk '{print NF}'`

temp=`echo ${replace} | cut -d '&' -f8`
key=${temp#*=}


if [ "${REQUEST_METHOD}" = "GET" ]; then
	if [ "${my_admin}" != "${pridev_admin}" -o "${my_pwd}" != "${pridev_pwd}" ]; then
		#error:01 indicates that admin or pwd doesn't matching
		echo "ERROR:1";
	else
		if [ "${my_buildrel}" != "${buildrel}" -o "${my_builddate}" != "${builddate}" -o \
			 "${my_jflow_ipe_version}" != "${jflow_ipe_version}" ]; then
			#erro:02 indicates that configuration not matching
			echo "ERROR:2";

		elif [ "${my_bsdver}" != "${bsdver}" ]; then
			echo "ERROR:3";
		else
			#judge data_ports list matching or not
			if [ "${data_ports_num}" -ne "${my_data_ports_num}" ]; then
				echo "ERROR:4";
				exit 0
			fi
			count=1
			while [ "${data_ports_num}" -ge "${count}" ];
			do 
				temp=`echo "${data_ports}" | awk '{print $"'${count}'"}'`
				matching=`echo "${my_data_ports}" | grep "${temp}"`
				if [ "${matching}" = "" ]; then
					echo "ERROR:5";
					exittag="exit"
					break;
				fi
				count=`expr ${count} + 1`
			done
			if [ "${exittag}" != "" ]; then
				exit 0
			fi
			
			#anthentication passed
			if [ "${key}" != "${my_key}" ]; then
				cat ${CUR_CONF}
			else
				#same conf
				echo "ERROR:6";
			fi
		fi
	fi
fi
