#!/bin/sh

if [ -f "/usr/pabin/pacu" ]; then
	CURL="/usr/pabin/pacu"
else
	CURL="`which curl | tail -1`"
fi


get_upgradepkg()
{
	DEVTYPES=`floweye sac list listdevtype=1 | sed 's/"//g'`
	URI='https://download.panabit.com:8443/appbags/PanabitAP/'
	WEBDIR='/usr/ramdisk/tmp/sac/new'
	mkdir -p ${WEBDIR}

	echo "${DEVTYPES}" | while read oem_type type;
	do
		#check new version
		fname="PanabitAP_1_${type}.txt"
		fetchurl="${URI}${fname}"
		
		${CURL} --connect-timeout 60 -m 120 -s -k "${fetchurl}" -o /tmp/${fname}

		[ -f "/tmp/${fname}" ] && mv /tmp/${fname} ${WEBDIR}
	done
}


# Ӻ ִһΣ
# 3 ִһ

get_upgradepkg

sleeptime=$((3600*24*3))
lastworktime=$(( (`date +%s`) - (${sleeptime} - 120) ))

while true 
do
	sleep 1

	cursecs=`date +%s`
	elapse=$((${cursecs} - ${lastworktime}))

	if [ ${elapse} -ge ${sleeptime} ]; then
		lastworktime="${cursecs}"
		get_upgradepkg
	fi
done
