#!/bin/bash

set -e

if [ -z "$1" ]; then
    echo "Usage: $0 <new-hostname>"
    exit 1
fi

NEW_HOSTNAME=$1
OLD_HOSTNAME=$(hostname)

echo ""
echo "Changing hostname to '$NEW_HOSTNAME'..."

sudo hostnamectl set-hostname "$NEW_HOSTNAME"

sudo sed -i "s/$OLD_HOSTNAME/$NEW_HOSTNAME/g" /etc/hosts

echo ""
echo "Hostname has been changed to: $(hostname)"
