#!/bin/bash
set -e
case "$1" in
    configure)
        CALLER="${SUDO_USER:-}"
        if [ -n "$CALLER" ] && [ "$CALLER" != "root" ]; then
            su - "$CALLER" -c \
                "pipx install 'yazses[desktop]' 2>/dev/null || pipx upgrade yazses"
            su - "$CALLER" -c "pipx ensurepath"
            # The packaged unit's ExecStart is /usr/bin/yazses-daemon but pipx installs into
            # the user's ~/.local/bin; without these links the service crash-loops (203/EXEC).
            CALLER_HOME="$(getent passwd "$CALLER" | cut -d: -f6)"
            for bin in yazses yazses-daemon; do
                if [ -x "$CALLER_HOME/.local/bin/$bin" ] && [ ! -e "/usr/bin/$bin" ]; then
                    ln -s "$CALLER_HOME/.local/bin/$bin" "/usr/bin/$bin"
                fi
            done
        fi
        echo ""
        echo "YazSes installed. Complete setup (once per user):"
        echo "  sudo usermod -aG input \$USER   # keyboard access"
        echo "  systemctl --user enable --now yazses.service"
        ;;
esac
#DEBHELPER#
