#!/bin/bash

# Copyright (c) 2023 Paul PASCUAL (paul@paul-pascual.com)
# https://paul-pascual.com/share/tools/backtonormalnetwork.sh
# Version 1.1
#
# This program is free software: you can redistribute it and/or modify
#
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>
#
# The license is available on this server here:
# https://paul-pascual.com/licences/gpl-3.0.txt

netplan_file='/etc/netplan/50-cloud-init.yaml'
netplan_backup_file='/root/netplan_backup_file.yaml'
network_file='/etc/network/interfaces'
grub_config="/etc/default/grub"

# Verify if the netplan file exists
if [ -f "$netplan_file" ]; then

	echo -e "\033[0;32m$netplan_file exists\033[0m"

	# Disable cloudinit's network auto configuration
	echo "network: {config: disabled}" > /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg

	# Install ifupdown2
	apt-get install -y ifupdown2

	# Backup netplan configuration file
	cp $netplan_file $netplan_backup_file

	# Extracting network informations from netplan file
	interface_name=$(awk '/ethernets:/{getline; sub(/:$/, ""); print}' $netplan_file | tr -d ' ')
	ip_address=$(ip -4 addr show ${interface_name} | grep -oP 'inet \K[\d.]+/\d+')
	gateway=$(ip route | grep default | awk '{print $3}')

	# Creating new network file
	network_content="
auto $interface_name
iface $interface_name inet static
	address $ip_address
	gateway $gateway
"
sleep 30
	cat >$network_file <<EOF
	$network_content
EOF

	# Fix interface name in grub
	# Verify if GRUB configuration file exists
	if [ -f "$grub_config" ]; then
		echo -e "\033[0;32mGRUB config exists.\033[0m"
		# Add line to GRUB configuration if it does not already exist
    		if ! grep -q "net.ifnames=0 biosdevname=0" "$grub_config"; then
        		echo "Adding net.ifnames=0 biosdevname=0 to GRUB configuration..."
        		sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="\1 net.ifnames=0 biosdevname=0"/' "$grub_config"
    		else
        		echo "The line net.ifnames=0 biosdevname=0 already exists in the GRUB configuration !"
    		fi

    		# Update GRUB configuration
    		update-grub
    		echo -e "\033[0;32mGRUB configuration has been updated\033[0m"
	else
		echo -e "\033[0;32mThe GRUB configuration file ($grub_config) does not exist. Doing nothing.\033[0m"
	fi

	# Disable netplan
	apt-get remove --purge -y netplan.io
	rm -rf /etc/netplan
	rm -rf /usr/share/netplan

	# Fix systemd-networkd (required for boot)
	systemctl unmask networking
	systemctl enable networking
	systemctl restart networking

	systemctl stop systemd-networkd.socket systemd-networkd \
	networkd-dispatcher systemd-networkd-wait-online

	systemctl disable systemd-networkd.socket systemd-networkd \
	networkd-dispatcher systemd-networkd-wait-online

	systemctl mask systemd-networkd.socket systemd-networkd \
	networkd-dispatcher systemd-networkd-wait-online


	# Remove systemd-resolved
	systemctl disable --now systemd-resolved.service
  	systemctl disable --now systemd-resolved.service
	apt-get remove --purge -y resolvconf

	# Add DNS in resolv.conf
	rm /etc/resolv.conf #/etc/resolv.conf is an alias of /run/systemd/resolve/resolv.conf
	echo "nameserver 1.1.1.1" > /etc/resolv.conf
	echo "nameserver 9.9.9.9" >> /etc/resolv.conf
else
	echo -e "\033[38;5;160m$netplan_file doesn't exists, doing nothing.\033[0m"
	exit 1
fi
