#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG_001="The file does not contain version information. Please check whether the file is correct"
	LANG_002="There is not enough space in the root directory. Please clean up and try to upgrade the system!"
	LANG_003="There is not enough space in the ramdisk. Please clean up and try to upgrade the system!"
	LANG_004="Invalid system: version information file does not exist!"
	LANG_005="When unpacking the package, please contact the manufacturer to ensure the system package is correct!"
	LANG_006="The uploaded upgrade package is inconsistent with the current version!"
	LANG_007="Upgrade_WEB_Interface"
	LANG_008="Upgrade success"
	LANG_009="Package Error"
	LANG_010="Upgrade System"
	LANG_011="Error unpacking feature library"
	LANG_012="Invalid feature library: no version information"
	LANG_013="Feature library is not compatible with current version"
	LANG_014="Invalid feature library: feature code file does not exist"
	LANG_015="Invalid feature library: dictionary table does not exist"
	LANG_016="System inconsistency"
	LANG_017="Upgrade failed, please confirm the validity of license with the manufacturer!"
	LANG_018="Import License"
	LANG_019="Import Success"
	LANG_020="Check upload packet"	#У
	LANG_021="Copy file to disk"	#ļӲ
	LANG_022="Copy file to ram"		#ļڴ
	LANG_023="Copy done, do restart" #ļɣʼϵͳ
	LANG_024="Extract the upgrade package file"	#ѹļ
	LANG_025="NO License" #Ȩļ
	LANG_026="An error occurred in the conversion version date"
	LANG_027="Uploaded upgrade package, not within the upgrade license time, can not upgrade"
    LANG_028="Signature database update expired"
    LANG_029="System license invalid"
    LANG_030="Signature database import failed"
fi

UPGRDE_NOTICE_FILE="${RAMDISK}/tmp/sys_up_status"


upable_check()
{
	for val in `${FLOWEYE} key info`
	do
		eval "L_${val}"
	done

	[ "${L_release}" = "0" ] && return
	[ "${L_oem}" = "" ] && return
	
	file_name=`tar -tf ${CGI_file} | head -1`
	if [ "${CGI_action}" = "upload_syspkt" ]; then
		version=`tar -xOzf ${CGI_file} ${file_name}etc/panabit.inf | grep BUILDDATE | cut -d"=" -f2 | sed 's/"//g'`
		if [ "${version}" = "" ]; then
			[ -f ${CGI_file} ] && rm ${CGI_file}
			retjson 1 "${LANG_001:=ļδ汾ϢļǷȷ}"
		fi
		if [ "`uname`" = "Linux" ]; then
			upload_version=`date -d "${version}" +%s`
		else
			upload_version=`date -j -f "%Y-%m-%d %H:%M:%S" "${version}" "+%s"`
		fi
	else
		version=`tar -xOzf ${CGI_file} ${file_name}dpi.inf | grep VERSION | cut -d"_" -f2`
		if [ "${version}" = "" ]; then
			[ -f ${CGI_file} ] && rm ${CGI_file}
			retjson 1 "${LANG_001:=ļδ汾ϢļǷȷ}"
		fi
		version=`echo ${version} | sed 's/\(....\)\(..\)\(..\)\.\(..\)\(..\)\(..\)/\1-\2-\3 \4:\5:\6/'`
		if [ "`uname`" = "Linux" ]; then
			upload_version=`date -d "${version}" +%s`
		else
			upload_version=`date -j -f "%Y-%m-%d %H:%M:%S" "${version}" "+%s"`
		fi
	fi

	[ "${upload_version}" = "" ] && retjson 1 "${LNAG_026:=ת汾ڷ}"

	if [ ${upload_version} -gt ${L_freeend} ]; then
		[ -f ${CGI_file} ] && rm ${CGI_file}
		retjson 1 "${LANG_027:=ϴδʱڣ޷}"
	fi
}


build_upret()
{
	while read line
	do
		eval OLD_${line}
	done << EOF
`cat /usr/ramdisk/etc/panabit.inf`
EOF

	if [ "${PALANG}" = "en" ]; then
		OLD_BUILDREL=`echo ${OLD_BUILDREL} | sed -r 's/\(.*\)//g'`
		BUILDREL=`echo ${BUILDREL} | sed -r 's/\(.*\)//g'`
	fi

	if [ "${NTM}" = "1" ]; then
		[ "${PAVER}" = "free" ] && JOS_RELEASE=0
		[ "${OLD_PAVER}" = "free" ] && OLD_JOS_RELEASE=0
	fi

	printf "{"
	printf "\"newdate\":\"${BUILDDATE}\","
	printf "\"curjos\":\"${OLD_JOS_RELEASE}\","
	printf "\"newjos\":\"${JOS_RELEASE}\","
	printf "\"curver\":\"R${OLD_ENVER}[${OLD_BUILDREL}]Build date ${OLD_BUILDDATE}\","
	printf "\"newver\":\"R${ENVER}[${BUILDREL}]Build date ${BUILDDATE}\""
	printf "}"
}


upload_syspkt()
{
	# restart monitor
	[ -d ${PGETC}/log/ ] && rm -rf ${PGETC}/log/*
	${IPECTRL} stop monitor >/dev/null 2>&1
	${IPECTRL} start monitor >/dev/null 2>&1

	# verify the root size
	root_size=6000
	root_size=`df -k | awk '{if($6=="/")print $4}'`
	if [ ${root_size} -le 2000 ]; then
		rm -rf ${CGI_file}
		sync
		retjson 1 "${LANG_002:=Ŀ¼ռ䲻㣬ٳϵͳ}"
	fi

	# verify the ramdisk size
	ram_size=10000
	need_size=34000
	ram_size=`df -k | awk '{if($6=="/usr/ramdisk")print $4}'`
	if [ ${ram_size} -le ${need_size} ]; then
		rm -rf ${CGI_file}
		sync
		retjson 1 "${LANG_003:=RAMDISKռ䲻㣬ٳϵͳ}"
	fi

	# verify the ramdisk size
	conf_size=10000
	conf_size=`df -k | awk -v conf="${PGETC}" '{if($6==conf)print $4}'`
	if [ ${conf_size} -le 5000 ]; then
		rm -rf ${CGI_file}
		sync
		retjson 1 "PGETCռ䲻㣬ٳϵͳ"
	fi

	# verify the archive
	fileok=`tar ztvf ${CGI_file} | grep urlencode`
	if [ "${fileok}" = "" ]; then
		rm -rf ${CGI_file}
		sync
		retjson 1 "⣬UIļ"
	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 zxf ${CGI_file} -C ${RAMDISK}/sysupgrade 2>&1`
	if [ "$?" != "0" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		rm -rf ${CGI_file}
		sync
		retjson 1 "${LANG_005:=ѹʱ⣬ϵȷϵͳȷԣ}"
	fi

	rm -rf ${CGI_file}	
	sync

	panabitinf=`find ${RAMDISK}/sysupgrade -name panabit.inf`
	if [ "${panabitinf}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_004:=Ч:汾Ϣļڣ}"
	fi

	password_file=`find ${RAMDISK}/sysupgrade -name ".htpasswd"`
	if [ "${password_file}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "⣬ļȱʧ"
	fi
	
	rm -rf ${password_file}

	. ${panabitinf}

	# Check OEM
	local_oem=`grep -E "^OEM=" /usr/ramdisk/etc/panabit.inf | cut -d"=" -f2`
	if [ "${OEM}" != "${local_oem}" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	# Check NTM & PA
	have_ntm=`find /usr/ramdisk/sysupgrade/ -maxdepth 3 -name "ntm" -type d 2>/dev/null`

	if [ "${NTM}" = "1" -a "${have_ntm}" = "" ]; then	# ntm use panabit
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	if [ "${NTM}" != "1" -a "${have_ntm}" != "" ]; then	# panabit use ntm
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	# Check TAP & PA
	have_tap=`find /usr/ramdisk/sysupgrade/ -maxdepth 3 -name "tap" -type d 2>/dev/null`

	if [ "${TAP}" = "1" -a "${have_tap}" = "" ]; then	# tap use panabit
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	if [ "${TAP}" != "1" -a "${have_tap}" != "" ]; then	# panabit use tap
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	# Check BSD version
	curbsd=`uname -r | cut -d'-' -f1`
	curbsdmajor=`echo ${curbsd} | cut -d'.' -f1`
	pkgbsdmajor=`echo ${BSDVER} | cut -d'.' -f1`
	ostype=`uname`
	if [ "${curbsdmajor}" != "${pkgbsdmajor}" -a "${ostype}" != "Linux" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi
	if [ "${ostype}" = "Linux" -a ${pkgbsdmajor} -ge 8 ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi
	if [ "${ostype}" = "Linux" ]; then
		# arm Linux
		if [ "`uname -m`" = "aarch64" ]; then
			if [ "${PAVER}" != "arm64" ]; then
				rm -rf ${RAMDISK}/sysupgrade/*
				sync
				retjson 1 "${LANG_006:=ϵͳ汾һ}"
			fi
		#x86 Linux
		else
			if [ "${PAVER}" = "arm64" ]; then
				rm -rf ${RAMDISK}/sysupgrade/*
				sync
				retjson 1 "${LANG_006:=ϵͳ汾һ}"
			fi
		fi
	fi
	
	retjson 0 "OK" "`build_upret`"
}


upgrade_web()
{
	newrel="${BUILDREL}"
	. /etc/PG.conf
	. /usr/ramdisk/etc/panabit.inf
	if [ "${newrel}" != "${BUILDREL}" ]; then 
		rm -rf ${RAMDISK}/sysupgrade/*
		retjson 1 "${LANG_006:=ϵͳ汾һ}"
	fi

	admindir=`find ${RAMDISK}/sysupgrade -name admin | head -1`
	pkgroot=`dirname ${admindir}`

	# do upgrade
	cp -Rf ${pkgroot}/admin/* ${PGPATH}/admin/

	cp -Rf ${PGPATH}/admin/* ${RAMDISK}/admin/
	rm -rf ${RAMDISK}/sysupgrade/
	sync

	touch ${RAMDISK}/tmp/sys_up_success

	WEB_LOGGER "${LANG_007:=ϵͳ}"
	retjson 0 "OK"
}


upgrade_sys()
{
	mkdir -p ${RAMDISK}/tmp
	rm -f ${RAMDISK}/tmp/sys_up_success

	kerneldir=`find ${RAMDISK}/sysupgrade -maxdepth 2 -name kernel`
	admindir=`find ${RAMDISK}/sysupgrade -maxdepth 2 -name admin`
	bindir=`find ${RAMDISK}/sysupgrade -maxdepth 2 -name bin`
	userdir=`find ${RAMDISK}/sysupgrade -maxdepth 3 -name webuser`
	newinfo=`find ${RAMDISK}/sysupgrade -maxdepth 3 -name panabit.inf`

	echo "${LANG_020:=У}..." > ${UPGRDE_NOTICE_FILE}
	if [ "${kerneldir}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=}NO_KERNEL_DIR"
	fi

	if [ "${admindir}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=}NO_ADMIN_DIR"
	fi

	if [ "${bindir}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=}NO_BIN_DIR"
	fi

	if [ "${newinfo}" = "" ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=} NO_PANABIT_INFO"
	fi
	
	# verify the package.
	if [ ! -f ${bindir}/panaos ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=}NO_PANAOS"
	fi

	if [ ! -f ${bindir}/floweye ]; then
		rm -rf ${RAMDISK}/sysupgrade/*
		sync
		retjson 1 "${LANG_009:=}NO_FLOWEYE"
	fi

	pkgroot=`dirname ${kerneldir}`
	binroot=`dirname ${bindir}`
	admroot=`dirname ${admindir}`

	if [ "${pkgroot}" != "${binroot}" ]; then
		rm -rf ${RAMDISK}/sysupgrade
		sync
		retjson 1 "${LANG_009:=}NO_BIN_DIR"
	fi

	if [ "${pkgroot}" != "${admroot}" ]; then
		rm -rf ${RAMDISK}/sysupgrade
		sync
		retjson 1 "${LANG_009:=}NO_ADMIN_DIR"
	fi

	if [ -d ${pkgroot}/usr ]; then
		rm -rf ${RAMDISK}/sysupgrade
		sync
		retjson 1 "${LANG_009:=}NO_USE_DIR"
	fi

	. ${RAMDISK}/etc/panabit.inf
	old_panabit_version="R${ENVER}[${BUILDREL}], release=${JOS_RELEASE}, ${BUILDDATE}"

	. ${newinfo}
	new_panabit_version="R${ENVER}[${BUILDREL}], release=${JOS_RELEASE}, ${BUILDDATE}"

	. /etc/PG.conf
	#ɾ쳣ļ
	[ -d ${PGPATH}/usr ] && rm -Rf ${PGPATH}/usr
	[ -f ${RAMDISK}/admin/cgi-bin/auth ] && rm -rf ${RAMDISK}/admin/cgi-bin/auth
	[ -f ${PGPATH}/admin/cgi-bin/auth ] && rm -rf ${PGPATH}/admin/cgi-bin/auth

	# Save Passowrd
	ufile_num=`ls -l ${USER_DIR} | awk 'END{print NR}'`
	[ ${ufile_num} -gt 1 ] && rm -rf ${userdir}

	# Upgrede web only
	[ "${CGI_method}" = "web" ] && upgrade_web

	# Copy to PGPATH
	echo "${LANG_021:=ļӲ}..." > ${UPGRDE_NOTICE_FILE}
	cp -Rf ${pkgroot}/* ${PGPATH}/
	rm -rf ${RAMDISK}/sysupgrade/
	sync

	# Upgrade core only
	if [ "${CGI_method}" = "core" ]; then
		cp -Rf /usr/ramdisk/admin/* ${PGPATH}/admin/
		cp -Rf /usr/ramdisk/bin/ipe_httpd ${PGPATH}/bin/ipe_httpd
	fi

	# Copy to ramdisk
	# Don't overwrite /usr/ramdisk/bin/dpi.so at this time.
	# backup the new dpi.so
	ren=0
	if [ -f ${PGPATH}/bin/dpi.so ]; then
		mv ${PGPATH}/bin/dpi.so ${PGPATH}/bin/dpi.so.new
		ren=1
	fi

	echo "${LANG_022:=ļڴ}..." > ${UPGRDE_NOTICE_FILE}
	cp -Rf ${PGPATH}/* ${RAMDISK}/
	
	if [ ${ren} -eq 1 ]; then
		mv ${RAMDISK}/bin/dpi.so.new ${PGPATH}/bin/dpi.so
	fi
	sync

	# Restart the monitor and keep upgrading with the new monitor
	${IPECTRL} stop monitor >/dev/null 2>&1
	${IPECTRL} start monitor >/dev/null 2>&1

	mkdir -p "${PGETC}/log/"
	echo "upgrade_system yes" >> ${EVENTFILE}
	echo "${LANG_023:=ļɣʼϵͳ}..." > ${UPGRDE_NOTICE_FILE}

	WEB_LOGGER "${LANG_010:=ϵͳ}" "${old_panabit_version} -> ${new_panabit_version}"
	retjson 0 "OK"
}


query_upsys_stat()
{
	if [ -f ${RAMDISK}/tmp/sys_up_success ]; then
		rm -f ${RAMDISK}/tmp/sys_up_success
		retjson 0 "${LANG_008:=ɹ}"
	else
		msg="${LANG_024:=ѹļ}..."
		if [ -f ${UPGRDE_NOTICE_FILE} ]; then
			msg="`cat ${UPGRDE_NOTICE_FILE}`"
		fi
		retjson 1 "${msg}"
	fi
}


upload_dpi()
{
	# Get a clean directory
	mkdir -p ${RAMDISK}/upgrade
	rm -rf ${RAMDISK}/upgrade/*
	
	# Mv the file to the directory
	mv ${CGI_file} ${RAMDISK}/upgrade/sigdb.tar.gz
	cd ${RAMDISK}/upgrade
	errmsg=`tar zxf sigdb.tar.gz 2>&1`
	
	if [ "$?" != "0" ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_011:=ʱִ}"
	fi
	
	rm -f ${RAMDISK}/upgrade/sigdb.tar.gz
	dpiinf=`find ./ -name dpi.inf`
	if [ "${dpiinf}" = "" ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_012:=Ч:ް汾Ϣ}"
	fi
	
	. ${dpiinf}

	curmach=`cat "${RAMDISK}/etc/panabit.inf" | grep "MACHINE" | cut -d"=" -f2`
	[ "${curmach}" = "" ] && curmach=`uname -m`

	if [ "${MACHINE}" != "${curmach}" ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_016:=ϵͳһ}:${curmach}<->${MACHINE}"
	fi

	pamajor=`cat /usr/ramdisk/etc/panabit.inf | grep BUILDREL | cut -d'"' -f2 | cut -d'(' -f1`
	paminor=`cat /usr/ramdisk/etc/panabit.inf | grep BUILDREL | cut -d'"' -f2 | cut -d')' -f2`
	paversion="${pamajor}${paminor}"
	vermatch=0

	for ver in ${PALIST}; do
		if [ "${ver}" = "${paversion}" ]; then
			vermatch=1
			break
		fi
	done
	
	#֧а汾
	[ ${PALIST} = "" -o ${PALIST} = "ALL" -o ${PALIST} != "all" ] && vermatch=1

	if [ ${vermatch} -eq 0 ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_013:=ⲻݵǰ汾}[${PALIST}->${paversion}]"
	fi

	mv ${dpiinf} ${RAMDISK}/upgrade/dpi.inf
	
	dpiso=`find ./ -name dpi.so`
	if [ "${dpiso}" = "" ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_014:=Ч:ļ}"
	fi

	mv ${dpiso} ${RAMDISK}/upgrade/dpi.so
	
	dictso=`find ./ -name dict.so`
	if [ "${dictso}" = "" ]; then
		cd -
		rm -rf ${RAMDISK}/upgrade/*
		retjson 1 "${LANG_015:=Ч:ֵ}!"
	fi

	mv ${dictso} ${RAMDISK}/upgrade/dict.so

	curver=`${FLOWEYE} dpi stat | grep version | cut -d"=" -f2`
	newver=`echo ${VERSION} | cut -d"_" -f2`

	retjson 0 "OK" "{\"newver\":\"${newver}\",\"curver\":\"${curver}\",\"curjos\":\"1\",\"newjos\":\"1\"}"
}


upgrade_dpi()
{
	mkdir -p ${RAMDISK}/tmp
	rm -f ${RAMDISK}/tmp/dpiupresult

	# Let monitor upgrade the system
	mkdir -p "${PGETC}/log/"
	echo "upgrade_sigdb yes" >> ${EVENTFILE}

	retjson 0 "OK"
}


query_updpi_stat()
{
	resfile=${RAMDISK}/tmp/dpiupresult
	
	if [ -f ${resfile} ]; then
		result=`cat ${resfile}`
		if [ "${result}" != "SUCCESS" ]; then
           if [ "${result}" = "SIGDB_EXPIRED" ]; then
               retjson 2 "${LANG_030:=ʧ}:${LANG_028:=ⷢʱ䲻ϵͳЧ}"
           elif [ "${result}" = "LICENSE_EXPIRED" ]; then
               retjson 2 "${LANG_030:=ʧ}:${LANG_029:=ϵͳȨʱʧЧ}"
           else
               retjson 2 "${result}"
            fi
		else
			rm -f  ${resfile}
			rm -rf ${RAMDISK}/upgrade/*
			sync
			retjson 0 "${LANG_008:=ɹ}"
		fi
	else
		retjson 1 "RUN"
	fi
}


upload_license()
{
	mkdir -p ${RAMDISK}/upgrade
	rm -rf ${RAMDISK}/upgrade/*

	mv ${CGI_file} ${RAMDISK}/upgrade/license.dat
	license=`cat ${RAMDISK}/upgrade/license.dat`
	errmsg=`${FLOWEYE} key install ${license}`

	[ $? -ne 0 ] && retjson 1 "${LANG_017:=ʧܣͬȷȨЧ!}"

	if [ -f ${PGETC}/license.dat ]; then
		mv ${PGETC}/license.dat ${PGETC}/license.dat.old
	fi

	mv ${RAMDISK}/upgrade/license.dat ${PGETC}/license.dat
	sync

	WEB_LOGGER "${LANG_018:=Ȩ}"
	retjson 0 "${LANG_019:=ɹ}"
}


export_license()
{
	license=`${FLOWEYE} key info  | cut -d" " -f2 | cut -d"=" -f2`
	[ "${license}" = "" ] && retjson 1 "${LANG_025:=Ȩļ}"

	file_name="${license}.pac"
	file_path="${WEB_DOWNLOAD}/${file_name}"

	mkdir -p ${WEB_DOWNLOAD}
	cp ${PGETC}/license.dat ${file_path}

	WEB_LOGGER "Ȩļ"
	retjson 0 "OK" "\"${file_name}\""
}


check_upgrade_env()
{
	mkdir -p /tmp
	rm -rf ${PGETC}/log/*

	[ -f ${RAMDISK}/tmp/sys_up_status  ] && rm -rf ${RAMDISK}/tmp/sys_up_status
	[ -f ${RAMDISK}/tmp/sys_up_success ] && rm -rf ${RAMDISK}/tmp/sys_up_success
	[ -f ${RAMDISK}/admin/cgi-bin/auth ] && rm -rf ${RAMDISK}/admin/cgi-bin/auth
	[ -f ${PGPATH}/admin/cgi-bin/auth ] && rm -rf ${PGPATH}/admin/cgi-bin/auth

	${IPECTRL} stop monitor >/dev/null 2>&1
	${IPECTRL} start monitor >/dev/null 2>&1

	find ${RAMDISK} -name "*.apx" | xargs rm -rf
	find ${RAMDISK} -name "*.fetch" | xargs rm -rf
	find ${RAMDISK} -name "*.core" | xargs rm -rf
	find ${RAMDISK} -name "core" | xargs rm -rf

	find ${PGPATH} -name "*.apx" | xargs rm -rf
	find ${PGPATH} -name "*.fetch" | xargs rm -rf
	find ${PGPATH} -name "*.core" | xargs rm -rf
	find ${PGPATH} -name "core" | xargs rm -rf

	upgrade_cancel
}


upgrade_cancel()
{
	rm -rf ${RAMDISK}/sysupgrade/
	rm -rf ${RAMDISK}/upgrade/
	rm -rf ${RAMDISK}/appupgrade/

	sync
	retjson 0 "OK"
}


case "${CGI_action}" in

	"upload_syspkt")
		action_check
		upable_check
		upload_syspkt
		;;

	"upgrade_sys_yes")
		action_check
		upgrade_sys
		;;

	"query_upsys_stat")
		query_upsys_stat
		;;

	"upload_dpi")
		action_check
		upable_check
		upload_dpi
		;;

	"upgrade_dpi")
		action_check
		upgrade_dpi
		;;
	
	"query_updpi_stat")
		query_updpi_stat
		;;

	"upload_license")
		action_check
		upload_license
		;;

	"export_license")
		action_check
		export_license
		;;

	"check_upgrade_env")
		action_check
		check_upgrade_env
		;;

	"upgrade_sys_cancel")
		action_check
		upgrade_cancel
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
