#!/bin/sh
. /usr/syscfg/wan.utils.sh
. /usr/syscfg/wan.comm.param.sh
. /usr/syscfg/firewall.param.sh

TR069CONF="/tmp/tr069.config"
TR069INFORMED="/tmp/tr069_informed"
CAFilePath="/tmp/server.crt"
need_restart="0"
need_start="1"
oURL=""
oUsername=""
oPassword=""
oPeriodicInformEnable=""
oPeriodicInformInterval=""
oPeriodicInformTime=""
oParameterKey=""
oConnectionRequestURL=""
oConnectionRequestUsername=""
oConnectionRequestPassword=""
oUpgradesManaged=""
oKickURL=""
oDownloadProgressURL=""
oManufacturer=""
oManufacturerOUI=""
oProductClass=""
oSerialNumber=""
myip=""
mygw=""
myif=""
mymask=""
conn_folder=""
tr069_resolv="/tmp/tr069_resolv.conf"

TR069Enable="InternetGatewayDevice.ManagementServer.EnableCWMP"
RequestPort="InternetGatewayDevice.ManagementServer.X_TRI_RequestPort"
URL="InternetGatewayDevice.ManagementServer.URL"
Username="InternetGatewayDevice.ManagementServer.Username"
Password="InternetGatewayDevice.ManagementServer.Password"
PeriodicInformEnable="InternetGatewayDevice.ManagementServer.PeriodicInformEnable"
PeriodicInformInterval="InternetGatewayDevice.ManagementServer.PeriodicInformInterval"
PeriodicInformTime="InternetGatewayDevice.ManagementServer.PeriodicInformTime"
ParameterKey="InternetGatewayDevice.ManagementServer.ParameterKey"
ConnectionRequestEnable="InternetGatewayDevice.ManagementServer.ConnectionRequestEnable"
ConnectionRequestURL="InternetGatewayDevice.ManagementServer.ConnectionRequestURL"
ConnectionRequestUsername="InternetGatewayDevice.ManagementServer.ConnectionRequestUsername"
ConnectionRequestPassword="InternetGatewayDevice.ManagementServer.ConnectionRequestPassword"
UpgradesManaged="InternetGatewayDevice.ManagementServer.UpgradesManaged"
KickURL="InternetGatewayDevice.ManagementServer.KickURL"
DownloadProgressURL="InternetGatewayDevice.ManagementServer.DownloadProgressURL"
CAEnable="InternetGatewayDevice.ManagementServer.X_TRI_CAEnable"
CAFile="InternetGatewayDevice.ManagementServer.X_TRI_CAFile"
Manufacturer="InternetGatewayDevice.DeviceInfo.Manufacturer"
ManufacturerOUI="InternetGatewayDevice.DeviceInfo.ManufacturerOUI"
ProductClass="InternetGatewayDevice.DeviceInfo.ProductClass"
SerialNumber="InternetGatewayDevice.DeviceInfo.SerialNumber"

stop_tr069_agent()
{
	vRequestPort=`cfgcmd get $RequestPort`

	echo "myif=$myif"
	if [ "$myif" == "" ]; then
		return
	fi

	iptables -t filter -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT
	iptables -t nat -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT
	echo "iptables -t filter -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT"
	echo "iptables -t nat -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT"

	killall -9 evcpe

	if [ -f $TR069INFORMED ]; then
		vInformed=`cat $TR069INFORMED`
		if [ "$vInformed" == "1" ]; then
			cfgcmd set InternetGatewayDevice.ManagementServer.X_TRI_TR069ValueChange 1
		fi
	fi
}

create_correct_resolv_base()
{
	file=$1

	if [ -s "$file" ];then
        i=1
        line=`cat $file | wc -l`
        while [ "${i}" -le "$line" ]
        do
                dns_ip=`sed -n ${i}p $file`
                i=`expr $i + 1`
                echo "nameserver $dns_ip" >> $tr069_resolv
        done

	fi	
}

create_correct_resolv_conf()
{
	
	echo -n > $tr069_resolv

	#MUST set bind-to before add nameserver,otherwise it dosn't work
	echo "options bind-to:${myip}" >> $tr069_resolv

	# 设置randomize-case为0, 关闭DNS-0x20 encoding, 增强对dns server的兼容性
	echo "options randomize-case:0" >> $tr069_resolv

	create_correct_resolv_base ${conn_folder}/${SERVER_ASSIGNED_DNS}
	create_correct_resolv_base ${conn_folder}/${USER_DEFINED_DNS}

	if [ ! -s "$tr069_resolv" ];then
		cp /etc/resolv.conf $tr069_resolv
	fi
}

create_tr069_agent()
{
	#cfgcmd sset InternetGatewayDevice.ManagementServer.X_CT-COM_ManualInformState 0
	#cfgcmd sset InternetGatewayDevice.ManagementServer.X_CT-COM_InformState 0

	echo "myif=$myif"
	echo "mygw=$mygw"
	echo "myip=$myip"
	if [ "$myif" == "" -o "$mygw" == "" -o "$myip" == "" ]; then
		vURL=`cfgcmd get $URL`
		if [ "$oURL" != "$vURL" ]; then
			if [ "$oURL" != "" ]; then
				echo "URL changed"
				nvram set Tr069strap=BS #恢复出厂设置时执行BS(0)
				nvram commit
			fi
		fi
		return
	fi

	mymask=`ifconfig $myif | grep Mask | awk '{print $4}' | cut -d ":" -f 2`
	echo "mymask=$mymask"
	vTR069Enable=`cfgcmd get $TR069Enable`
	vRequestPort=`cfgcmd get $RequestPort`
	vRequestPath=`cfgcmd get $RequestPath`

	cfgcmd sset $ConnectionRequestURL "http://$myip:$vRequestPort$vRequestPath"
	echo "cfgcmd sset $ConnectionRequestURL http://$myip:$vRequestPort$vRequestPath"

	acsip=`cfgcmd get $URL | cut -d : -f 2 | cut -d / -f 3`
	echo "acsip=$acsip"
	if [ ! -n $acsip ]; then
		return
	fi
	#DNS解析
	create_correct_resolv_conf
	#建立tr069wan访问规则
	vDelReqPort=`cat /tmp/.reqport`
	if [ "$vDelReqPort" != "" ]; then
		iptables -t filter -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vDelReqPort -j ACCEPT
		iptables -t nat -D ${SERVICES_CHAIN} -i $myif -p tcp --dport $vDelReqPort -j ACCEPT
	fi

	iptables -t filter -A ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT
	iptables -t nat -I ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT
	echo "iptables -t filter -A ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT"
	echo "iptables -t nat -A ${SERVICES_CHAIN} -i $myif -p tcp --dport $vRequestPort -j ACCEPT"

	echo $vRequestPort > /tmp/.reqport

	vURL=""
	if [ "$vTR069Enable" == "1" ]; then
		vURL=`cfgcmd get $URL`
	else
		killall -9 evcpe
		return
	fi
	vUsername=`cfgcmd get $Username`
	vPassword=`cfgcmd get $Password`
	vPeriodicInformEnable=`cfgcmd get $PeriodicInformEnable`
	vPeriodicInformInterval=`cfgcmd get $PeriodicInformInterval`
	vPeriodicInformTime=`cfgcmd get $PeriodicInformTime`
	vParameterKey=`cfgcmd get $ParameterKey`
	vConnectionRequestEnable=`cfgcmd get $ConnectionRequestEnable`
	vConnectionRequestURL=`cfgcmd get $ConnectionRequestURL`
	vConnectionRequestUsername=`cfgcmd get $ConnectionRequestUsername`
	vConnectionRequestPassword=`cfgcmd get $ConnectionRequestPassword`
	vUpgradesManaged=`cfgcmd get $UpgradesManaged`
	vKickURL=`cfgcmd get $KickURL`
	vDownloadProgressURL=`cfgcmd get $DownloadProgressURL`
	vManufacturer=`cfgcmd get $Manufacturer`
	vManufacturerOUI=`cfgcmd get $ManufacturerOUI`
	vProductClass=`cfgcmd get $ProductClass`
	vSerialNumber=`cfgcmd get $SerialNumber`
	vCAEnable=`cfgcmd get $CAEnable`
	vCAFile="`cfgcmd get $CAFile`"

	if [ "$vURL" == "" ]; then
		return
	fi

	if [ "$vPeriodicInformEnable" == "" ]; then
		return
	fi

	if [ "$vPeriodicInformInterval" == "" ]; then
		return
	fi

	echo "$URL=$vURL" > $TR069CONF
	echo "$Username=$vUsername" >> $TR069CONF
	echo "$Password=$vPassword" >> $TR069CONF
	echo "$PeriodicInformEnable=$vPeriodicInformEnable" >> $TR069CONF
	echo "$PeriodicInformInterval=$vPeriodicInformInterval" >> $TR069CONF
	echo "$PeriodicInformTime=$vPeriodicInformTime" >> $TR069CONF
	echo "$ParameterKey=$vParameterKey" >> $TR069CONF
	if [ "$vConnectionRequestEnable" == "1" ]; then
		echo "$ConnectionRequestURL=$vConnectionRequestURL" >> $TR069CONF
	fi
	echo "$ConnectionRequestUsername=$vConnectionRequestUsername" >> $TR069CONF
	echo "$ConnectionRequestPassword=$vConnectionRequestPassword" >> $TR069CONF
	echo "$UpgradesManaged=$vUpgradesManaged" >> $TR069CONF
	echo "$KickURL=$vKickURL" >> $TR069CONF
	echo "$DownloadProgressURL=$vDownloadProgressURL" >> $TR069CONF
	echo "$Manufacturer=$vManufacturer" >> $TR069CONF
	echo "$ManufacturerOUI=$vManufacturerOUI" >> $TR069CONF
	echo "$ProductClass=$vProductClass" >> $TR069CONF
	echo "$SerialNumber=$vSerialNumber" >> $TR069CONF
	echo "myif=$myif" >> $TR069CONF
	echo "mygw=$mygw" >> $TR069CONF
	echo "myip=$myip" >> $TR069CONF
	echo "mymask=$mymask" >> $TR069CONF
	
	rm -f $CAFilePath
	if [ "$vCAEnable" == 1 -a -n "$vCAFile" ]; then
		echo "$vCAFile" > $CAFilePath
	fi
	
	cat $TR069CONF

	judge_launch
}

judge_launch()
{
	echo "judge_launch"
	need_restart="0"
	if [ "$oURL" != "$vURL" ]; then
		if [ "$oURL" != "" ]; then
			echo "URL changed"
			nvram get Tr069strap
			nvram set Tr069strap=BS
			nvram commit
			nvram get Tr069strap
		fi
		need_restart="1"
	fi
	if [ "$oUsername" != "$vUsername" ]; then
		echo "Username changed"
		#need_restart="1"
	fi
	if [ "$oPassword" != "$vPassword" ]; then
		echo "Password changed"
		#need_restart="1"
	fi
	if [ "$oPeriodicInformEnable" != "$vPeriodicInformEnable" ]; then
		echo "PeriodicInformEnable changed"
		#need_restart="1"
	fi
	if [ "$oPeriodicInformInterval" != "$vPeriodicInformInterval" ]; then
		echo "PeriodicInformInterval changed"
		#need_restart="1"
	fi
	if [ "$vConnectionRequestEnable" == "1" ]; then
		if [ "$oConnectionRequestURL" != "$vConnectionRequestURL" ]; then
			echo "ConnectionRequestURL changed"
			need_restart="1"
		fi
		if [ "$oConnectionRequestUsername" != "$vConnectionRequestUsername" ]; then
			echo "ConnectionRequestUsername changed"
			#need_restart="1"
		fi
		if [ "$oConnectionRequestPassword" != "$vConnectionRequestPassword" ]; then
			echo "ConnectionRequestPassword changed"
			#need_restart="1"
		fi
	fi

	need_start="1"
	if [ "$vURL" == "" ]; then
		echo "URL null"
		need_start="0"
	fi
	if [ "$vPeriodicInformEnable" == "" ]; then
		echo "PeriodicInformEnable null"
		need_start="0"
	fi
	if [ "$vConnectionRequestEnable" == "" ]; then
		echo "vConnectionRequestEnable null"
		need_start="0"
	fi

	if [ "$need_start" == "0" ]; then
		echo  "not_need_start"
		echo  "not_need_start" > /tmp/tr069_status
		return
	fi
	if [ "$need_restart" == "1" ]; then
		echo  "need_restart"
		echo  "need_restart" > /tmp/tr069_status
		killall -9 evcpe
		evcpe start -f $TR069CONF &
	else
		echo  "need_start"
		echo  "need_start" > /tmp/tr069_status
		result=`pidof evcpe`
		echo  "result=$result"
		if [ "$result" != "" ]; then
			echo  "evcpe alive"
			cfgcmd manully_inform UP
			return
		fi
		echo  "evcpe start -f $TR069CONF"
		evcpe start -f $TR069CONF  &
	fi
	
}

if [ "$0" == "tr069.sh" -o "$0" == "./tr069.sh" -o "$0" == "/usr/syscfg/tr069.sh" ]; then
	if [ -f $TR069CONF ]; then
		oURL=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.URL" | cut -d "=" -f 2`
		oUsername=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.Username" | cut -d "=" -f 2`
		oPassword=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.Password" | cut -d "=" -f 2`
		oPeriodicInformEnable=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.PeriodicInformEnable" | cut -d "=" -f 2`
		oPeriodicInformInterval=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.PeriodicInformInterval" | cut -d "=" -f 2`
		oConnectionRequestURL=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.ConnectionRequestURL" | cut -d "=" -f 2`
		oConnectionRequestUsername=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.ConnectionRequestUsername" | cut -d "=" -f 2`
		oConnectionRequestPassword=`cat $TR069CONF | grep "InternetGatewayDevice.ManagementServer.ConnectionRequestPassword" | cut -d "=" -f 2`
		echo "oURL=$oURL"
		echo "oUsername=$oUsername"
		echo "oPassword=$oPassword"
		echo "oPeriodicInformEnable=$oPeriodicInformEnable"
		echo "oPeriodicInformInterval=$oPeriodicInformInterval"
		echo "oConnectionRequestURL=$oConnectionRequestURL"
		echo "oConnectionRequestUsername=$oConnectionRequestUsername"
		echo "oConnectionRequestPassword=$oConnectionRequestPassword"
	fi

	if [ "$2" != "" ]; then
		myif="$2"
	fi
	if [ "$3" != "" ]; then
		mygw="$3"
	fi
	if [ "$4" != "" ]; then
		myip="$4"
	fi
	if [ "$5" != "" ]; then
		conn_folder="$5"
	fi
	if [ "$conn_folder" = "" ]; then
		TR69_WAN_PATH=`cfgcmd get_wan`
		conn_folder=`gen_wanconn_folder ${TR69_WAN_PATH}`	
		conn_folder="/tmp/${conn_folder}/"
	fi
	if [ "$myif" == "" ]; then
		myif=`cfgcmd get InternetGatewayDevice.ManagementServer.X_TRI_TR069WANIFNAME`
	fi
	if [ "$mygw" == "" ]; then
		mygw=`cfgcmd get InternetGatewayDevice.ManagementServer.X_TRI_TR069WANGATEWAY`
	fi
	if [ "$myip" == "" ]; then
		myip=`cfgcmd get InternetGatewayDevice.ManagementServer.X_TRI_TR069WANIPADDR`
	fi

	if [ "$1" == "stop" ]; then
		stop_tr069_agent
	fi
	if [ "$1" == "quick_start" ]; then
		create_tr069_agent
	fi
fi
