From 01462445005b1ba3a35a82c1d34fc4f3d98aefc5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= <felix@piedallu.me>
Date: Sun, 5 Nov 2023 23:07:21 +0100
Subject: [PATCH] Create log files at install and restore steps

---
 scripts/install | 10 ++++++++--
 scripts/restore |  8 +++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/scripts/install b/scripts/install
index 8c717e2..d85f2ee 100755
--- a/scripts/install
+++ b/scripts/install
@@ -26,7 +26,7 @@ source /usr/share/yunohost/helpers
 # ...
 
 #
-# $app is the app id (i.e. 'example' for first install, 
+# $app is the app id (i.e. 'example' for first install,
 # or 'example__2', '__3', ... for multi-instance installs)
 #
 
@@ -44,7 +44,7 @@ ynh_script_progression --message="Setting up source files..." --weight=1
 # Download, check integrity, uncompress and patch the source from manifest.toml
 ynh_setup_source --dest_dir="$install_dir"
 
-# $install_dir will automatically be initialized with some decent 
+# $install_dir will automatically be initialized with some decent
 # permission by default ... however, you may need to recursively reapply
 # ownership to all files such as after the ynh_setup_source step
 chown -R $app:www-data "$install_dir"
@@ -96,6 +96,12 @@ ynh_add_systemd_config
 
 yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log"
 
+# Create the log file (required for fail2ban)
+mkdir -p "/var/log/$app"
+touch "/var/log/$app/$app.log"
+chown -R "$app:$app" "/var/log/$app"
+chmod -R u=rwX,g=rX,o= "/var/log/$app"
+
 ### Additional options starting with 3.8:
 ###
 ### --needs_exposed_ports "$port" a list of ports that needs to be publicly exposed
diff --git a/scripts/restore b/scripts/restore
index e60cb7a..97c290c 100755
--- a/scripts/restore
+++ b/scripts/restore
@@ -17,7 +17,7 @@ ynh_script_progression --message="Restoring the app main directory..." --weight=
 
 ynh_restore_file --origin_path="$install_dir"
 
-# $install_dir will automatically be initialized with some decent 
+# $install_dir will automatically be initialized with some decent
 # permissions by default ... however, you may need to recursively reapply
 # ownership to all files such as after the ynh_setup_source step
 chown -R $app:www-data "$install_dir"
@@ -57,6 +57,12 @@ systemctl enable $app.service --quiet
 
 yunohost service add $app --description="A short description of the app" --log="/var/log/$app/$app.log"
 
+# Create the log file (required for fail2ban)
+mkdir -p "/var/log/$app"
+touch "/var/log/$app/$app.log"
+chown -R "$app:$app" "/var/log/$app"
+chmod -R u=rwX,g=rX,o= "/var/log/$app"
+
 ynh_restore_file --origin_path="/etc/logrotate.d/$app"
 
 ynh_restore_file --origin_path="/etc/fail2ban/jail.d/$app.conf"