#!/usr/bin/env bash

# DAEMON_OPTS="--nodnsproxy"
CONNMAN_RUNSTATEDIR_RESOLVCONF=yes
#DAEMON_OPTS="--wifi=iwd_agent --nodnsproxy --nobacktrace"
#DAEMON_OPTS="--nodnsproxy --nobacktrace"
DAEMON_OPTS="--nobacktrace --nodnsproxy"

if [[ -f /etc/init.d/connman ]]; then
  /etc/init.d/connman stop
fi

# /etc/init.d/connman sources /etc/default/connman
# Start connman without its own dns server so that is doesn't compete with resolvconf
DEFAULT_FILE=connman
if [[ ! -f /etc/default/${DEFAULT_FILE} ]]; then
  touch /etc/default/${DEFAULT_FILE}
  echo "DAEMON_OPTS=" >/etc/default/${DEFAULT_FILE}
  echo "CONNMAN_RUNSTATEDIR_RESOLVCONF=" >>/etc/default/${DEFAULT_FILE}
fi

cat <<-EOF | sudo tee /etc/default/"${DEFAULT_FILE}" >/dev/null
	DAEMON_OPTS="${DAEMON_OPTS}"
	CONNMAN_RUNSTATEDIR_RESOLVCONF="${CONNMAN_RUNSTATEDIR_RESOLVCONF}"
EOF

# sudo sed -i 's|^\(DAEMON_OPTS=\).*|\1'\"${DAEMON_OPTS}\"'|' /etc/default/"${DEFAULT_FILE}"
# sudo sed -i 's|^\(CONNMAN_RUNSTATEDIR_RESOLVCONF=\).*|\1'\"yes\"'|' /etc/default/"${DEFAULT_FILE}"

CONNMAN_RUNSTATEDIR_RESOLVCONF=no sudo apt install -y connman connman-gtk connman-ui
/etc/init.d/connman stop

# Fix typo in the /etc/init.d/connman script
sed -i 's,\"CONNMAN_RUNSTATEDIR_RESOLVCONF,\"$CONNMAN_RUNSTATEDIR_RESOLVCONF,g' /etc/init.d/connman

# blacklist docker
sed -i '/^# NetworkInterfaceBlacklist =.*/ s/.*/&\nNetworkInterfaceBlacklist = \"=vmnet,vboxnet,virbr,ifb,docker,ve-,vb-,docker,veth,br\"/' /etc/connman/main.conf

sed -i '/^# TetheringTechnologies =.*/ s/.*/&\nTetheringTechnologies = \"wifi,bluetooth,gadget\"/' /etc/connman/main.conf

# Network settings appear under /var/lib/connman/settings:
cat <<-EOF | sudo tee /var/lib/connman/settings >/dev/null
	[global]
	OfflineMode=false

	[Bluetooth]
	Enable=true
	Tethering=false

	[WiFi]
	Enable=true
	Tethering=false

	[Wired]
	Enable=true
	Tethering=false
EOF

# Connection settings
vodafonedistance_ssid="766f6461666f6e6564697374616e6365"
vodafonespeed_ssid="001cbf7b2c70_766f6461666f6e657370656564"

interface=eth0
read -r cable_mac </sys/class/net/"${interface}"/address
cable_name="${cable_mac//:/}"

pass="CFX3CC6Z3adet3aC"

mkdir -p /var/lib/connman/wifi_"${vodafonedistance_ssid}"_managed_psk

cat <<-EOF | tee /var/lib/connman/wifi_"${vodafonedistance_ssid}"_managed_psk/settings >/dev/null
	[wifi_"${vodafonedistance_ssid}"_managed_psk]
	Name=vodafonedistance
	SSID="${vodafonespeed_ssid}"
	Frequency=5540
	Favorite=true
	AutoConnect=true
	Modified=2021-12-11T15:30:40.140960Z
	Passphrase="${pass}"
	IPv4.method=dhcp
	IPv4.DHCP.LastAddress=192.168.1.62
	IPv6.method=auto
	IPv6.privacy=disabled
	IPv6.DHCP.DUID=0001000129478086001cbf7b2c70
	Proxy.Method=direct
	Nameservers=208.67.222.222;94.247.43.254;69.164.196.21;84.200.79.111;
EOF

mkdir -p /var/lib/connman/wifi_${vodafonespeed_ssid}_managed_psk

cat <<-EOF | tee /var/lib/connman/wifi_"${vodafonespeed_ssid}"_managed_psk/settings >/dev/null
	[wifi_"${vodafonespeed_ssid}"_managed_psk]
	Name=vodafonespeed
	SSID="${vodafonespeed_ssid}"
	Frequency=2447
	Favorite=true
	AutoConnect=true
	Modified=2021-12-11T15:29:54.171569Z
	Passphrase="${pass}"
	IPv4.method=dhcp
	IPv4.DHCP.LastAddress=192.168.1.62
	IPv6.method=auto
	IPv6.privacy=disabled
	IPv6.DHCP.DUID=0001000129477d73001cbf7b2c70
	Domains=broadband;
	Proxy.Method=direct
	Nameservers=208.67.222.222;94.247.43.254;69.164.196.21;84.200.79.111;
EOF

mkdir -p /var/lib/connman/ethernet_"${cable_name}"_cable

cat <<-EOF | sudo tee /var/lib/connman/ethernet_"${cable_name}"_cable/settings >/dev/null
	[ethernet_"${cable_name}"_cable]
	Name=Wired
	AutoConnect=true
	Modified=2021-12-11T15:12:27.117312Z
	IPv4.method=dhcp
	IPv4.DHCP.LastAddress=192.168.1.120
	IPv6.method=auto
	IPv6.privacy=disabled
	IPv6.DHCP.DUID=0001000129475f4500e04c36092e
	Nameservers=208.67.222.222;94.247.43.254;69.164.196.21;84.200.79.111;
EOF

/etc/init.d/connman restart
