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

"
echo -n ""; 
. ../common/common.sh
myself="/cgi-bin/Maintain/`basename $0`"

echo -n "
<script languate=\"javascript\">
function Validate(frm)
{
	var dbfile = document.getElementsByName(\"sigdbfile\")[0];
	if (dbfile.value == \"\") {
		alert(\"·!\");
		return false;
	}
	if (confirm(\"ȷҪϴ?\"))
		return true;
	else
		return false;
}
</script>
";
RAMDISK=/usr/ramdisk
if [ ! -d ${RAMDISK} ]; then
	afm_dialog_msg "/usr/ramdisk!"
	exit 0
fi
if [ "${CGI_action}" = "upload" ]; then
	operator_check "${myself}"
	# verify the archive
	fileok=`tar ztvf ${CGI_sigdbfile} | grep panabit.inf`
	if [ "${fileok}" = "" ]; then
		afm_dialog_msg "Чϵͳ:汾Ϣļ!"
		rm -rf ${CGI_sigdbfile}
		afm_load_page 0 "${myself}"
		sync
		exit 0
	fi
	# give it more space for upgrading
	rm -rf ${RAMDISK}/admin/tmp/*
	mkdir -p ${RAMDISK}/sysupgrade
	rm -rf ${RAMDISK}/sysupgrade/*
	mkdir -p ${RAMDISK}/tmp
	rm -rf ${RAMDISK}/tmp/*
	errmsg=`tar xzvf ${CGI_sigdbfile} -C ${RAMDISK}/sysupgrade 2>&1`
	if [ "$?" != "0" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		rm -rf ${CGI_sigdbfile}
		sync
		afm_dialog_msg "ϵͳʱ⣬ϵȷϵͳȷ!"
		afm_load_page 0 "${myself}"
		exit 0
	fi
	rm -rf ${CGI_sigdbfile}	
	sync
	panabitinf=`find ${RAMDISK}/sysupgrade -name panabit.inf`
	if [ "${panabitinf}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_dialog_msg "Чϵͳ:汾Ϣļ!"
		afm_load_page 0 "${myself}"
		exit 0
	fi
	. ${panabitinf}
	# Check BSD version
	jrel=`${FLOWEYE} jflow stat | grep jflow_jos_release | cut -d'=' -f2`
	if [ ${jrel} -le 1 ]; then
		curbsd=`uname -r | cut -d'.' -f1`
		pkgbsd=`echo ${BSDVER} | cut -d'.' -f1`
		if [ "${curbsd}" != "${pkgbsd}" ]; then
			afm_dialog_msg "BSD汾һ£ǰBSD汾Ϊ`uname -r |cut -d'-' -f1`õBSD汾Ϊ${BSDVER}"
			rm -rf ${RAMDISK}/sysupgrade/*
			afm_load_page 0 "${myself}"
			exit 0
		fi
	elif [ ${jrel} -gt 1 ]; then
		curbsd="`uname -r | cut -d'-' -f1`"
		if [ "${curbsd}" != "${BSDVER}" ]; then
			afm_dialog_msg "BSD汾һ£ǰBSD汾Ϊ${curbsd}õBSD汾Ϊ${BSDVER}"
			rm -rf ${RAMDISK}/sysupgrade/*
			afm_load_page 0 "${myself}"
			exit 0
		fi
	fi
fi
if [ "${CGI_action}" = "upgrade" ]; then
	mkdir -p ${RAMDISK}/tmp
	rm -f ${RAMDISK}/tmp/sys_up_success
	fontsz="14px"
	kerneldir=`find ${RAMDISK}/sysupgrade -name kernel`
	admindir=`find ${RAMDISK}/sysupgrade -name admin`
	bindir=`find ${RAMDISK}/sysupgrade -name bin`
        if [ "${kerneldir}" = "" -o "${admindir}" = "" -o "${bindir}" = "" ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">⣬ϵ!</p></b></br>"
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_load_page 0 "${myself}"
		exit 0
	fi
       	# verify the package.
       	if [ ! -f ${kerneldir}/joskm.ko -o ! -f ${bindir}/panaos -o ! -f ${bindir}/floweye ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">⣬ϵ!</p></b></br>"
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_load_page 0 "${myself}"
		exit 0
	fi
	pkgroot=`dirname ${kerneldir}`
	binroot=`dirname ${bindir}`
	admroot=`dirname ${admindir}`
	if [ "${pkgroot}" != "${binroot}" -o "${pkgroot}" != "${admroot}" ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">⣬ϵ!</p></b></br>"
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_load_page 0 "${myself}"
		exit 0
	fi
	if [ -d ${pkgroot}/usr ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">г쳣Ŀ¼!</!</p></b></br>"
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_load_page 0 "${myself}"
		exit 0
	fi
	. /etc/PG.conf
	if [ ! -f ${PGPATH}/admin/.htpasswd ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">ϵͳ𻵣ϵ!</p></b></br>"
		rm -rf ${RAMDISK}/sysupgrade/*
		afm_load_page 0 "${myself}"
		exit 0
	fi
	if [ -d ${PGPATH}/usr ]; then
		echo "<br><b><p style=\"color:#ff0000;font-size:${fontsz}\">ϵͳг쳣Ŀ¼ɾ֮!</!</p></b></br>"
		rm -Rf ${PGPATH}/usr
	fi
	echo "<br><b><p style=\"color:#0000ff;font-size:${fontsz}\">ȷЧʼ......</p></b>"
        cp -f ${PGPATH}/admin/.htpasswd ${DATAPATH}/.htpasswd
        cp -Rf ${pkgroot}/* ${PGPATH}/
        cp -f ${DATAPATH}/.htpasswd ${PGPATH}/admin/.htpasswd
	rm -f ${DATAPATH}/.htpasswd
	# upgrade kernel part
        cp -f ${kerneldir}/*.ko /boot/kernel/
	sync
	sync
	rm -rf ${RAMDISK}/sysupgrade/*
	# Copy to ramdisk
	cp -Rf ${PGPATH}/* ${RAMDISK}/
	echo "upgrade_system yes" >> ${EVENTFILE}
	waitsecs=20
	echo "<br><b><p style=\"color:#0000ff;font-size:${fontsz}\">ڸ£Ҫ${waitsecs}ңĵȴ......</p></b>"
	WEB_LOGGER "ϵͳ" ""
	# Delete the tmp file
	afm_load_page ${waitsecs} "${myself}?action=afterupgrade"
	exit 0
fi
if [ "${CGI_action}" = "afterupgrade" ]; then
	if [ -f ${RAMDISK}/tmp/sys_up_success ]; then
		rm -f ${RAMDISK}/tmp/sys_up_success
		afm_dialog_msg "ɹ!"
	else
		afm_dialog_msg "ʧ!"
	fi
fi

echo -n "
<body>
"; 
cgi_show_title "ϵͳ->ϵͳ"
if [ "${CGI_action}" = "upload" ]; then 

echo -n "
<br>
<table style=\"width:100%; font-size:14px;\">
<tr>
<td width=20></td>
<td align=left>ϴϵͳ\"${BUILDDATE}\", Ϊ\"${BUILDREL}\"</td></tr>
<table>
<br>
<table style=\"width:100%; font-size:14px;\">
<tr>
<td width=20></td>
<td align=left>˴<a style=\"color:#0000ff;font-size:15px\" href=\"${myself}?action=upgrade\"><b></b></a>(ע⣬̻ж缸)</td></tr>
</table>
<br>
<table style=\"width:100%; font-size:14px;\">
<tr>
<td width=20></td>
<td align=left>,˴<a style=\"color:#0000ff;font-size:15px\" href=\"${myself}?action=return\"><b>ɾ</b></a>ղϴռñflashռ</td><r>
</table></body></html>
";
	exit 0
fi
[ -f ${PGPATH}/etc/panabit.inf ] && . ${PGPATH}/etc/panabit.inf
if [ ${JOS_RELEASE} -ne 0 ]; then
	for nameval in `${FLOWEYE} key info`; do
		eval "${nameval}"
	done
else
	cantupgrade=0
fi
if [ "${cantupgrade}" = "1" ]; then
	echo "<br><p style=\"color:#ff0000;font-size:15px\">ЧޣϵһȡLicense!</p></br>"
	exit 0
fi
freebsdver=`uname -r | cut -d'-' -f1`

echo -n "
<br>
<table style=\"width:700; border-bottom:1px #787882 solid; font-size:14px;font-bold:true;\">
<tr><td align=left><b>Ϣʾ</b></td></tr>
</table>
<br>
<table width=\"700\" border=0 cellspacing=1 cellpadding=3 style=\"font-size:13px;font-bold:true;\">
<tr>
    <td width=20></td>
"; if [ "${TOPSEC}" != "" ]; then 
echo -n "
    <td align=left>1. ǰ汾\"${BUILDREL}\"</td>
"; else 
echo -n "
    <td align=left>1. ǰ汾\"${BUILDREL}\"${BUILDDATE}[${freebsdver}]</td>
"; fi 
echo -n "
</tr>
<tr>
    <td width></td>
    <td align=left>2. һ: ϴϵͳϴɹϵͳʾ</td>
</tr>
<tr>
    <td></td>
    <td align=left>3. ڶ: ̻ỨѼʱ䣬ڴڼڣжϣ׼</td>
</tr>
</table>
<br>
<table style=\"width:700; border-bottom:1px #787882 solid; font-size:14px;font-bold:true;\">
<tr><td align=left><b>ϵͳ</b></td></tr>
</table>
<br>
<form method=post onsubmit=\"return Validate(this)\" action=\"${myself}\" enctype=\"multipart/form-data\">
<table width=700 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr height=22>
	<td width=20></td>
	<td width=140 style=\"font-bold:true;font-size:14px\" align=left>·</td>
	<td width=* class=cssTDArial align=left>
		<input type=file name=sigdbfile style=\"width:450px\">
	</td>
	<td align=right>
		<input type=hidden name=action value=upload></input>
		<input type=submit style=\"width:90\" value=\"ϴ\"></input>
	</td>
</tr>
</table>
</form>
";
if [ "${CGI_action}" = "afterupgrade" ]; then
	if [ ! -e ${RAMDISK}/sysupgrade/panabit.tar.gz ]; then
		afm_dialog_msg "رյǰڣµ½ϵͳЧ!"
	fi
fi

echo -n "
</body>
</html>
";