From 07c2eee890ab6e9df6c50f909090e50700714dff Mon Sep 17 00:00:00 2001
From: Fabio Manganiello <fabio@manganiello.tech>
Date: Mon, 4 Sep 2023 02:19:13 +0200
Subject: [PATCH] Changed (fixed) default location for config dir if not
 existing.

Following some common UNIX conventions, if no configuration file is
specified and none exists under the default locations, then a new
configuration directory should be created under:

```
- if root: /etc/platypush
- else:
  - if XDG_CONFIG_HOME:
    - $XDG_CONFIG_HOME/platypush
  - else:
    - ~/.config/platypush
```
---
 platypush/config/__init__.py | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py
index 28c6beaaa..fce5fe92c 100644
--- a/platypush/config/__init__.py
+++ b/platypush/config/__init__.py
@@ -20,6 +20,7 @@ from platypush.utils import (
     is_functional_procedure,
     is_functional_hook,
     is_functional_cron,
+    is_root,
 )
 
 
@@ -212,11 +213,24 @@ class Config:
 
     def _create_default_config(self):
         cfg_mod_dir = os.path.dirname(os.path.abspath(__file__))
-        cfgfile = self._cfgfile_locations[0]
+        # Use /etc/platypush/config.yaml if the user is running as root,
+        # otherwise ~/.config/platypush/config.yaml
+        cfgfile = (
+            (
+                os.path.join(os.environ['XDG_CONFIG_HOME'], 'config.yaml')
+                if os.environ.get('XDG_CONFIG_HOME')
+                else os.path.join(
+                    os.path.expanduser('~'), '.config', 'platypush', 'config.yaml'
+                )
+            )
+            if not is_root()
+            else os.path.join(os.sep, 'etc', 'platypush', 'config.yaml')
+        )
+
         cfgdir = pathlib.Path(cfgfile).parent
         cfgdir.mkdir(parents=True, exist_ok=True)
-        for cfgfile in glob.glob(os.path.join(cfg_mod_dir, 'config*.yaml')):
-            shutil.copy(cfgfile, str(cfgdir))
+        for cf in glob.glob(os.path.join(cfg_mod_dir, 'config*.yaml')):
+            shutil.copy(cf, str(cfgdir))
 
         return cfgfile