#/bin/sh
rm -f /etc/apt/sources.list.d/nvpnc.list
apt-get update
killall python
nvpnc=0
killswitch=""
if [ -f /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/killswitch ]; then
	killswitch=$(cat /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/killswitch)
fi
if [ -f /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/iptables_off ]; then
	/usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/iptables_off
	nvpnc=1
fi
#if [ -f /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/route_off ]; then
	#/usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/route_off
#fi
apt-get remove -y enigma2-plugin-extensions-nordvpnconnector
apt-get purge -y enigma2-plugin-extensions-nordvpnconnector
rm /var/lib/dpkg/info/enigma2-plugin-extensions-nordvpnconnector*
if [ $nvpnc == 1 ]; then
	echo "... waiting"
	connmanctl disable ethernet
	sleep 3
	connmanctl enable ethernet
	sleep 10
fi
if [ $(cat /proc/stb/info/model) == "one" ] || [ $(cat /proc/stb/info/model) == "two" ]; then
	echo deb [trusted=yes] http://nordvpnconnector.boxpirates.to/apt64/ ./ > /etc/apt/sources.list.d/nvpnc.list
else
	echo deb [trusted=yes] http://nordvpnconnector.boxpirates.to/apt/ ./ > /etc/apt/sources.list.d/nvpnc.list
fi
apt-get update
if [ $(cat /proc/stb/info/model) == "one" ] || [ $(cat /proc/stb/info/model) == "two" ]; then
	apt-get install -y --allow-unauthenticated python-certifi liblzo2-2 python-requests python-urllib3 python-ndg-httpsclient python-pysocks openvpn curl kernel-module-xt-multiport
else
	apt-get install -y --allow-unauthenticated apt-transport-https kernel-module-xt-multiport kernel-module-xt-state kernel-module-xt-tcpudp python-requests python-urllib3 python-ndg-httpsclient python-pysocks curl openvpn
fi
apt-get install -y enigma2-plugin-extensions-nordvpnconnector
sleep 3
if [ -f //usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/ipv6_off ]; then
	/usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/ipv6_off
fi
if [[ $killswitch == "iptables" ]]; then
	echo "iptables" > /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/killswitch
fi
if [[ $killswitch == "route" ]]; then
	echo "route" > /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/killswitch
fi
if [ -f /etc/openvpn/update-resolv-conf ]; then
	rm -f /etc/openvpn/update-resolv-conf
	cp /usr/lib/enigma2/python/Plugins/Extensions/NordVPNConnector/configs/update-resolv-conf /etc/openvpn
fi
sleep 3
systemctl restart enigma2

