From: Peymaneh <peymaneh@posteo.net>
Date: Fri, 23 Dec 2022 21:20:44 +0100
Subject: postinst/postrm: do not leave directories on purge

---
 dist/scripts/postinstall.sh | 7 +++++++
 dist/scripts/postremove.sh  | 3 ++-
 2 files changed, 9 insertions(+), 1 deletion(-)

--- a/dist/scripts/postinstall.sh
+++ b/dist/scripts/postinstall.sh
@@ -19,6 +19,13 @@
 		usermod -aG www-data caddy
 	fi
 
+        # handle cases where package was installed and then purged;
+        # user and group will still exist but with no home dir
+        if [ ! -d /var/lib/caddy ]; then
+                mkdir -p /var/lib/caddy
+                chown caddy:caddy /var/lib/caddy
+        fi
+
 	# Add log directory with correct permissions
 	if [ ! -d /var/log/caddy ]; then
 		mkdir -p /var/log/caddy
--- a/dist/scripts/postremove.sh
+++ b/dist/scripts/postremove.sh
@@ -19,4 +19,5 @@
 		deb-systemd-helper unmask caddy.service >/dev/null || true
 		deb-systemd-helper unmask caddy-api.service >/dev/null || true
 	fi
-fi
\ No newline at end of file
+        rm -rf /var/lib/caddy /var/log/caddy /etc/caddy
+fi
