#!/bin/bash
########################################################
######################################################## 
# Wire-G App for simple management a Wireguard TUNNEL ##
# Author: Désiré Werner Menrath                       ##
# Email: polunga40@unity-mail.de                      ##
# Translate German to English with LibreTranslate     ##
# Translatefiles edit with Poedit                     ##
# Use without warranty!                               ##
######################################################## 
########################################################

Encoding=UTF-8

# i18n - Internationalization - Internationalisierung

export TEXTDOMAIN=wire-g
export TEXTDOMAINDIR="/usr/share/locale"

############################################################################################################ 
############################################################################################################ 
VERSION=3.1.9
# wg_workdir Arbeitsverzeichnis wg
WG_WDIR="/home/$USER/.config/wg_nmcli/"
# Pfad wg Icons
WG_PIC="/home/$USER/.icons/"
# Wireguard TUNNEL auslesen mit grep nur Wireguard zeigen 
# mit sed alles nach dem ersten Leerzeichen bis Zeilenende weggeschneiden und tunnel.txt schreiben
nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/  .*//' > ${WG_WDIR}tunnel.txt
# Variable der verfügbaren TUNNELanzeige für Yad
TUNNEL=$(nmcli connection show | grep -iPo "(.*)(wireguard)" | sed 's/  .*//') 
# Variable der aktiven TUNNELanzeige für Yad 
ACTIVE=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//')
# Dies zeigt auf die richtige pid
 
############################################################################################################
############################################################################################################ 

wg_notify(){
    sleep 1 
    nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//' &> ${WG_WDIR}onstart.txt
    ACTIVE=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//')
    case $LANG in  
   
        de_DE.UTF-8)  OPEN=öffnen ;;
        
                  *)  OPEN=open ;;
    esac         
        yad --image-on-top --image=${WG_PIC}wg-vpn-info.png \
            --undecorated --borders=8 --skip-taskbar \
            --text-align=center --no-buttons --auto-close \
            --timeout 1 \
            --text=$"<span color='#0fad0a'><b>$ACTIVE</b></span>   <span><b>powered</b></span>"

    yad --notification \
        --image="${WG_PIC}wg-vpn.png" \
        --icon-size=32 --no-middle \
        --text=$"$ACTIVE activ" \
        --menu=$"Wire-G open!/sbin/wire_g
        |$ACTIVE stop!/sbin/wg_stop" \
        --command="menu" &
# Schreib die pid von Notify in 
    pid=$(ps aux | grep yad | grep -v grep | awk '{print $2}')
    echo $pid > ${WG_WDIR}for-kill_pid.txt
    IFS=$'\n' lines=( $(cat ${WG_WDIR}for-kill_pid.txt) ) 

}   

# function end
############################################################################################################ 
############################################################################################################ 
# funktion wireguard import 

import_wg(){
    
    if WG_CONF=$(yad --file --separator=" \n" \
                     --button="OK" --button=$"Cancel" \
                     --width=1200 --height=800 --no-klick \
                     --window-icon=${WG_PIC}wg-import.png \
                     --title=$"Wireguard .conf Select file")
        then
            echo $WG_CONF > ${WG_WDIR}exist.txt
            if grep -f ${WG_WDIR}tunnel.txt ${WG_WDIR}exist.txt
                then 
                    yad --image-on-top --image=${WG_PIC}wg-info.png \
                        --text=$"Tunnel already exists!" \
                        --text-align=center --button "OK" \
                        --borders=8 --undecorated \
                        --timeout-indicator=bottom --skip-taskbar
                        import_wg  
            else
        
                case "$WG_CONF" in

                    *_*.conf) if grep -i 'PEER\|PublicKey' $WG_CONF  > /dev/null &&  grep -i 'Interface\|PrivateKey' $WG_CONF > /dev/null
                                then
                                    ACTIVE=$(nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//')
                                    if [[ -n $ACTIVE ]]
                                        then  
                                            wg_stop
                                            nmcli connection import type wireguard file $WG_CONF &> ${WG_WDIR}.tmp.txt
                                            wg_notify                                    
                                    else
                                            kill $lines
                                            nmcli connection import type wireguard file $WG_CONF &> ${WG_WDIR}.tmp.txt
                                            wg_notify 
                                    fi                                    
                                nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//' &> ${WG_WDIR}onstart.txt
                                IFS=$'\n' WG_START=( $(cat ${WG_WDIR}onstart.txt) )
                                nmcli con mod "$WG_START" connection.autoconnect no
                              fi ;;

                           *) yad --image-on-top --image=${WG_PIC}wg-info.png --height=150 --width=240 \
                                  --text-align=center --undecorated --skip-taskbar \
                                  --borders=12 --button="OK" --buttons-layout=center --center \
                                  --text $"<b>Oh, something went wrong. No valid Wireguard file. </b> \n
<b>Here is an example:</b><span color='#0fad0a'><b> my_wireguard.conf</b></span>"           
import_wg ;;
                esac 
            fi

    fi }

# function end
############################################################################################################ 
############################################################################################################ 
# funktion wireguard remove

remove(){ 
  
    nmcli connection show --active | grep -iPo "(.*)(wireguard)" | sed 's/  .*//' &> ${WG_WDIR}.wg_noactive.txt
    ACTIVE=$(cat ${WG_WDIR}.wg_noactive.txt)

        nmcli connection delete $SELECTION &> ${WG_WDIR}.tmp.txt 
        if SUCCESS=$(grep -i 'erfolgreich gelöscht\|successfully deleted' ${WG_WDIR}.tmp.txt)
            then 
                yad --window-icon=${WG_PIC}wg-trash.png --undecorated \
                    --image-on-top --image=${WG_PIC}wg-trash-info.png \
                    --text=$"<span>Your Tunnel $SELECTION was successfully deleted.</span> \n
<span>Attention! The autostart has been reset.</span> \n
<span>If you need the autostart for another tunnel,</span> \n
<span>please activate it again.</span> \n" \
                    --text-align=center --button=$"OK" \
                    --fixed --buttons-layout=center --borders=8 
        else 
                yad --window-icon=${WG_PIC}wg-trash.png --undecorated \
                    --image-on-top --image=${WG_PIC}wg-trash-info.png \
                    --text=$"Oh something went wrong.\nPlease delete Tunnel in the network manager." \
                    --text-align=center --button=$"OK" --height=60 --width=350 \
                    --buttons-layout=center --borders=8 --fixed               
        fi

        if [ "$SELECTION" = "$ACTIVE" ]
            then
                IFS=$'\n' lines=( $(cat ${WG_WDIR}for-kill_pid.txt) )
                kill $lines 
        fi
        if grep 'TRUE TRUE' ${WG_WDIR}about.txt
            then 
                echo FALSE TRUE > ${WG_WDIR}about.txt
        
        elif grep 'TRUE FALSE' ${WG_WDIR}about.txt
            then
                echo FALSE FALSE > ${WG_WDIR}about.txt
        fi
        rm /home/$USER/.config/autostart/wgonstart.desktop
            } 


# function end
############################################################################################################ 
############################################################################################################ 
#funktion for active TUNNEL a stop 

up_or_down(){

    if [[ -z $ACTIVE ]]
        then
            yad --image-on-top --image=${WG_PIC}wg-info.png \
                --text=$"There is no active Tunnel that you could stop." \
                --text-align=center --no-buttons \
                --borders=8 --undecorated \
                --timeout 3 --timeout-indicator=bottom --skip-taskbar
    else
            wg_stop

    fi }

# funktion end
############################################################################################################ 
############################################################################################################ 
#for about of select Autostart

on_start(){

if [ -d /home/$USER/.config/autostart ] # for GNOME, XFCE, KDE, Budgie
            then 
                if grep 'TRUE TRUE\|TRUE FALSE' ${WG_WDIR}about.txt 
                    then 
                        cp /home/"$USER"/.local/share/wg_nmcli/wgonstart.desktop /home/"$USER"/.config/autostart/
                else 
                        rm /home/"$USER"/.config/autostart/wgonstart.desktop
                fi

        elif [ -d /home/$USER/.config/lxsession/Lubuntu/autostart ] # for LXDE
            then
                if grep 'TRUE TRUE\|TRUE FALSE' ${WG_WDIR}about.txt
                    then
                        cp /home/"$USER"/.local/share/wg_nmcli/wgonstart.desktop  /home/$USER/.config/lxsession/Lubuntu/autostart/ 
                else
                        rm /home/$USER/.config/lxsession/Lubuntu/autostart/wgonstart.desktop
                fi
        fi
}


# function button "about" 

about(){ 
    
        if grep 'TRUE TRUE' ${WG_WDIR}about.txt
            then
                yad --form --fixed \
                    --borders=8 --buttons-layout=center --text-align=center \
                    --button="OK" --undecorated \
                    --field=$"Autoconnect on PC Start:CHK" TRUE \
                    --field=$"Search automatically for Wire-G updates:CHK" TRUE \
                    --image-on-top --image=${WG_PIC}wg-vpn-info.png \
                    --separator=" " --window-icon=${WG_PIC}wg-vpn.png \
                    --text=$"<span color='#626ff1'><b>Wire-G Author: Désiré Werner Menrath</b></span> \n 
<span color='#626ff1'><b>E-Mail: polunga40@unity-mail.de</b></span> \n
<span color='#626ff1'><b>At all Tunnels, the auto start is disabled.</b></span> \n
<span color='#626ff1'><b>Version: $VERSION </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
<a href='https://git.lunix.dedyn.io/punix/Wire-G/releases'>Download Wire-G </a> \n" &> ${WG_WDIR}about.txt

                on_start

        elif grep 'FALSE TRUE' ${WG_WDIR}about.txt
            then
                yad --form --fixed \
                    --borders=8 --buttons-layout=center --text-align=center \
                    --button="OK" --undecorated \
                    --field=$"Autoconnect on PC Start:CHK" FALSE \
                    --field=$"Search automatically for Wire-G updates:CHK" TRUE \
                    --image-on-top --image=${WG_PIC}wg-vpn-info.png \
                    --separator=" " --window-icon=${WG_PIC}wg-vpn.png \
                    --text=$"<span color='#626ff1'><b>Wire-G Author: Désiré Werner Menrath</b></span> \n 
<span color='#626ff1'><b>E-Mail: polunga40@unity-mail.de</b></span> \n
<span color='#626ff1'><b>At all Tunnels, the auto start is disabled.</b></span> \n
<span color='#626ff1'><b>Version: $VERSION </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
<a href='https://git.lunix.dedyn.io/punix/Wire-G/releases'>Download Wire-G </a> \n" &> ${WG_WDIR}about.txt

                on_start

        elif grep 'FALSE FALSE' ${WG_WDIR}about.txt
            then
                yad --form --fixed \
                    --borders=8 --buttons-layout=center --text-align=center \
                    --button="OK" --undecorated \
                    --field=$"Autoconnect on PC Start:CHK" FALSE \
                    --field=$"Search automatically for Wire-G updates:CHK" FALSE \
                    --image-on-top --image=${WG_PIC}wg-vpn-info.png \
                    --separator=" " --window-icon=${WG_PIC}wg-vpn.png \
                    --text=$"<span color='#626ff1'><b>Wire-G Author: Désiré Werner Menrath</b></span> \n 
<span color='#626ff1'><b>E-Mail: polunga40@unity-mail.de</b></span> \n
<span color='#626ff1'><b>At all Tunnels, the auto start is disabled.</b></span> \n
<span color='#626ff1'><b>Version: $VERSION </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
<a href='https://git.lunix.dedyn.io/punix/Wire-G/releases'>Download Wire-G </a> \n" &> ${WG_WDIR}about.txt

                on_start

        elif grep 'TRUE FALSE' ${WG_WDIR}about.txt
            then
                yad --form --fixed \
                    --borders=8 --buttons-layout=center --text-align=center \
                    --button="OK" --undecorated \
                    --field=$"Autoconnect on PC Start:CHK" TRUE \
                    --field=$"Search automatically for Wire-G updates:CHK" FALSE \
                    --image-on-top --image=${WG_PIC}wg-vpn-info.png \
                    --separator=" " --window-icon=${WG_PIC}wg-vpn.png \
                    --text=$"<span color='#626ff1'><b>Wire-G Author: Désiré Werner Menrath</b></span> \n 
<span color='#626ff1'><b>E-Mail: polunga40@unity-mail.de</b></span> \n
<span color='#626ff1'><b>At all Tunnels, the auto start is disabled.</b></span> \n
<span color='#626ff1'><b>Version: $VERSION </b></span> \n
<span color='#626ff1'><b>Use without warranty.</b></span> \n
<a href='https://git.lunix.dedyn.io/punix/Wire-G/releases'>Download Wire-G </a> \n" &> ${WG_WDIR}about.txt

                on_start

fi

}

# function end
############################################################################################################ 
############################################################################################################
# funktion wireguard enable select Tunnel

connect(){

    if SUCCESS=$(grep -i 'erfolgreich aktiviert\|successfully activated' ${WG_WDIR}.tmp.txt)
        then 
            wg_notify  
                
    fi }

# function end
############################################################################################################ 
############################################################################################################                                                                                                                                 # --text "<b>Aktiver TUNNEL:</b> <span color='#0fad0a'><b>$ACTIVE</b></span> # mit <b>gewünschtes wort</b> wird fett geschrieben. 
# Farbe für Variable $ACTIVE festgelegt mit fetter Schrift. (bei --text zwischen " ")
if [[ ! -d ${WG_WDIR} || ! -e ${WG_WDIR}about.txt ]]
    then
        mkdir -p /home/"$USER"/.config/wg_nmcli
        echo FALSE TRUE > ${WG_WDIR}about.txt
fi
#Here is update of Wire-G for future
#if AUTOUPDATE=$(grep 'TRUE TRUE\|FALSE TRUE' ${WG_WDIR}about.txt) 
#        then 
#            here comms command to update Wire-G
#fi
SELECTION=$(yad --no-klick --list --center \
                   --height=300 --width=60 --title="Wire-G" \
                   --buttons-layout=center \
                   --button=$"Start:0" --button=$"Stop:2" \
                   --button=$"Import:3" --button=$"Remove:4" \
                   --button=$"About:5" --separator="" \
                   --window-icon=${WG_PIC}wg-vpn.png \
                   --borders=8 --image-on-top --image=${WG_PIC}wg-active.png \
                   --column=$"Selection" Tunnel \
                   --rest=${WG_WDIR}tunnel.txt \
                   --text=$"<b>Activ Tunnel: </b> <span color='#0fad0a'><b>$ACTIVE</b></span> 
<b>-----------------------------------------------------------------------------------------------------</b>  
<b>If no tunnels are listed,</b> 
<b>you must first import your tunnel.</b>") 
RET=$? 

[[ $? -eq 1 ]] && exit 0

if [[ $RET -eq 2 ]]
    then 
        up_or_down
        wire_g
fi

if [[ $RET -eq 3 ]]
    then 
        import_wg
        wire_g
fi

if [[ $RET -eq 4 ]]
    then 
        remove
        wire_g
fi

if [[ $RET -eq 5 ]]
    then 
        about
        wire_g
fi

if [[ $RET -eq 0 ]]
    then
        if [[ -n $ACTIVE ]]
            then
                wg_stop
                nmcli connection up $SELECTION &> ${WG_WDIR}.tmp.txt 
                connect
                wire_g 
        else
                nmcli connection up $SELECTION &> ${WG_WDIR}.tmp.txt
                connect
                wire_g
        fi
fi
############################################  END  ##########################################################