#!/bin/sh 
# copyrights 
# (1)epg dowload Script VER 0.10for openwrt  by zjhzzyf


. /etc/functions.sh


epg_down()
{
	bsuc=1
	echo "File Path:"$path
	filename="epg.xml"
	echo "Setup FileName:"$filename
	sfullfilename=`echo $path$filename`
	echo "sFullFileName:"$sfullfilename	
		
	index=0
	echo "Try Times Max:"$trytimes
	while [ "$index" -lt "$trytimes" ]
	do
		echo "Try Times:"$index
			if wget -qO- -t3 $url > $sfullfilename;then
			break	
		fi
		index=`expr $index + 1`
	done
	if [ "$index" -eq "$trytimes"  ]; then
		ErrMsg=`date`
		ErrMsg=$ErrMsg"  下载配置文件失败"
		bsuc=0
		return
	fi
	echo "down $filename  completed"
mfullfilename=`echo ${path}store/${filename}.md5`
	echo "MD5 File Name:"$mfullfilename
	md5old=`cat $mfullfilename`
	md5old=`echo $md5old|awk '{print $1}'|tr "[a-z]" "[A-Z]"`
	echo "Old File MD5:"$md5old
	md5new=`/usr/bin/md5sum $sfullfilename`
	md5new=`echo $md5new|awk '{print $1}'|tr "[a-z]" "[A-Z]"`
	echo "New File MD5:"$md5new
	if [ "$md5old" = "$md5new" ]; then
		ErrMsg=`date`
                ErrMsg=$ErrMsg"  数据不需要更新"
		bsuc=1
		return
	else
 
		counttimes=0
		suctimes=0
	counttimes=`expr $counttimes + 1`
  url=$(grep -m 1 "URL" $sfullfilename |awk -F "<URL>" '{print $2}'|awk -F "</URL>" '{print $1}')
  echo "Down Data URL:" $url

filename=`echo ${url##*/}`
				echo "Down File Name:"$filename

				echo "====="
echo $url
 replaces=`echo ${url%/*}`

replaces=`echo ${replaces#*//}`

				echo replaces: $replaces
				echo -e `sed -e "s#$replaces#$replaced#g" $sfullfilename` > `echo $sfullfilename`
				echo "====="
								
				dfullfilename=`echo $path$filename`
				echo "Down Data Save File Name:"$dfullfilename
			
				index=0
				while [ "$index" -lt "$trytimes" ]
				do
				        echo "Try Times:"$index
				        if wget -t3 -T120 -qO- $url > $dfullfilename;then
						suctimes=`expr $suctimes + 1`

					        break
				        fi
					index=`expr $index + 1`
					
				done

				if [ "$index" -eq "$trytimes"  ]; then
			               bsuc=0
			               ErrMsg=`date`
			               ErrMsg=$ErrMsg"  下载"$url"失败"

			        fi


		if [ "$counttimes" -eq  "$suctimes"  ]; then
			echo $md5new > `echo $mfullfilename`

mount -o bind /tmp/epg/ /www/atmosphere
 rm -rf ${path}/epg/*

cp ${path}$filename ${path}/epg
cp ${path}epg.xml ${path}/epg
 rm -rf ${path}$filename
 rm -rf ${path}epg.xml
			ErrMsg=`date`
       ErrMsg=$ErrMsg"  下载EPG数据成功"
			echo $ErrMsg
		exit 0
		else
			ErrMsg=`date`
      ErrMsg= $ErrMsg "  未知原因失败"
		fi
	fi
}


epg_start() {
	
config_get enable $1 enable
config_get interval $1 interval
[ "$enable" = "" ]&&enable=0

#make  scheduler

if  [ "$enable" == "1"  -a  "${interval}" != "0" ]; then 
uci delete cron.`uci show cron|grep "/usr/bin/epg"|cut -d "." -f2`
mkdir -p /tmp/epg/
mkdir -p /tmp/epg/store
mkdir -p /tmp/epg/epg
mkdir -p /www/atmosphere

uci add cron task
uci set cron.@task[-1].enabled=1
uci set cron.@task[-1].task_Everyday=1
uci set cron.@task[-1].task_time=custom
uci set cron.@task[-1].task_minute=${interval}
uci set cron.@task[-1].task_name="epg_scheduler"
uci set cron.@task[-1].task_task="usr/bin/epg  scheduler"
uci commit cron
/etc/init.d/cron restart
		else
uci delete cron.`uci show cron|grep "/usr/bin/epg"|cut -d "." -f2`
uci commit cron
/etc/init.d/cron restart
fi
}


con_get_addr(){

 config_get addr_enable $1 addr_enable
 config_get address $1 address
 
[  "$addr_enable" == "1"  ]&&{
url=$address
echo $url
ErrMsg=`date`
ErrMsg=$ErrMsg"  开始从"$url"下载"
echo $ErrMsg >> `echo /tmp/epglog.txt`
rm `echo $path"*"`
epg_down
echo $ErrMsg >> `echo /tmp/epglog.txt`
echo $bsuc
echo "******************************************************************" >> `echo /tmp/epglog.txt`
}
}



epg_scheduler(){
	
path="/tmp/epg/"
trytimes=3
bsuc=1
ErrMsg=""
lan_ipaddr=$(uci get network.lan.ipaddr)
replaced="${lan_ipaddr}/atmosphere/epg"

	
}
	
		
config_load epg 
case "$1" in
	start)
 config_foreach  epg_start epg_set
 	 ;;
	stop)
	  echo "stop test "
	;;
 scheduler)
 epg_scheduler
 config_foreach  con_get_addr downaddress
;;
esac

