aboutsummaryrefslogtreecommitdiffstats
path: root/bin/udt-machine-name
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-29 10:42:29 +0200
committerDanilo M. <danix@danix.xyz>2026-09-29 10:42:29 +0200
commit4645998b64815d15aef6efe00dcbdfc4a045359e (patch)
tree89665a54e8afcfc63c6ec4749c041d440432eaa6 /bin/udt-machine-name
parentdab41433688ff2247020d7a6fe12ac2140a75fb5 (diff)
downloadunified-desktop-theme-4645998b64815d15aef6efe00dcbdfc4a045359e.tar.gz
unified-desktop-theme-4645998b64815d15aef6efe00dcbdfc4a045359e.zip
feat(bin): udt-machine-name writes PRETTY_HOSTNAME to /etc/machine-info
The GRUB theme titles its panel with the machine's pretty name, and on Slackware nothing writes /etc/machine-info: hostnamectl is systemd's. This sets the one key, keeping any others, and with no argument prints it. The value is double-quoted with only \ and " escaped, the escapes systemd's reader and the GRUB build's shlex agree on; control characters are refused. It needs root and is never run by install.sh, which only suggests it in the GRUB output while no name is set. tests/machine-name.sh checks the quoting round-trip against shlex. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'bin/udt-machine-name')
-rwxr-xr-xbin/udt-machine-name40
1 files changed, 40 insertions, 0 deletions
diff --git a/bin/udt-machine-name b/bin/udt-machine-name
new file mode 100755
index 0000000..1a677eb
--- /dev/null
+++ b/bin/udt-machine-name
@@ -0,0 +1,40 @@
+#!/bin/bash
+# Set or show this machine's pretty hostname, PRETTY_HOSTNAME in
+# /etc/machine-info. hostnamectl is systemd's writer; Slackware has neither,
+# and the GRUB theme reads the name for its panel title.
+# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+# Licensed under the GNU General Public License v2 only.
+#
+# udt-machine-name print the current name
+# sudo udt-machine-name "My Box" set it
+set -euo pipefail
+
+file="${UDT_MACHINE_INFO:-/etc/machine-info}"
+
+if [ $# -eq 0 ]; then
+ [ -f "$file" ] && sed -n 's/^PRETTY_HOSTNAME=//p' "$file"
+ exit 0
+fi
+if [ $# -ne 1 ] || [ -z "$1" ]; then
+ echo "usage: $(basename "$0") [NAME]" >&2
+ exit 2
+fi
+case "$1" in
+ *[[:cntrl:]]*) echo "name must not contain control characters" >&2; exit 2 ;;
+esac
+
+# Double-quoted, with only \ and " escaped: the escapes both systemd's reader
+# and the GRUB build's shlex understand the same way.
+value="$1"
+value="${value//\\/\\\\}"
+value="${value//\"/\\\"}"
+line="PRETTY_HOSTNAME=\"$value\""
+
+# Every other key in the file (ICON_NAME, CHASSIS, ...) is kept as it was.
+tmp="$(mktemp "$file.XXXXXX")"
+trap 'rm -f "$tmp"' EXIT
+{ [ -f "$file" ] && grep -v '^PRETTY_HOSTNAME=' "$file" || true; echo "$line"; } > "$tmp"
+chmod 644 "$tmp"
+mv "$tmp" "$file"
+trap - EXIT
+echo "$line"