#!/bin/bash

# Directory containing the image files
dir="/usr/share/wallpapers"

# detect if use dark theme
if [[ "$(dconf read /org/gnome/desktop/interface/color-scheme)" = "'prefer-dark'" ]]; then
    files=(
    "art face1.heic"
    "cat steampunk5.heic"
    "fluffy aliens.heic"
    "future planet.heic"
    "penguin steampunk2.heic"
    "cat cute2.heic"
    "space globe4.heic"
    "z non sense cat3.heic"
    "abstract natural2.heic"
    "art paint10.heic"
    "art paint48.heic"
    "glass or crystal.heic"
    "animal butterfly3.heic"
    "ambiance6.heic"
    "animal owl6.heic"
    "art random6.heic"

    )
else
    files=(
    "animal cute2.heic"
    "fluffy aliens.heic"
    "penguin art.heic"
    "cat cute2.heic"
    "future planet.heic"
    "abstract natural2.heic"
    "nature2.heic"
    "clean blue4.heic"
    "ambiance6.heic"
    "ambiance39.heic"
    "z nonsense4.heic"
    "glass or crystal9.heic"
    )
fi

# Array to store the existing files
existing_files=()

# Checks if the files exist and adds them to the 'existing_files' array
for file in "${files[@]}"; do
  if [ -e "$dir/$file" ]; then
    existing_files+=("$file")
  fi
done

# Checks if there are existing files
if [ ${#existing_files[@]} -eq 0 ]; then
  echo "No existing image files found."
  exit 1
fi

# Selects a random file from the 'existing_files' array
random_file="$dir/${existing_files[$RANDOM % ${#existing_files[@]}]}"


# apply as plasma wallpaper
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript "var allDesktops = desktops();print (allDesktops);for (i=0;i<allDesktops.length;i++) {d = allDesktops[i];d.wallpaperPlugin = \"org.kde.image\";d.currentConfigGroup = Array(\"Wallpaper\", \"org.kde.image\", \"General\");d.writeConfig(\"Image\", \"$random_file\");}"

# apply as lock screen
sed -i "s|^Image=.*|Image=$random_file|" ~/.config/kscreenlockerrc

sed -i "s|^Image=.*|Image=$random_file|" ~/.config/plasma-org.kde.plasma.desktop-appletsrc


# Displays the randomly selected file
echo "$random_file"
