#! /bin/bash

connected_interface=$(awg show interfaces | grep -P "awg\d+" -o)

connect() {
  #		doas wg-quick up wg0 && doas route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1 enp5s0 && notify-send -i /home/$USER/.config/dunst/scripts/vpn.png 'VPN connected'
  doas awg-quick up awg0 && sleep 3 && doas route add -host 178.49.132.41 gw 192.168.1.1 enp5s0 && doas systemctl restart dnsproxy.service && notify-send -i /home/$USER/.config/dunst/scripts/vpn.png 'VPN connected'

  #		sudo wg-quick up wg0 && sudo route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1 enp9s0 && notify-send -i /home/$USER/.config/dunst/scripts/vpn.png 'VPN connected'

  #    sudo wg-quick up wg0 && sudo route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.1.1 enp9s0 && sudo route add -host 78.47.125.180 gw 192.168.1.1 enp9s0 && notify-send -i /home/$USER/.config/dunst/scripts/vpn.png 'VPN connected'
}

disconnect() {
  # Normally we should have a single connected interface but technically
  # there's nothing stopping us from having multiple active intgerfaces so
  # let's do this in a loop:
  for connected_config in $(awg show interfaces | grep -P "awg\d+" -o); do
    doas awg-quick down awg0 && notify-send -i /home/$USER/.config/dunst/scripts/vpn.png 'VPN disconnected'
  done
}

toggle() {
  if [[ $connected_interface ]]; then
    disconnect
  else
    connect
  fi
}

print() {
  if [[ $connected_interface ]]; then
    echo "%{T1}說%{T-} %{T2}VPN: ON%{T-}"
    #echo "%{F#FFDE7D}%{T1}說%{T-} %{T2}VPN: ON%{T-}"
  else
    echo "%{T1}說%{T-} %{T2}VPN: OFF%{T-}"
    #echo "%{F#5f5f5f}%{T1}說%{T-} %{T2}VPN: OFF%{T-}"
  fi
}

case "$1" in
--connect)
  connect
  ;;
--disconnect)
  disconnect
  ;;
--toggle)
  toggle
  ;;
*)
  print
  ;;
esac
