#!/bin/sh

if [ "$(uci -q get network.omrvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		delete network.glorytun
		delete network.omrvpn
		set network.omrvpn=interface
		set network.omrvpn.device=tun0
		set network.omrvpn.proto=dhcp
		set network.omrvpn.ip4table=vpn
		set network.omrvpn.multipath=off
		set network.omrvpn.leasetime=12h
		set network.omrvpn.type=tunnel
		set network.omrvpn.txqueuelen=100
		commit network
	EOF
fi

if [ "$(uci -q get glorytun.vpn.localip)" = "10.0.0.2" ]; then
	uci -q batch <<-EOF >/dev/null
		delete glorytun.vpn.localip
		delete glorytun.vpn.remoteip
	EOF
fi

if [ "$(uci -q get network.omrvpn.txqueuelen)" = "1000" ]; then
	uci -q batch <<-EOF >/dev/null
		set network.omrvpn.txqueuelen=100
		commit network
	EOF
fi

if [ "$(uci -q get openvpn.omr.proto)" != "tcp-client" ]; then
	uci -q batch <<-EOF >/dev/null
		set openvpn.omr=openvpn
		set openvpn.omr.dev=tun0
		set openvpn.omr.port=65301
		set openvpn.omr.cipher=AES-256-CBC
		set openvpn.omr.proto=tcp-client
		set openvpn.omr.auth_nocache=1
		set openvpn.omr.client=1
		set openvpn.omr.tls_client=1
		set openvpn.omr.reneg_sec=0
		set openvpn.omr.allow_recursive_routing=1
		set openvpn.omr.sndbuf=0
		set openvpn.omr.rcvbuf=0
		set openvpn.omr.route_delay=5
		set openvpn.omr.disable_dco=1
		set openvpn.omr.ping_restart=60
		commit openvpn
	EOF
fi
if [ -z "$(uci -q get openvpn.omr.disable_dco)" ]; then
	uci -q batch <<-EOF >/dev/null
		set openvpn.omr.disable_dco=1
		commit openvpn
	EOF
fi
if [ -z "$(uci -q get openvpn.omr.ping_restart)" ]; then
	uci -q batch <<-EOF >/dev/null
		set openvpn.omr.ping_restart=60
		commit openvpn
	EOF

fi


uci -q delete openvpn.omr.secret
uci -q delete openvpn.omr.ncp_disable=0

#if [ "$(uci -q get openvpn.omr.com_lzo)" = "" ]; then
#	uci -q batch <<-EOF >/dev/null
#		set openvpn.omr.comp_lzo=adaptive
#	EOF
#fi
if [ "$(uci -q get ipsec.ipsec.password)" = "myvpn" ]; then
	uci -q batch <<-EOF >/dev/null
		set ipsec.ipsec.enabled='0'
		commit ipsec
	EOF
	chmod u+x /etc/init.d/ipsec
fi

if [ "$(uci -q get dsvpn.vpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set dsvpn.vpn=dsvpn
		set dsvpn.vpn.dev=tun0
		set dsvpn.vpn.port=65011
		set dsvpn.vpn.localip=10.255.251.2
		set dsvpn.vpn.remoteip=10.255.251.1
		set dsvpn.vpn.mode=client
		set dsvpn.vpn.enable=0
		commit dsvpn
	EOF
fi

if [ "$(uci -q get dsvpn.vpn.mode)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set dsvpn.vpn.mode=client
		commit dsvpn
	EOF
fi

if [ "$(uci -q show firewall | grep omrvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list firewall.zone_vpn.network=omrvpn
		delete firewall.allow_dhcp_request_vpn
		set firewall.allow_dhcp_request_vpn=rule
		set firewall.allow_dhcp_request_vpn.name=Allow-DHCP-Request-VPN
		set firewall.allow_dhcp_request_vpn.src=vpn
		set firewall.allow_dhcp_request_vpn.proto=udp
		set firewall.allow_dhcp_request_vpn.dest_port=67
		set firewall.allow_dhcp_request_vpn.target=ACCEPT
		set firewall.allow_dhcp_request_vpn.family=ipv4
		commit firewall
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep glorytun)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="glorytun"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep glorytun-udp)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="glorytun-udp"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@network[-1].affects | grep dsvpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@network[-1].affects="dsvpn"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@glorytun[-1].affects | grep openmptcprouter-vps)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@glorytun[-1].affects="openmptcprouter-vps"
		commit ucitrack
	EOF
fi
if [ "$(uci -q get ucitrack.@glorytun-udp[-1].affects | grep openmptcprouter-vps)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		add_list ucitrack.@glorytun-udp[-1].affects="openmptcprouter-vps"
		commit ucitrack
	EOF
fi

if [ "$(uci -q get glorytun-udp.vpn.key)" = "" ] && [ "$(uci -q get glorytun.vpn.key)" != "" ]; then
	uci -q batch <<-EOF >/dev/null
		set glorytun-udp.vpn.key=$(uci -q get glorytun.vpn.key)
		set glorytun-udp.vpn.host=$(uci -q get glorytun.vpn.host)
	EOF
	if [ "$(uci -q get glorytun.vpn.proto)" = "udp" ]; then
		uci -q batch <<-EOF >/dev/null
			set glorytun-udp.vpn.enable=1
			set glorytun.vpn.enable=0
			commit glorytun
		EOF
	fi
	uci -q commit glorytun-udp
fi

if [ "$(uci -q get network.tun0.mtu)" = "1200" ] || [ -z "$(uci -q get network.tun0.mtu)" ]; then
	uci -q batch <<-EOF >/dev/null
		set network.tun0=device
		set network.tun0.name='tun0'
		set network.tun0.mtu=1500
		commit network
	EOF
fi

# Set the default VPN
if [ "$(uci -q get openmptcprouter.settings.vpn)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set openmptcprouter.settings.vpn='openvpn'
		set glorytun.vpn.enable='1'
		commit glorytun
		commit openmptcprouter
	EOF
fi

rm -f /tmp/luci-indexcache
exit 0
