#!/bin/bash

# omarchy:summary=Rename this computer, as it shows up on the network
# omarchy:args=[<name>]
# omarchy:examples=omarchy setup hostname | omarchy setup hostname workshop

set -euo pipefail

if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
  echo "Usage: omarchy-setup-hostname [<name>]"
  echo
  echo "Without a name, asks for one, starting from the current hostname."
  exit 0
fi

name=${1:-}
while true; do
  if [[ -z $name ]]; then
    name=$(gum input --no-show-help --value "$(uname -n)" --prompt "$(omarchy-i18n setupForm.hostnamePrompt)")
  fi

  # The same rule the installer's setup form holds hostnames to.
  [[ $name =~ ^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?$ ]] && break
  omarchy-i18n setupForm.hostnameInvalid >&2
  (($#)) && exit 1
  name=""
done

old=$(uname -n)
sudo hostnamectl set-hostname "$name"
new=$(uname -n)

# A Chromium-based browser refuses to start on a profile whose SingletonLock, a
# symlink to <host>-<pid>, names another host, so the locks follow the rename.
config="${XDG_CONFIG_HOME:-$HOME/.config}"
for lock in "$config"/*/SingletonLock "$config"/*/*/SingletonLock; do
  holder=$(readlink "$lock" 2>/dev/null) || continue
  if [[ $holder =~ ^"$old"-([0-9]+)$ ]]; then
    ln -sfn "$new-${BASH_REMATCH[1]}" "$lock" || true
  fi
done
