wallpaperctl/usr/local/bin/wallpaperctl

79 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
gversion="0"
pictureuri="picture-uri"
validate_config() {
if [ "$XDG_CURRENT_DESKTOP" != "GNOME" ]; then
quit "Error: GNOME is not detected. Please install GNOME."
fi
if [ -z "$WALLPAPER_DIR" ]; then
quit "Error: The wallpaper directory environment variable (WALLPAPER_DIR) has not been set."
fi
if [ ! -e "$WALLPAPER_DIR" ]; then
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" does not exist."
fi
if [ ! -d "$WALLPAPER_DIR" ]; then
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" is not a directory."
fi
if [ ! -r "$WALLPAPER_DIR" ]; then
quit "Error: The wallpaper directory \"$WALLPAPER_DIR\" is not accessible. "
fi
}
prepare() {
get_gnome_version
get_gnome_color_scheme
set_gnome_color_scheme
get_wallpaper
validate_wallpaper
}
get_gnome_version() {
if [ -r "/usr/share/gnome/gnome-version.xml" ]; then
gversion="$(grep -i "platform" /usr/share/gnome/gnome-version.xml | tr -d " <platform>" | tr -d "/")"
elif [ -e "/usr/bin/gnome-shell" ] && [ -x "/usr/bin/gnome-shell" ]; then
gversion="$(gnome-shell --version | cut -d' ' -f3 | cut -d'.' -f1)"
fi
}
get_gnome_color_scheme() {
if [ "$gversion" -ge 42 ]; then
style="$(gsettings get org.gnome.desktop.interface color-scheme)"
fi
}
set_gnome_color_scheme() {
if [ "$style" = "'prefer-dark'" ]; then
pictureuri="picture-uri-dark"
fi
}
get_wallpaper() {
selection="$(find "$WALLPAPER_DIR" -type f -name "*.jpg" -o -name "*.png" | shuf -n1)"
}
validate_wallpaper() {
if [ ! -f "$selection" ]; then
quit "Error: The wallpaper \"$WALLPAPER_DIR\" is not a file."
fi
if [ ! -r "$selection" ]; then
quit "Error: The wallpaper \"$WALLPAPER_DIR\" is not accessible."
fi
}
set_wallpaper() {
gsettings set org.gnome.desktop.background "$pictureuri" "file://$selection"
}
quit() {
if [[ "$1" = 0 ]]
then
exit 0
else
printf "%b\n" "$1"
exit 1
fi
}
validate_config
prepare
set_wallpaper
quit 0