#!/bin/bash

############################################################################
# Skript zur Konfiguration eines neuen Netzwerkschnittstellen-Profils.
#
# Quellen:
# https://stackoverflow.com/questions/2642585/read-a-variable-in-bash-with-a-default-value#2642592
# https://askubuntu.com/questions/1705/how-can-i-create-a-select-menu-in-a-shell-script#1716
############################################################################

ETH="$( ip r | grep default | awk '{printf $5;}' )"
IP="$( ip a | grep "$ETH" | awk '/inet/{printf $2;}' )"
DR="$( ip r | awk '{ print $3 }' | head -1 )"
DNS1="$( nmcli dev show  2>/dev/null | grep DNS | awk '{ print $2 }' )"
ACON="$(nmcli -g name connection show | head -1)"

printf "\n\n=============================================\n      aktuelles Netzwerkprofil anpassen\n=============================================\n\n"
IP4="$IP"
read -e -r -i "$IP4" -p "IP-Adresse ggf. anpassen: " input
IP4="${input:-$IP4}"

GW4="$DR"
read -e -r -i "$GW4" -p "Default Route ggf. anpassen: " input
GW4="${input:-$GW4}"

DNS1="$DNS1"
read -e -r -i "$DNS1" -p "Bitte DNS-Server1 anpassen: " input
DNS1="${input:-$DNS1}"

#DNS2="1.1.1.1"
#read -e -r -i "$DNS2" -p "Bitte DNS-Server2 anpassen: " input
#DNS2="${input:-$DNS2}"

# List interfaces
Link="$ETH" 
read -e -r -i "$ETH" -p "aktives Netzwerkinterfaces: " input
LINK="${input:-$LINK}"

nmcli con modify "$ACON" ifname "$LINK" ipv4.method manual ipv4.address "$IP4" ipv4.gateway "$GW4" ipv4.dns "$DNS1 $DNS2"

echo "Änderungen wurden übernommen"
sleep 1
printf "\n========================================\nRechner wird in 5 Sekunden neu gestartet\n========================================\n"
echo -ne '<<<<<<<<<<<<<<<<<<<<<<<<<[5]\r'
sleep 1
echo -ne '<<<<<<<<<<<<<<<<<<<<-----[4]\r'
sleep 1
echo -ne '<<<<<<<<<<<<<<<----------[3]\r'
sleep 1
echo -ne '<<<<<<<<<<---------------[2]\r'
sleep 1
echo -ne '<<<<<--------------------[1]\r'
sleep 0.5
echo "bye bye, see ya............."
sleep 0.5
reboot

exit 0
