#!/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 (confirm(\"ȷҪϴ½?\"))
		return true;
	else
		return false;
}
</script>
";
fontsz=13
APPWEBDIR="/usr/appweb"

if [ "${CGI_action}" = "upload" ]; then
	operator_check "${myself}"
	# verify the archive
	fileok=`tar ztvf ${CGI_sigdbfile} | grep "panabit/www/"`
	if [ "${fileok}" = "" ]; then
		afm_dialog_msg "ϴļʽ!"
		afm_load_page 0 "${myself}"
		exit 0
	fi
  
	# Stop appweb and lighttpd
	errmsg=`killall appwebs 2>&1`
  errmsg=`killall spingsvr 2>&1`
	errmsg=`killall -15 lighttpd 2>&1`

	sleep 1
  if [ -d ${APPWEBDIR} ]; then
	    echo "<br><b><p style=\"color:#0000ff;font:${fontsz}\">ɾɰ½.....</p></b>"
      errmsg=`rm /usr/lib/libphp5.so 2>&1`
      rm -rf ${APPWEBDIR}
      sync
  fi

	echo "<br><b><p style=\"color:#0000ff;font:${fontsz}\">ʼ.....</p></b></br>"

	errmsg=`tar xzf ${CGI_sigdbfile} -C /usr/ 2>&1`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ѹ½ʱ⣬ϵȷϵͳȷ!"
		rm -rf ${CGI_sigdbfile}
		afm_load_page 0 "${myself}"
		sync
		exit 0
	else
		rm -rf ${CGI_sigdbfile}
		sync
	fi
  
	# start appwebs service
	mkdir -p /usr/ramdisk/tmp
  
  # cpoy web server and www and spingsvr
  if [ -f ${RAMDISK}/PG.conf ]; then
  	. ${RAMDISK}/PG.conf 
  else
  	. /etc/PG.conf
  fi 
  
  if [ -f ${RAMDISK}/etc/lighty_port.conf ]; then
      cp ${RAMDISK}/etc/lighty_port.conf ${PGPATH}/etc/lighty_port.conf
  fi
  
  cp -fRp ${PGPATH}/bin/spingsvr ${PGPATH}/bin/sping ${RAMDISK}/bin/
  sync
  cp -fRp ${PGPATH}/etc/lighty_*.conf ${RAMDISK}/etc/
  sync
  cp -fRp ${PGPATH}/www ${RAMDISK}/
  sync
  
  [ -f ${RAMDISK}/PG.conf ] && PGPATH="${RAMDISK}"
  
  # start web server
  cd /usr/local/sbin
  ./lighttpd -f ${PGPATH}/etc/lighty_user.conf
  cd -
  
	echo "<br><b><p style=\"color:#0000ff;font:${fontsz}\">ɹ,Ϊȷ飬ʹIE8ʹ½!</p></b></br>"
  afm_dialog_msg "ɹ,Ϊȷ飬ʹIE8ʹ½!"
	afm_load_page 0 "${myself}"
	exit 0
fi

echo -n "
<body>
"; 
cgi_show_title "ϵͳ->½"
[ -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

caption="ʾӰжϣҲ½"
if [ -f ${PGPATH}/etc/lighty_port.conf ]; then 
  server_root=`cat ${PGPATH}/etc/lighty_port.conf | grep var.server_root | cut -d'=' -f2`
  . ${PGETC}/ifadmin.conf
  if [ "${server_root}" = "" ]; then
      caption="ʾӰжϣǰ½ʶ˿ڵ"
  else
      caption="ʾӰжϣ½ʵַΪhttp://${ADMIN_IP}:${server_root}ĬΪǰҳnetzone"
  fi
fi

echo -n "
<br>
<table style=\"width:750; border-bottom:1px #787882 solid; font:14px bold ; color:#ff0000\">
<tr>
	<td ><b>${caption}</b></td>
</tr>
</table>
<br>
<form method=post onsubmit=\"return Validate(this)\" action=\"${myself}\" enctype=\"multipart/form-data\">
<table width=750 border=0 cellspacing=1 cellpadding=1 bgcolor=\"#ffffff\">
<tr height=22>
	<td width=20></td>
	<td width=170 style=\"font:14px bold\" align=left><b>½·:</b></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:100\" value=\"½\"></input>
	</td>
</tr>
</table>
</form>
</body>
</html>
";