#!/bin/sh

[ "$(uci -q get freifunk.wizard)" == "settings" ] || uci set freifunk.wizard=settings
uci set freifunk.interface.dns="2002:d596:2a92:1:71:53:: 2002:5968:c28e::53 88.198.178.18 141.54.1.1 212.204.49.83 8.8.8.8 8.8.4.4"
uci commit freifunk
uci set uhttpd.main.rfc1918_filter=0
uci set uhttpd.main.listen_http="80"
uci set uhttpd.main.listen_https="443"
uci commit uhttpd
uci set luci_olsr.general.resolve=1
uci commit luci_olsr
uci set luci.general=internal
uci set luci.general.resolve=1
uci commit luci

tables="/etc/iproute2/rt_tables"
test -d /etc/iproute2/ || mkdir -p /etc/iproute2/
grep -q "111 olsr" $tables || echo "111 olsr" >> $tables
grep -q "112 olsr-default" $tables || echo "112 olsr-default" >> $tables
grep -q "113 olsr-tunnel" $tables || echo "113 olsr-tunnel" >> $tables

(
sleep 10
if [ -f /etc/config/openvpn ] ; then
	if ! uci -q get openvpn.ffvpn >/dev/null ; then
		uci delete openvpn.custom_config
		uci delete openvpn.sample_server
		uci delete openvpn.sample_client
		uci set openvpn.ffvpn=openvpn
		uci set openvpn.ffvpn.enabled=0
		uci set openvpn.ffvpn.client=1
		#uci set openvpn.ffvpn.lport=1192
		uci set openvpn.ffvpn.nobind=1
		uci set openvpn.ffvpn.proto=udp
		uci set openvpn.ffvpn.dev=ffvpn
		uci set openvpn.ffvpn.dev_type=tun
		uci set openvpn.ffvpn.persist_key=1
		uci set openvpn.ffvpn.keepalive="10 60"
		uci set openvpn.ffvpn.ns_cert_type=server
		uci set openvpn.ffvpn.comp_lzo="no"
		uci set openvpn.ffvpn.script_security=2
		uci set openvpn.ffvpn.cipher="none"
		uci add_list openvpn.ffvpn.remote="77.87.48.10 1194 udp"
		uci add_list openvpn.ffvpn.remote="78.41.116.65 1194 udp"
		uci set openvpn.ffvpn.ca="/etc/openvpn/freifunk-ca.crt"
		uci set openvpn.ffvpn.cert="/etc/openvpn/freifunk_client.crt"
		uci set openvpn.ffvpn.key="/etc/openvpn/freifunk_client.key"
		uci set openvpn.ffvpn.status="/var/log/openvpn-status-ffvpn.log"
		uci set openvpn.ffvpn.up="/etc/openvpn/ffvpn-up.sh"
		uci set openvpn.ffvpn.route_nopull=1
		uci commit openvpn
		uci set network.ffvpn=interface
		uci set network.ffvpn.ifname=ffvpn
		uci set network.ffvpn.proto=none
		uci commit network
	fi
fi
) >/dev/null 2>&1 &
exit 0

