#!/usr/bin/env bash

set -euo pipefail

PAYLOAD="/usr/share/raveos/plasma-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
}

disable_kmix_autostart() {
    local target_home="$1"
    mkdir -p "${target_home}/.config/autostart"
    cat > "${target_home}/.config/autostart/kmix_autostart.desktop" <<'KMEOF'
[Desktop Entry]
Hidden=true
KMEOF
}

write_konsolerc() {
    local target_home="$1"
    cat > "${target_home}/.config/konsolerc" <<'KSEOF'
[Desktop Entry]
DefaultProfile=RaveOS.profile

[General]
ConfigVersion=1

[UiSettings]
ColorScheme=RaveOS
KSEOF
}

write_kscreenlockerrc() {
    local target_home="$1"
    mkdir -p "${target_home}/.config"
    cat > "${target_home}/.config/kscreenlockerrc" <<KLEOF
[Greeter][Wallpaper][org.kde.image][General]
FillMode=2
Image=file://${wallpaper_dst}
KLEOF
}

write_ksplashrc() {
    local target_home="$1"
    mkdir -p "${target_home}/.config"
    cat > "${target_home}/.config/ksplashrc" <<'SPLEOF'
[KSplash]
Engine=KSplashQML
Theme=org.kde.raveos.desktop
SPLEOF
}

write_kdeglobals() {
    local target_home="$1"
    mkdir -p "${target_home}/.config"
    cat > "${target_home}/.config/kdeglobals" <<'GLBEOF'
[General]
ColorScheme=BreezeDark

[Icons]
Theme=breeze-dark

[KDE]
widgetStyle=Breeze
GLBEOF
}

# skel konfiguráció új felhasználóknak
mkdir -p \
    /etc/skel/.config/kitty \
    /etc/skel/.config/fastfetch \
    /etc/skel/.config/autostart \
    /etc/skel/.config/raveos \
    /etc/skel/.local/share/konsole

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

[[ -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}/konsole/RaveOS.colorscheme" ]] && \
    install -Dm644 "${PAYLOAD}/konsole/RaveOS.colorscheme" /etc/skel/.local/share/konsole/RaveOS.colorscheme
[[ -f "${PAYLOAD}/konsole/RaveOS.profile" ]] && \
    install -Dm644 "${PAYLOAD}/konsole/RaveOS.profile" /etc/skel/.local/share/konsole/RaveOS.profile

write_konsolerc /etc/skel
write_kscreenlockerrc /etc/skel
write_ksplashrc /etc/skel
write_kdeglobals /etc/skel
disable_kmix_autostart /etc/skel
ensure_bashrc_hook /etc/skel/.bashrc
echo "1" > /etc/skel/.config/raveos/plasma-migrated

# meglévő felhasználók frissítése
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/autostart" \
        "${home}/.config/raveos" \
        "${home}/.local/share/konsole"

    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}/.local/share/konsole/RaveOS.colorscheme" && -f "${PAYLOAD}/konsole/RaveOS.colorscheme" ]]; then
        install -Dm644 "${PAYLOAD}/konsole/RaveOS.colorscheme" "${home}/.local/share/konsole/RaveOS.colorscheme"
    fi

    if [[ ! -f "${home}/.local/share/konsole/RaveOS.profile" && -f "${PAYLOAD}/konsole/RaveOS.profile" ]]; then
        install -Dm644 "${PAYLOAD}/konsole/RaveOS.profile" "${home}/.local/share/konsole/RaveOS.profile"
    fi

    if [[ ! -f "${home}/.config/konsolerc" ]]; then
        write_konsolerc "${home}"
    fi

    if [[ ! -f "${home}/.config/autostart/kmix_autostart.desktop" ]]; then
        disable_kmix_autostart "${home}"
    fi

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

    chown "${uid}:${gid}" "${home}/.config" "${home}/.local" 2>/dev/null || true
    chown -R "${uid}:${gid}" \
        "${home}/.config/kitty" \
        "${home}/.config/fastfetch" \
        "${home}/.config/autostart" \
        "${home}/.config/raveos" \
        "${home}/.local/share/konsole" \
        "${home}/.bashrc" 2>/dev/null || true

done < /etc/passwd

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