#!/bin/sh
_checkpid(){
	pid=`echo $$`
	pname=`echo $0`
	mypidfile=/tmp/myvpnd.pid
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		if grep $pname /proc/`cat $mypidfile`/cmdline > /dev/null 2>&1 ;then
			echo "The process $pname is already exists ! pid:$expid."
			exit 0
		fi
	fi
	echo $pid > $mypidfile
}

_init(){
	touch /etc/config/myvpn
	uci get myvpn.@myvpn[0] > /dev/null 2>&1
	if [ "$?" != "0" ];then
		uci add myvpn myvpn
		uci set myvpn.@myvpn[0].version=`opkg list|grep luci-app-myvpnconfig|awk '{print $3}'`
		uci set myvpn.@myvpn[0].server='node.vps000.org'
		uci commit myvpn
	fi
	echo "127.0.0.1 localhost">/etc/hosts
	echo "nameserver 180.76.76.76" > /etc/resolv.conf
	echo "nameserver 223.5.5.5" >> /etc/resolv.conf
	/etc/init.d/dnsmasq stop > /dev/null 2>&1
	/etc/init.d/dnsmasq start > /dev/null 2>&1
	apiserver=`uci get myvpn.@myvpn[0].server|sed s/[[:space:]]//g`
}

_update(){
	newversion=`curl -s -k --connect-timeout 3 https://$apiserver/version|awk '{print $1}'`
	localversion=`uci get myvpn.@myvpn[0].version|sed s/[[:space:]]//g`
	if [ "$newversion" != "$localversion" ];then
		curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/ipk/luci-app-myvpnconfig_'$newversion'_all.ipk' > /tmp/'luci-app-myvpnconfig_'$newversion'_all.ipk'
		localipkmd5=`md5sum /tmp/'luci-app-myvpnconfig_'$newversion'_all.ipk'|awk '{print $1}'`
		newipkmd5=`curl -s -k --connect-timeout 3 https://$apiserver/version|awk '{print $2}'`
		if [ "$localipkmd5" = "$newipkmd5" ];then
			opkg install /tmp/'luci-app-myvpnconfig_'$newversion'_all.ipk'
			if [ "$?" = "0" ];then
				uci set myvpn.@myvpn[0].version=$newversion
				uci commit myvpn
				/etc/init.d/myvpn restart
			fi
		fi
	fi
}

_checkfile(){
	echo 0 > /tmp/myvpn_checkfile
	curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update/myvpn_file_md5' > /tmp/myvpn_file_md5
	local_myvpn_file_md5=`md5sum /tmp/myvpn_file_md5|awk '{print $1}'`
	ol_myvpn_file_md5=`curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update/md5'|awk '{print $1}'`
	if [ "$local_myvpn_file_md5" = "$ol_myvpn_file_md5" ];then
		cat /tmp/myvpn_file_md5 | while read file;do
			file_name=`echo $file|awk '{print $2}'`
			ol_md5=`echo $file|awk '{print $1}'`
			local_md5=`md5sum $file_name|awk '{print $1}'`
			if [ "$local_md5" != "$ol_md5" ];then
				rm /tmp/myvpn_temp_file
				curl -s -k --connect-timeout 3 'https://'$apiserver'/downloads/update'$file_name > /tmp/myvpn_temp_file
				dl_md5=`md5sum /tmp/myvpn_temp_file|awk '{print $1}'`
				if [ "$dl_md5" = "$ol_md5" ];then
					\cp -rf /tmp/myvpn_temp_file $file_name
					rm -rf /tmp/myvpn_temp_file
					echo 1 > /tmp/myvpn_checkfile
				fi
			fi
		done
		check=`cat /tmp/myvpn_checkfile`
		if [ "$check" = "1" ];then
			/etc/init.d/myvpn restart
		fi
	fi
}

_checkvpn(){
	mypidfile=/tmp/myvpn.pid
	if test -f "$mypidfile";then
		expid=`cat $mypidfile`
		if grep myvpn /proc/$expid/cmdline > /dev/null 2>&1 ;then
			echo "The process $pname is already exists ! pid:$expid."
		else
			echo run myvpn...
			/usr/sbin/myvpn start > /dev/null 2>&1 &
		fi		
	else
		echo run myvpn...
		/usr/sbin/myvpn start > /dev/null 2>&1 &
	fi
}


_start(){
	_checkpid
	_init
	while true;do
		_update
		_checkfile
		_checkvpn
		sleep 10
	done
}

_start


