#!/usr/local/bin/bash
#
# Copyright (c) 2016 Joyent Inc., All rights reserved.
# This script checks new network interfaces for and enables via DHCP at boot.

# load common functions and vars
. /usr/local/lib/smartdc/common.lib

nic_added=n

for device in $(ifconfig | grep flags | cut -d: -f1); do
  if [[ "lo" == "${device:0:2}" ]]; then
    continue
  fi

  grep "ifconfig_$device=" /etc/rc.conf &>/dev/null
  if [[ $? -ne 0 ]]; then
    lib_triton_info "New interface found: $device. Adding to /etc/rc.conf"
    echo "ifconfig_$device=\"DHCP\" # Added by triton" >> /etc/rc.conf

    nic_added=y
  fi
done

if [[ "$nic_added" == "y" ]]; then
  lib_triton_info "Restarting /etc/rc.d/netif"
  /etc/rc.d/netif restart
fi

exit 0
