From 064e992a7aec8916a2bc65e5279f65bab4464140 Mon Sep 17 00:00:00 2001
From: Dennis Camera <dennis.camera@ssrq-sds-fds.ch>
Date: Sun, 6 Oct 2019 21:03:49 +0200
Subject: [PATCH] [__hostname] Support openSUSE and replace printf with echo

---
 .../type/__hostname/explorer/persistent_hostname     | 12 ++++++------
 cdist/conf/type/__hostname/gencode-remote            | 12 +++++++-----
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/cdist/conf/type/__hostname/explorer/persistent_hostname b/cdist/conf/type/__hostname/explorer/persistent_hostname
index 6bc0786f..ec52bbd2 100755
--- a/cdist/conf/type/__hostname/explorer/persistent_hostname
+++ b/cdist/conf/type/__hostname/explorer/persistent_hostname
@@ -31,12 +31,9 @@ fi
 # Fall back to alternative OS-specific approaches.
 case $("$__explorer/os")
 in
-	alpine|archlinux|coreos|debian|devuan|suse|ubuntu)
+	alpine|archlinux|coreos|debian|devuan|ubuntu)
 		[ -f /etc/hostname ] && cat /etc/hostname
 		;;
-	suse)
-		[ -f /etc/HOSTNAME ] && cat /etc/HOSTNAME
-		;;
 	centos|fedora|redhat|scientific)
 		[ -f /etc/sysconfig/network ] \
 			&& (. /etc/sysconfig/network && echo "$HOSTNAME")
@@ -44,6 +41,9 @@ in
 	freebsd)
 		(. /etc/rc.conf && echo "$hostname")
 		;;
+	macosx)
+		scutil --get HostName 2>/dev/null
+		;;
 	netbsd)
 		if grep -q '^hostname=' /etc/rc.conf
 		then
@@ -57,7 +57,7 @@ in
 	openbsd)
 		cat /etc/myname
 		;;
-	macosx)
-		scutil --get HostName 2>/dev/null
+	suse)
+		cat /etc/HOSTNAME 2>/dev/null
 		;;
 esac
diff --git a/cdist/conf/type/__hostname/gencode-remote b/cdist/conf/type/__hostname/gencode-remote
index b258165d..37ace1b7 100755
--- a/cdist/conf/type/__hostname/gencode-remote
+++ b/cdist/conf/type/__hostname/gencode-remote
@@ -75,20 +75,22 @@ fi
 case $os
 in
     archlinux|debian|ubuntu|devuan|coreos|alpine)
-        printf "printf '%%s\\\\n' '$name_should' > /etc/hostname\\n"
+        echo "printf '%s\\n' '$name_should' > /etc/hostname"
         echo "hostname -F /etc/hostname"
     ;;
     centos|fedora|redhat|scientific|freebsd|openbsd)
         # NOTE: Writing the hostname to file is done in the manifest.
         echo "hostname '$name_should'"
     ;;
-    suse)
-        echo "hostname '$name_should'"
-        printf "printf '%%s\\\\n' '$name_should' > /etc/HOSTNAME\\n"
-    ;;
     macosx)
         echo "scutil --set HostName '$name_should'"
     ;;
+    suse)
+        # We write into /etc/HOSTNAME for backwards-compatibility.  Modern SuSE
+        # has hostnamectl anyway and symlinks /etc/HOSTNAME to /etc/hostname.
+        echo "printf '%s\\n' '$name_should' > /etc/HOSTNAME"
+        echo 'hostname -F /etc/HOSTNAME'
+    ;;
     *)
         echo "echo 'Unsupported OS: $os' >&2; exit 1"
     ;;