#!/bin/sh /etc/rc.common

device=$(uci get xunlei.config.device)
file=$(uci get xunlei.config.file)
mountpoint="`mount | grep "$device" | awk '{print $3;exit}'`"
DIR=$file/xunlei
$DIR/portal -s
sleep 1
mkdir -p ${DIR}/cfg
cd ${DIR}
FILES=$(uci get xunlei.config.xware)
ver=$(uci get xunlei.config.ver)
url=$(uci get xunlei.config.url)
zversion=$(uci get xunlei.config.zversion)
VERSION=$(wget --no-check-certificate -O- ${url}/latest)

{
	if [ "$zversion" -eq 1 ];then
        wget --no-check-certificate ${url}/${ver}/${FILES}
	else
        wget --no-check-certificate ${url}/${VERSION}/${FILES}
	fi
}

{
xl_configfile="/tmp/etc/xlver"
[ ! -f "$xl_configfile" ] && touch "$xl_configfile"
	
cat > "$xl_configfile" << EOF
$VERSION
EOF
}

rm -rf $DIR/portal && rm -rf $DIR/lib && tar zxvf ${FILES}
chmod +x $DIR/* && chmod +x $DIR/lib/*
rm -rf ${FILES}
rm -rf $file/xunlei/cfg/thunder_mounts.cfg>> /dev/null 2>&1
uci set xunlei.config.up=0>> /dev/null 2>&1
uci commit 2>&1 > /dev/null
vod=$(uci get xunlei.config.vod)   
[ "$vod" -eq 1 ] && rm -f "$file"/xunlei/vod_httpserver
[ "$vod" -eq 1 ] && rm -f "$file"/xunlei/lib/vod_httpserver
sleep 3 && killall wget
