#!/bin/bash

# Copyright 2016, DELL EMC, Inc.
set -e

# Source debconf library.
. /usr/share/debconf/confmodule

# Please specific if you are will to set dhcp
db_input high RackHD/WILLING_TO_SETUP_RACKHD || true
db_go || true

db_get RackHD/WILLING_TO_SETUP_RACKHD
if [ "$RET" != "true" ]; then
    db_stop
    exit 0
fi

# Please specific the name of control NIC port of RackHD(example : eth1)  >
db_input high RackHD/NIC_NAME || true
db_go || true

db_get RackHD/NIC_NAME
NIC_Name=$RET

NIC_IP=$( ifconfig $NIC_Name | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' )

if [ "$NIC_IP" != "172.31.128.1" ]; then
    # Please specific if you are will to set ip by force
    sudo echo "\
    auto $NIC_Name
    iface $NIC_Name inet static
    address 172.31.128.1
    netmask 255.255.252.0" >> /etc/network/interfaces
    echo "[INFO] will restart your $NIC_Name..."
    ifdown $NIC_Name
    ifup $NIC_Name
    New_IP=$(ifconfig $NIC_Name | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
    echo "Your $NIC_Name new IP address is $New_IP"
fi

# Update DHCP config
sudo echo "
##### RackHD added lines #####
deny duplicates;
ignore-client-uids true;
subnet 172.31.128.0 netmask 255.255.240.0 {
   range 172.31.128.2 172.31.143.254;
   # Use this option to signal to the PXE client that we are doing proxy DHCP
   option vendor-class-identifier \"PXEClient\";
}
" >> /etc/dhcp/dhcpd.conf

db_stop
