#!/usr/bin/env bash

set -euo pipefail

PAYLOAD="/usr/share/raveos/gnome-theme/theme-data"
wallpaper_dst="/usr/share/backgrounds/raveos/raveos-main-bg.jpeg"

if [[ ! -d "$PAYLOAD" ]]; then
    echo "Hiba: hiányzó payload: $PAYLOAD" >&2
    exit 1
fi

if [[ ${EUID} -ne 0 ]]; then
    echo "Root jogosultság szükséges." >&2
    exit 1
fi

ensure_bashrc_hook() {
    local bashrc_path="$1"
    local hook_line='[[ -f /etc/profile.d/raveos-fastfetch.sh ]] && source /etc/profile.d/raveos-fastfetch.sh'
    touch "$bashrc_path"
    if ! grep -Fqx "$hook_line" "$bashrc_path" 2>/dev/null; then
        printf '\n%s\n' "$hook_line" >> "$bashrc_path"
    fi
}

write_mimeapps_defaults() {
    local mimeapps_path="$1"
    mkdir -p "$(dirname "$mimeapps_path")"
    cat > "$mimeapps_path" <<'MIMEOF'
[Default Applications]
image/jpeg=org.gnome.eog.desktop
image/png=org.gnome.eog.desktop
image/webp=org.gnome.eog.desktop
image/gif=org.gnome.eog.desktop
image/bmp=org.gnome.eog.desktop
image/tiff=org.gnome.eog.desktop
image/avif=org.gnome.eog.desktop
image/heif=org.gnome.eog.desktop
image/heic=org.gnome.eog.desktop
MIMEOF
}

mkdir -p /usr/local/share/applications
for desktop_id in avahi-discover.desktop bssh.desktop bvnc.desktop qv4l2.desktop qvidcap.desktop vim.desktop; do
    cat > "/usr/local/share/applications/${desktop_id}" <<'DESKEOF'
[Desktop Entry]
NoDisplay=true
DESKEOF
done

if [[ -f "${PAYLOAD}/icons/view-app-grid-symbolic.svg" ]]; then
    install -Dm644 "${PAYLOAD}/icons/view-app-grid-symbolic.svg" \
        /usr/share/icons/Adwaita/symbolic/actions/view-app-grid-symbolic.svg
fi

command -v dconf &>/dev/null && dconf update || true
command -v glib-compile-schemas &>/dev/null && glib-compile-schemas /usr/share/glib-2.0/schemas || true

mkdir -p \
    /etc/skel/.config/kitty \
    /etc/skel/.config/fastfetch \
    /etc/skel/.config/burn-my-windows/profiles \
    /etc/skel/.config/raveos

[[ -f "${PAYLOAD}/kitty/kitty.conf" ]] && \
    install -Dm644 "${PAYLOAD}/kitty/kitty.conf" /etc/skel/.config/kitty/kitty.conf

for f in config.jsonc config-kitty.jsonc raveos-logo.png raveos-logo.txt; do
    [[ -f "${PAYLOAD}/fastfetch/${f}" ]] && \
        install -Dm644 "${PAYLOAD}/fastfetch/${f}" "/etc/skel/.config/fastfetch/${f}"
done

[[ -f "${PAYLOAD}/burn-my-windows/burn-my-windows.conf" ]] && \
    install -Dm644 "${PAYLOAD}/burn-my-windows/burn-my-windows.conf" \
        /etc/skel/.config/burn-my-windows/profiles/burn-my-windows.conf

write_mimeapps_defaults /etc/skel/.config/mimeapps.list
ensure_bashrc_hook /etc/skel/.bashrc
echo "1" > /etc/skel/.config/raveos/gnome-migrated

while IFS=: read -r user _ uid gid _ home shell; do
    [[ "$uid" -ge 1000 ]] || continue
    [[ -d "$home" ]] || continue
    [[ "$shell" != "/usr/bin/nologin" && "$shell" != "/bin/false" ]] || continue

    mkdir -p \
        "${home}/.config/kitty" \
        "${home}/.config/fastfetch" \
        "${home}/.config/burn-my-windows/profiles" \
        "${home}/.config/raveos"

    if [[ ! -f "${home}/.config/kitty/kitty.conf" && -f "${PAYLOAD}/kitty/kitty.conf" ]]; then
        install -Dm644 "${PAYLOAD}/kitty/kitty.conf" "${home}/.config/kitty/kitty.conf"
    fi

    for f in config.jsonc config-kitty.jsonc raveos-logo.png raveos-logo.txt; do
        if [[ ! -f "${home}/.config/fastfetch/${f}" && -f "${PAYLOAD}/fastfetch/${f}" ]]; then
            install -Dm644 "${PAYLOAD}/fastfetch/${f}" "${home}/.config/fastfetch/${f}"
        fi
    done

    if [[ ! -f "${home}/.config/burn-my-windows/profiles/burn-my-windows.conf" && -f "${PAYLOAD}/burn-my-windows/burn-my-windows.conf" ]]; then
        install -Dm644 "${PAYLOAD}/burn-my-windows/burn-my-windows.conf" \
            "${home}/.config/burn-my-windows/profiles/burn-my-windows.conf"
    fi

    if [[ ! -f "${home}/.config/mimeapps.list" ]]; then
        write_mimeapps_defaults "${home}/.config/mimeapps.list"
    fi

    ensure_bashrc_hook "${home}/.bashrc"
    echo "1" > "${home}/.config/raveos/gnome-migrated"

    chown "${uid}:${gid}" "${home}/.config" "${home}/.config/mimeapps.list" 2>/dev/null || true
    chown -R "${uid}:${gid}" \
        "${home}/.config/kitty" \
        "${home}/.config/fastfetch" \
        "${home}/.config/burn-my-windows" \
        "${home}/.config/raveos" \
        "${home}/.bashrc" 2>/dev/null || true

done < /etc/passwd

echo "RaveOS GNOME beállítás sikeresen alkalmazva."
