#!/bin/bash

SECRETFILE="/etc/ipsec.secrets"
EAPFIRST="passwort"
EAPNAME="username"
VPNCONN="livedesign"

NEWPASS=$(zenity --password --title="2FA Code eingeben")

[ -z "$NEWPASS" ] && exit 1

LINE="$EAPNAME : EAP '$EAPFIRST$NEWPASS'"

if grep -q "^$EAPNAME : EAP " "$SECRETFILE"; then
    sed -i "/^$EAPNAME : EAP /c\\$LINE" "$SECRETFILE"
else
    echo "$LINE" >> "$SECRETFILE"
fi

ipsec rereadsecrets
ipsec down "$VPNCONN" 2>/dev/null
sleep 1

if ipsec up "$VPNCONN"; then
    zenity --info \
      --title="VPN" \
      --text="VPN erfolgreich verbunden."
else
    zenity --error \
      --title="VPN" \
      --text="VPN konnte nicht aufgebaut werden."
fi