#!/bin/bash

# Aktives Wallpaper ermitteln
active=$(hyprctl hyprpaper listactive | grep -E '^[^[:space:]]+ =' | cut -d'=' -f2 | xargs)

echo $active
# Geladene Wallpaper extrahieren
output=$(hyprctl hyprpaper listloaded)
wallpapers=()

while IFS= read -r line; do
    path=$(echo "$line" | cut -d',' -f2 | xargs)
    wallpapers+=("$path")
done <<<"$output"

# Aktives Wallpaper im Array suchen
active_index=-1
for i in "${!wallpapers[@]}"; do
    echo ${wallpapers[$i]}
    if [[ "${wallpapers[$i]}" == "$active" ]]; then
        active_index=$i
        break
    fi
done

# Nächstes Wallpaper setzen
echo nächstes WP= $wallpapers[$next_index]
if [[ $active_index -ge 0 ]]; then
    next_index=$(((active_index + 1) % ${#wallpapers[@]}))
    next_wallpaper="${wallpapers[$next_index]}"
    echo nächstes WP = $next_wallpaper
    hyprctl hyprpaper wallpaper "eDP-1, $next_wallpaper"
    wal -i $next_wallpaper
    pkill -SIGUSR2 waybar
else
    echo "Aktives Wallpaper wurde nicht gefunden."
fi
