File Hawck-user_configurable_paths.patch of Package Hawck

From f75ab31101d2aeb3d71c10fd1a29974ae0bf56fb Mon Sep 17 00:00:00 2001
From: Luigi Baldoni <1450716+lgbaldoni@users.noreply.github.com>
Date: Wed, 27 Jan 2021 16:05:31 +0100
Subject: [PATCH] Add user-configurable paths (#51)

---
 bin/hawck-install.sh.in    | 61 ++++++++++++++++++++++++++++++++++++++
 bin/hawck-user-setup.sh.in | 50 +++++++++++++++++++++++++++++++
 bin/meson.build            | 19 ++++++++++++
 meson.build                | 21 ++++++++++++-
 meson_options.txt          | 20 +++++++++++++
 5 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100644 bin/hawck-install.sh.in
 create mode 100644 bin/hawck-user-setup.sh.in

diff --git a/bin/hawck-install.sh.in b/bin/hawck-install.sh.in
new file mode 100644
index 0000000..0ddc755
--- /dev/null
+++ b/bin/hawck-install.sh.in
@@ -0,0 +1,61 @@
+#!/bin/bash
+
+##
+## Install all Hawck files, this script is run by Meson.
+##
+
+function setup-files() {
+    pushd "${MESON_SOURCE_ROOT}" &>/dev/null
+
+    HAWCK_SHARE="$DESTDIR@HAWCK_SHARE_DIR@/hawck"
+    HAWCK_BIN="$HAWCK_SHARE/bin"
+    HAWCK_CFG="$DESTDIR@HAWCK_CFG_DIR@/hawck"
+    for src in src/scripts/*.sh; do
+        install -m 755 -D "$src" "$HAWCK_BIN/$(basename "$src")"
+    done
+    install -m 755 -D ${MESON_BUILD_ROOT}/bin/hawck-user-setup.sh "$HAWCK_BIN/hawck-user-setup"
+    install -m 755 -D src/tools/lskbd.rb "$HAWCK_BIN/lskbd"
+    install -m 644 -D bin/__UNSAFE_MODE.csv  "$HAWCK_SHARE/keys/__UNSAFE_MODE.csv"
+    install -m 644 -D bin/hawck-macrod.desktop "$HAWCK_SHARE/bin/hawck-macrod.desktop"
+    install -m 644 -D bin/cfg.lua "$HAWCK_CFG/cfg.lua"
+    install -m 644 -D src/macro-scripts/example.hwk "$HAWCK_CFG/scripts/example.hwk"
+
+    for lib in src/Lua/*.lua; do
+        install -m 644 -D "$lib" "$HAWCK_SHARE/LLib/$(basename "$lib")"
+    done
+    cp -r keymaps $HAWCK_SHARE/keymaps
+    cp -r icons $HAWCK_SHARE/icons
+
+    BIN="${DESTDIR}/${MESON_INSTALL_PREFIX}/bin"
+
+    install -m 755 src/scripts/hawck-add.sh "$BIN/hawck-add"
+    install -m 755 src/hwk2lua/hwk2lua.py "$BIN/hwk2lua"
+
+    ## Copy icons
+    pushd "icons" &>/dev/null
+    for res in 32 64 128 256 512; do
+        res2="$res"'x'"$res"
+        icon_src="alt_hawck_logo_v2_red_$res2.png"
+        if [ -f "$icon_src" ]; then
+            install -m 644 -D "$icon_src" "$DESTDIR${MESON_INSTALL_PREFIX}/share/icons/hicolor/$res2/apps/hawck.png"
+        fi
+    done
+    popd &>/dev/null
+
+    ## Install rules to make /dev/uinput available to uinput users
+    install -m 644 -D bin/99-hawck-input.rules "$DESTDIR@UDEV_RULES_DIR@/99-hawck-input.rules"
+
+    ## Make sure the uinput module is loaded, this isn't necessary on
+    ## some systems, but is required on Arch Linux. If this isn't done
+    ## the 99-hawck-input.rules file will have no effect.
+    install -m 644 -D bin/hawck-uinput.conf "$DESTDIR@MODULES_LOAD_DIR@/hawck-uinput.conf"
+
+    chmod -R a+r "$HAWCK_SHARE"
+    find "$HAWCK_SHARE" -type d -exec chmod a+x '{}' \;
+
+    install -m 644 -D LICENSE "${DESTDIR}${MESON_INSTALL_PREFIX}/share/licenses/hawck/LICENSE"
+
+    popd &>/dev/null ## Done installing files
+}
+
+setup-files
diff --git a/bin/hawck-user-setup.sh.in b/bin/hawck-user-setup.sh.in
new file mode 100644
index 0000000..1cdfc48
--- /dev/null
+++ b/bin/hawck-user-setup.sh.in
@@ -0,0 +1,50 @@
+#!/bin/bash
+
+if [ "$(whoami)" = "root" ]; then
+    echo "Will not run as root."
+    echo "You should run this setup script as the desktop user."
+    exit 1
+fi
+
+LOCAL_SHARE="${XDG_DATA_HOME:-$HOME/.local/share}"
+
+mkdir -p "$LOCAL_SHARE/hawck/scripts"
+mkdir -p "$LOCAL_SHARE/hawck/scripts-enabled"
+
+## Install example script if it does not exist.
+## The user may rewrite this script without renaming it, so the check
+## is critical.
+if ! [ -f "$LOCAL_SHARE/hawck/scripts/example.hwk" ]; then
+    install -m 644 -D @HAWCK_CFG_DIR@/hawck/scripts/example.hwk "$LOCAL_SHARE/hawck/scripts/example.hwk"
+fi
+if ! [ -f "$LOCAL_SHARE/hawck/cfg.lua" ]; then
+    install -m 644 -D @HAWCK_CFG_DIR@/hawck/cfg.lua "$LOCAL_SHARE/hawck/cfg.lua"
+fi
+
+LLIB="$LOCAL_SHARE/hawck/scripts/LLib"
+[ -e "$LLIB" ] || ln -s @HAWCK_SHARE_DIR@/hawck/LLib "$LLIB"
+KEYMAPS_LNK="$LOCAL_SHARE/hawck/scripts/keymaps"
+[ -e "$KEYMAPS_LNK" ] || ln -s @HAWCK_SHARE_DIR@/hawck/keymaps "$KEYMAPS_LNK"
+INIT_LNK="$LOCAL_SHARE/hawck/scripts/init.lua"
+[ -e "$INIT_LNK" ] || ln -s @HAWCK_SHARE_DIR@/hawck/LLib/init.lua "$INIT_LNK"
+
+
+if ! groups | tr " " "\n" | grep "^hawck-input-share\$"; then
+    notify-send --icon=hawck \
+                --app-name="Hawck" \
+                "You are not a member of the hawck-input-share group."
+
+    if ! pkexec usermod -aG hawck-input-share "$(whoami)"; then
+        notify-send --icon=hawck \
+            --app-name="Hawck" \
+            "Unable to authenticate, is a polkit authentication agent running?"
+        echo "Do you have a PolicyKit Authentication agent running?"
+        echo "Try running:"
+        echo "  nohup /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 &"
+        echo "  disown"
+    fi
+
+    notify-send --icon=hawck \
+                --app-name="Hawck" \
+                "You have been added to the hawck-input-share group, logging out and back in again is required"
+fi
diff --git a/bin/meson.build b/bin/meson.build
index 65faf78..b95682a 100644
--- a/bin/meson.build
+++ b/bin/meson.build
@@ -6,6 +6,11 @@ if get_option('prefix') == '/usr/local'
   systemd_prefix = '/etc/systemd'
 endif
 
+conf.set('HAWCK_CFG_DIR', get_option('hawck_cfg_dir'))
+conf.set('HAWCK_SHARE_DIR', hawck_full_share_dir)
+conf.set('MODULES_LOAD_DIR', get_option('modules_load_dir'))
+conf.set('UDEV_RULES_DIR', get_option('udev_rules_dir'))
+
 configure_file(
   input: 'hawck-inputd.service.in',
   output: 'hawck-inputd.service',
@@ -19,3 +24,17 @@ configure_file(
   install_dir: systemd_prefix / 'user',
   configuration: conf
 )
+
+configure_file(
+  input: 'hawck-user-setup.sh.in',
+  output: 'hawck-user-setup.sh',
+  configuration: conf
+)
+
+hawck_install_configured = configure_file(
+  input: 'hawck-install.sh.in',
+  output: 'hawck-install.sh',
+  configuration: conf
+)
+
+hawck_install_script = find_program(hawck_install_configured)
diff --git a/meson.build b/meson.build
index 68a04ac..4ef15d8 100644
--- a/meson.build
+++ b/meson.build
@@ -20,10 +20,29 @@ add_global_arguments('-DMESON_COMPILE=1', language : 'cpp')
 
 inc = include_directories('src')
 
+hawck_full_share_dir = join_paths(get_option('prefix'), get_option('hawck_share_dir'))
+
 subdir('src')
 subdir('tests')
 subdir('hawck-ui')
 subdir('docs')
 subdir('bin')
 
-meson.add_install_script('bin/hawck-install.sh')
+meson.add_install_script(hawck_install_script)
+
+# summary() instead of the message at the bottom would be neat, it's
+# available only in 0.53.0
+message(
+'\n------------------------------------------------------------------------\n'+
+'  '+meson.project_name()+' ' +meson.project_version()+':  Automatic configuration OK.'+'\n'+
+'\n  General configuration:'+'\n'+
+'\n    redirect_std: ................ '+get_option('redirect_std').to_string('yes', 'no')+
+'\n    use_meson_install: ........... '+get_option('use_meson_install').to_string('yes', 'no')+
+'\n    development_build: ........... '+get_option('development_build').to_string('yes', 'no')+'\n'+
+'\n  Installation paths:'+'\n'+
+'\n    prefix: ...................... '+get_option('prefix')+
+'\n    hawck_share_dir: ............. '+hawck_full_share_dir+
+'\n    hawck_cfg_dir: ............... '+get_option('hawck_cfg_dir')+
+'\n    modules_load_dir: ............ '+get_option('modules_load_dir')+
+'\n    udev_rules_dir: .............. '+get_option('udev_rules_dir')+'\n'
+)
diff --git a/meson_options.txt b/meson_options.txt
index d6e808d..e50555e 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -12,3 +12,23 @@ option('development_build',
        type : 'boolean',
        value : false,
        description : 'Whether or not to build Hawck for development.')
+
+option('hawck_share_dir',
+       type : 'string',
+       value : 'share',
+       description : 'Where to install Hawck data files.')
+
+option('hawck_cfg_dir',
+       type : 'string',
+       value : '/etc',
+       description : 'Where to install Hawck configuration files.')
+
+option('udev_rules_dir',
+       type : 'string',
+       value : '/etc/udev/rules.d',
+       description : 'Where to install Hawck udev rules file.')
+
+option('modules_load_dir',
+       type : 'string',
+       value : '/etc/modules-load.d',
+       description : 'Where to install Hawck modules file.')
-- 
2.20.1

openSUSE Build Service is sponsored by