#!/bin/bash -xe

if [ -z "$PROVISIONING_IP" ]; then
    echo "ERROR: PROVISIONING_IP environment variable unset."
    exit 1
fi

if [ -z "$PROVISIONING_INTERFACE" ]; then
  # If no provisioning interface is specified, then we're probably in
  # the mode where the provisioning network is optional, so to avoid a
  # hard dependency of forcing users to specify the interface, we detect
  # which network interface has the IP's from the machine network.
  IP_ONLY=$(echo "$PROVISIONING_IP" | cut -d/ -f1)

  # See if we already have the IP on an interface
  PROVISIONING_INTERFACE=$(ip -j addr | jq -r -c ".[].addr_info[] | select(.local == \"$IP_ONLY\") | .label")

  # If not, let's figure out what interface it should go on
  if [ -z "$PROVISIONING_INTERFACE" ]; then
    PROVISIONING_INTERFACE=$(ip -j route get "$IP_ONLY" | jq -r '.[] | select(.dev != "lo") | .dev')
  fi
else
  # Get rid of any DHCP addresses only if we have a dedicated
  # provisioning interface
  /usr/sbin/ip address flush dev "$PROVISIONING_INTERFACE"
fi

if [ -z "$PROVISIONING_INTERFACE" ]; then
  ip addr show
  echo "ERROR: Could not find suitable interface for \"$PROVISIONING_IP\""
  exit 1
fi

# Need this to be long enough to bring up the pod with the ip refresh in it.
# The refresh-static-ip container should lower this back to 10 seconds once it starts.
# The only time this will actually be set for 5 minutes is if the containers fail to come up.
/usr/sbin/ip addr add "$PROVISIONING_IP" dev "$PROVISIONING_INTERFACE" valid_lft 300 preferred_lft 300
