#!/usr/bin/env sh

# SPDX-FileCopyrightText: 2023-2026 UnionTech Software Technology Co., Ltd.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

# shellcheck shell=sh

# Systemd environment generator for linglong/linyaps
# Injects entries/bin into PATH and the XDG_DATA_DIRS generation script,
# then prints the modified environment variables to stdout.
# systemd runs this generator for the system manager; user sessions inherit
# the resulting environment when they start (e.g. via user@.service).

# Inject entries/bin into PATH (append, not prepend, to avoid shadowing
# system commands like git/python with wrapper scripts).
_linglong_bin_dir="/var/lib/linglong/entries/bin"
if [ -n "${_linglong_bin_dir}" ] && [ -d "${_linglong_bin_dir}" ]; then
    case ":${PATH}:" in
        *":${_linglong_bin_dir}:"*) ;; # Already in PATH
        *) PATH="${PATH:+${PATH}:}${_linglong_bin_dir}" ;;
    esac
fi
unset _linglong_bin_dir

source_script="/usr/lib/linglong/generate-xdg-data-dirs.sh"

# Source the script and export XDG_DATA_DIRS if successful
[ -r "${source_script}" ] && . "${source_script}"

# !!!systemd requires environment variables to be printed to standard output!!!
# https://www.freedesktop.org/software/systemd/man/latest/systemd.environment-generator.html
[ -n "${PATH}" ] && echo "PATH=${PATH}"
[ -n "${XDG_DATA_DIRS}" ] && echo "XDG_DATA_DIRS=${XDG_DATA_DIRS}"
