#!/bin/bash

# Display current hostname
current_hostname=$(hostname)
echo "Current hostname: $current_hostname"
echo

# Suggest options or prompt for input
echo "Choose a new hostname:"
PS3="Enter the number or type your own custom hostname: "
options=("web01" "db01" "storage01" "k3s-node" "Custom")

select opt in "${options[@]}"; do
    if [[ "$opt" == "Custom" ]]; then
        read -rp "Enter custom hostname: " new_hostname
        break
    elif [[ -n "$opt" ]]; then
        new_hostname=$opt
        break
    else
        echo "Invalid choice. Try again."
    fi
done

# Confirm choice
echo "You selected: $new_hostname"
read -rp "Apply new hostname? [y/N]: " confirm

if [[ "$confirm" =~ ^[Yy]$ ]]; then
    hostnamectl set-hostname "$new_hostname"
    echo "127.0.1.1   $new_hostname" >> /etc/hosts
    echo "Hostname changed to $new_hostname. You may need to re-login for prompt update."
else
    echo "Hostname change aborted."
fi
