#!/bin/bash
#update & install
apt update
apt install ifupdown qemu-guest-agent -y
systemctl start qemu-guest-agent
#export
export IP=$(ip addr show dev eth0 | grep inet | head -1 | awk '{print $2}')
export GW=$(ip route | grep default | awk '{print $3}')
export BC=$(ip addr show dev eth0 | grep inet | head -1 | awk '{print $2}' | cut -d '/' -f 1 | cut -d '.' -f 1,2,3).255
export DM=$(hostname -f | cut -d '.' -f 2,3)
export N1=$(grep nameserver /etc/resolv.conf | head -1 | cut -d ' ' -f 2)
export N2=$(grep nameserver /etc/resolv.conf | tail -1 | cut -d ' ' -f 2)
#Tulis etc network itnerfaces
cat >> /etc/network/interfaces <<EOF
source /etc/network/interfaces.d/*
auto lo
iface lo inet loopback

allow-hotplug eth0
auto eth0
iface eth0 inet static
	address $IP
	broadcast $BC
	gateway $GW
EOF
#down dan upkan lagi
ifdown --force eth0 lo && ifup -a
sleep 3
#enable dan restart networking
systemctl unmask networking
systemctl enable networking
systemctl restart networking
#disable networkd
systemctl stop systemd-networkd.socket systemd-networkd systemd-networkd-wait-online
systemctl disable systemd-networkd.socket systemd-networkd systemd-networkd-wait-online
systemctl mask systemd-networkd.socket systemd-networkd systemd-networkd-wait-online
#remove netplan
apt purge nplan netplan.io -y
rm -Rf /etc/netplan
rm -Rf /usr/share/netplan
#remove systemd-resolved
systemctl stop systemd-resolved
rm -Rf /etc/resolv.conf
apt remove systemd-resolved -y
#tulis ulang etc resolv conf
cat > /etc/resolv.conf <<EOF
nameserver $N1
nameserver $N2
search $DM
EOF
#update grub untuk pake eth0
sed -i '/GRUB_CMDLINE_LINUX/ s/^\(.*\)\("\)/\1 net.ifnames=0\2/' /etc/default/grub
grub-mkconfig -o /boot/grub/grub.cfg
update-grub2
#done, restart
reboot
