#!/bin/sh
# Waybar click commands run without a terminal. Keep Python/GTK tracebacks in a
# per-user log instead of silently hiding a startup error.
set -u
: "${XDG_RUNTIME_DIR:?A logged-in graphical session is required}"
exec 9>"$XDG_RUNTIME_DIR/cyberpunk-apps.lock"
flock -n 9 || exit 0
state_dir="${XDG_STATE_HOME:-$HOME/.local/state}/cyberpunk-apps"
mkdir -p "$state_dir" || exit 1
log="$state_dir/apps.log"
if /usr/bin/python3 /usr/lib/cyberpunk-apps/cyberpunk-apps.py >>"$log" 2>&1; then
    exit 0
else
    status=$?
    printf 'Cyberpunk Apps exited with code %s.\n' "$status" >>"$log"
    /usr/bin/notify-send -u critical 'Could not open Apps' \
        "See $log, or use Super+Space to search." || true
    exit "$status"
fi
