#!/bin/sh

. /lib/functions/guard.sh
guard "openvpn"

(
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

