#!/bin/sh
[ "$1" = --First ] && A=Y || A=N
LOCKU=/tmp/lock/bypass-update.lock
[ -f $LOCKU ] && exit 1
touch $LOCKU
DIR=/usr/share/bypass
TMP=/etc/bypass
TMP2=/tmp/etc/bypass
LOG=/tmp/log/bypass.log
chnroute_url=$(uci -q get bypass.@global_rules[0].chnroute_url)
chnroute6_url=$(uci -q get bypass.@global_rules[0].chnroute6_url)
domains_url=$(uci -q get bypass.@global_rules[0].domains_url)
gfwlist_url="$(uci -q get bypass.@global_rules[0].gfwlist_url)"
ad_url="$(uci -q get bypass.@global_rules[0].ad_url)"
google_status=$(curl -I -4 -m 3 -o /dev/null -s -w %{http_code} http://www.google.com/generate_204)
[ "$google_status" -ne "204" ] && proxy="https://ghproxy.com/"
log(){
	echo "$(date +'%Y-%m-%d %H:%M:%S') $*" >> $LOG
}

echo '------------Update start------------' >> $LOG
[ ! -d $TMP ] && mkdir -p $TMP 2>/dev/null
[ ! -d $TMP2 ] && mkdir -p $TMP2 2>/dev/null

        echo -e "\e[1;32mDownloading "$proxy${chnroute_url}"\e[0m"
	curl --connect-timeout 5 -m 60 --ipv4 -kfSLo "/tmp/china.txt" ""$proxy"${chnroute_url}" 
	[[ -f /tmp/china.txt && $? == 0 ]] && {
	if ! cmp -s /tmp/china.txt $TMP/china.txt;then
			cp -f /tmp/china.txt $TMP/china.txt
			mv /tmp/china.txt $TMP2/china.txt
			log "By Update  : Updated China IPv4 List Total Number of Items :`cat /etc/bypass/china.txt | wc -l` "
			[ $A = N ] && ipset list china_v4 >/dev/null 2>&1 && $DIR/chinaipset

	else
			log "By Update  : China IPv4 List Total Number of Items  :` cat /etc/bypass/china.txt | wc -l` "
	fi
	}

	echo -e "\e[1;32mDownloading "$proxy${chnroute6_url}"\e[0m"
	curl --connect-timeout 5 -m 60 --ipv4 -kfSLo "/tmp/china_v6.txt" ""$proxy"${chnroute6_url}" 
	[[ -f /tmp/china_v6.txt && $? == 0 ]] && {
	if ! cmp -s /tmp/china_v6.txt $TMP/china_v6.txt;then
			cp -f /tmp/china_v6.txt $TMP/china_v6.txt 
			mv /tmp/china_v6.txt $TMP2/china_v6.txt 
			log "By Update  : Updated China IPv6 List Total Number of Items :` cat /etc/bypass/china_v6.txt | wc -l` "
		[ $A = N ] && ipset list china_v6 >/dev/null 2>&1 && $DIR/chinaipset v6
	else
			log "By Update  : China IPv6 List Total Number of Items :`cat /etc/bypass/china_v6.txt | wc -l)` "
	fi
	}

	echo -e "\e[1;32mDownloading "$proxy${gfwlist_url}"\e[0m"
	curl --connect-timeout 5 -m 60 --ipv4 -kfSLo "/tmp/gfw.b64" ""$proxy"${gfwlist_url}" 
	[[ -f /tmp/gfw.b64 && $? == 0 ]] && {
	   $DIR/by-gfw  >/dev/null 2>&1
	   if ! cmp -s /tmp/gfwnew.txt  $TMP/gfw.list ;then
			cp -f /tmp/gfwnew.txt $TMP/gfw.list   >/dev/null 2>&1
			mv /tmp/gfwnew.txt $TMP2/gfw.list 
			log "By Update  : Updated GFW List Total Number of Items : `cat /etc/bypass/gfw.list | wc -l` "
	   else
			log "By Update  : GFW List Total Number of Items : `cat /etc/bypass/gfw.list | wc -l` "
	   fi
	}

        echo -e "\e[1;32mDownloading "$proxy${domains_url}"\e[0m"
	curl --connect-timeout 5 -m 60 --ipv4 -kfSLo "/tmp/domains_cn.txt" ""$proxy"${domains_url}"
		
	[[ -f /tmp/domains_cn.txt && $? == 0 ]] && {
	if ! cmp -s /tmp/domains_cn.txt $TMP/domains_cn.txt;then
			cp -f /tmp/domains_cn.txt $TMP/domains_cn.txt
			mv /tmp/domains_cn.txt $TMP2/domains_cn.txt
			log "By Update  : Updated China Domains List Total Number of Items :` cat /etc/bypass/domains_cn.txt | wc -l` "
	else
			log "By Update  : China Domains Total Number of Items :`cat /etc/bypass/domains_cn.txt | wc -l)` "
	fi
	}

        echo -e "\e[1;32mDownloading "$proxy${ad_url}"\e[0m"
	curl --connect-timeout 5 -m 60 --ipv4 -kfSLo "/tmp/ad_list.txt" "$proxy${ad_url}"
	
	[[ -f /tmp/ad_list.txt && $? == 0 ]] && {
	if ! cmp -s /tmp/ad_list.txt $TMP/ad_list.txt;then
			cp -f /tmp/ad_list.txt $TMP/ad_list.txt
			mv /tmp/ad_list.txt $TMP2/ad_list.txt
			log "By Update  : anti-AD Update List Total Number of Items :` cat /etc/bypass/ad_list.txt | wc -l` "
	else
			log "By Update  : anti-AD List Total Number of Items :`cat /etc/bypass/ad_list.txt | wc -l)` "
	fi
	}
log  "By Update  : Check or update IPV4/IPV6/GFW/AD list complete!  "
echo '------------Update end------------' >> $LOG
rm -f $LOCKU /tmp/china*.txt /tmp/gfwnew.txt /tmp/gfw.b64 /tmp/ad_list.txt /tmp/domains_cn.txt

[ $A = Y ] && /etc/init.d/bypass start &
