#!/bin/bash
  
sleep 5
pass=$(cat /var/lib/dhcp/dhclient* | awk '/password/ {print $3; exit}' | awk -F\" '{print $2}')
nfs_server=$(cat /var/lib/dhcp/dhclient* | awk '/dhcp-server-identifier/ {print $3; exit}' | tr -d \;)
public_ip=$(cat /var/lib/dhcp/dhclient* | awk '/public-ip/ {print $3; exit}' | awk -F\" '{print $2}')
public_gateway=$(cat /var/lib/dhcp/dhclient* | awk '/public-gateway/ {print $3; exit}' | tr -d \;)
echo "root:$pass" | chpasswd
echo -e "$nfs_server\tnfs-server" >> /etc/hosts
#curl -s "http://$nfs_server:8300/disable-rescue-boot"
mount -a
if [ -n "$public_ip" ] && [ -n "$public_gateway" ]
then
    ip addr add $public_ip dev eno1
    cat <<EOF> /etc/network/interfaces
auto lo
iface lo inet loopback

allow-hotplug eno1
iface eno1 inet dhcp
auto eno1 
iface eno1 inet static
        address $public_ip
        netmask 255.255.255.0
        gateway $public_gateway

allow-hotplug eno2
iface eno2 inet dhcp
EOF
#        systemctl restart networking
        ip route delete default
        ip route add default via $public_gateway
fi

installcommand="$(cat /proc/cmdline | grep -oP 'installcommand=\"[^"]*' | awk -F\" '{print $2}')"
if [ -n "$installcommand" ]
then
	curl -s "http://$nfs_server:8300/disable-rescue-boot"
	eval $installcommand
	/usr/sbin/reboot        
fi

