#!/bin/bash

process_started[0]='FALSE'
process_started[1]='TRUE'

if ! ip a | grep -q wg0; then
  process_started[0]='TRUE'
  process_started[1]='FALSE'
fi

output=$(zenity --title='WireGuard Manager' --text='Was möchtest du tun?' --list --radiolist --column='Select' --column='Choice' --column='Option' ${process_started[0]} 'up' 'Verbinden' FALSE 'restart' 'Restart' ${process_started[1]} 'down' 'Trennen' --hide-column=2)

if [ -z $output ]; then
  exit 1
fi

if [ -n $output ]; then
  passwd=$(zenity --entry --hide-text --text="Gebe dein Anmeldepasswort vom Laptop ein:" --title="Anmeldungs Passwort")
fi

if [ "$output" == "restart" ]; then
  echo -e "$passwd\n" | sudo wg-quick down wg0
  echo -e "$passwd\n" | sudo systemctl stop wg-quick@wg0.service
  echo -e "$passwd\n" | sudo systemctl start wg-quick@wg0.service
  exit
fi

echo -e "$passwd\n" | sudo wg-quick $output wg0 >/dev/null

if [ "$output" == "up" ]; then
  if ping -c1 10.200.100.1 >/dev/null; then
    zenity --info --text="VPN Wurde erfolgreich verbunden"
  fi
fi
