#!/bin/bash

# Prepare the (static) interfaces of an SOE4/CentOS VM
# Used within PVC cloud-init steps to perform the auto-IP functionality of
# the soe-install stage2 under the SOE4 hypervisor.

V=0.50  # initial
V=0.51  # support CentOS VMs in addition to SOE4 VMs

set -o errexit
#set -o xtrace

# PVC-provisioned systems on PVC will have a single static public interface at eth1
# The interface eth0 is reserved for the PVC bootstrap network

bootstrap_interface="eth0"
target_interface="eth1"

os_release="$( grep '^NAME=' /etc/os-release | awk -F '"' '{ print $2 }' )"

case "${os_release}" in
    "Debian GNU/Linux")
        bootstrap_interface_file="/etc/network/interfaces.d/${bootstrap_interface}"
        target_interface_file="/etc/network/interfaces.d/${target_interface}"
    ;;
    "CentOS Linux")
        bootstrap_interface_file="/etc/sysconfig/network-scripts/ifcfg-${bootstrap_interface}"
        target_interface_file="/etc/sysconfig/network-scripts/ifcfg-${target_interface}"
    ;;
esac

# Ensure this is the first run
if [[ -f ${target_interface_file} ]]; then
    echo "Network interface ${target_interface} has already been prepared."
    exit 0
fi

# Ensure we're root
if [[ $( whoami ) != "root" ]]; then
    echo "Run this script as root."
    exit 1
fi

# Get the gateway+subnet from the first argument
gateway_address="${1}"

if [[ -z ${gateway_address} ]]; then
    echo "A gateway address in CIDR format must be specified for static configuration to succeed."
fi

echo -n "Determining IPv4 address... "

# defaults for the interface
IPV4_ADDRESS=""

HOSTNAME_QUERY=$( hostname )
TMP_HOST=$( mktemp )
if host -t A ${HOSTNAME_QUERY}. > ${TMP_HOST}; then
    IPV4_ADDRESS=$( awk -v HOSTNAME=${HOSTNAME_QUERY} -F ' has address ' '{if ($1==HOSTNAME){print $2}}' ${TMP_HOST} )
elif getent hosts ${HOSTNAME_QUERY} > $TMP_HOST; then
    IPV4_ADDRESS=$( awk -v HOSTNAME=${HOSTNAME_QUERY} '{if ($2==HOSTNAME){print $1}}' ${TMP_HOST} )
fi
rm ${TMP_HOST}

if [[ -z ${IPV4_ADDRESS} ]]; then
    echo "failed."
    echo "Could not find an address for hostname, using DHCP instead."
    IS_DHCP=1
else
    echo "done."
    IS_DHCP=0
    if [[ -z ${gateway_address} ]]; then
        echo "No gateway specified and static IP selected. Failing..."
        exit 1
    fi
fi

if [[ ${IS_DHCP} -eq 0 ]]; then
    IPV4_NETMASK_STANDARD=$( netmask --standard ${gateway_address} | awk -F/ '{gsub(" ","",$2);print $2}' )
    IPV4_NETMASK_CIDR=$( netmask --cidr ${gateway_address} | awk -F/ '{gsub(" ","",$2);print $2}' )
    IPV4_GATEWAY=$( awk -F '/' '{ print $1 }' <<<"${gateway_address}" )
fi

case "${os_release}" in
    "Debian GNU/Linux")
        interface_content_dhcp="auto ${target_interface}
iface ${target_interface} inet dhcp"
        interface_content_static="auto ${target_interface}
iface ${target_interface} inet static
    address ${IPV4_ADDRESS}
    netmask ${IPV4_NETMASK_STANDARD}
    gateway ${IPV4_GATEWAY}"
    ;;
    "CentOS Linux")
        interface_content_dhcp="TYPE=\"Ethernet\"
PROXY_METHOD=\"none\"
BROWSER_ONLY=\"no\"
BOOTPROTO=\"dhcp\"
DEFROUTE=\"yes\"
IPV4_FAILURE_FATAL=\"no\"
IPV6INIT=\"yes\"
IPV6_AUTOCONF=\"yes\"
IPV6_DEFROUTE=\"yes\"
IPV6_FAILURE_FATAL=\"no\"
IPV6_ADDR_GEN_MODE=\"stable-privacy\"
NAME=\"${target_interface}\"
DEVICE=\"${target_interface}\"
ONBOOT=\"yes\"
IPV6_PRIVACY=\"no\""
        interface_content_static="TYPE=\"Ethernet\"
PROXY_METHOD=\"none\"
BROWSER_ONLY=\"no\"
BOOTPROTO=\"none\"
DEFROUTE=\"yes\"
IPV4_FAILURE_FATAL=\"no\"
IPV6INIT=\"yes\"
IPV6_AUTOCONF=\"yes\"
IPV6_DEFROUTE=\"yes\"
IPV6_FAILURE_FATAL=\"no\"
IPV6_ADDR_GEN_MODE=\"stable-privacy\"
NAME=\"${target_interface}\"
DEVICE=\"${target_interface}\"
ONBOOT=\"yes\"
IPADDR=\"${IPV4_ADDRESS}\"
PREFIX=\"${IPV4_NETMASK_CIDR}\"
GATEWAY=\"${IPV4_GATEWAY}\"
IPV6_PRIVACY=\"no\""
    ;;
esac

if [[ ${IS_DHCP} -eq 1 ]]; then
    echo -n "Creating DHCP-based public interface configuration... "
	interface_content="${interface_content_dhcp}"
else
    echo
    echo "IPv4 autodetect results:"
    echo "IPv4 public: ${IPV4_ADDRESS}/${IPV4_NETMASK_CIDR} -> ${IPV4_GATEWAY}"
    echo
    
    echo -n "Creating public network interface configuration... "
	interface_content="${interface_content_static}"
fi

cat <<EOF >${target_interface_file}
${interface_content}
EOF
echo "done."

if ! grep -q 'cloud-init' /usr/local/etc/pvc-manifest &>/dev/null; then
    echo -n "Removing bootstrap network interface configuration... "
    rm -f ${bootstrap_interface_file}
    rm -f /var/lib/dhcp/dhclient.${bootstrap_interface}.leases
    test -f /etc/dhcp/dhclient.conf.orig-${bootstrap_interface} || mv /etc/dhcp/dhclient.conf /etc/dhcp/dhclient.conf.orig-${bootstrap_interface}
    echo "done."
fi
