#!/bin/env bash
#
## Setup network and update hostname

# Wait for WiFi
echo -n "Waiting for network... "
sleep 3s
echo "Done"

# Set hostname
echo -n "Updating hostname... "
IP="$(ip a show scope global \
  | grep inet \
  | head -1 \
  | sed -r 's#.*inet ([0-9]+.[0-9]+.[0-9]+.[0-9]+.)/.*#\1#')"
if [[ "${IP:+x}" ]]; then
  NEW_HOSTNAME="$(dig +noall +answer +short -x "$IP" \
    | grep -v ';' \
    | head -1 \
    | sed 's/\.$//')"
fi
if [[ "${NEW_HOSTNAME:+x}" ]]; then
  sudo hostnamectl set-hostname "${NEW_HOSTNAME}"
fi
echo "Done"

