#!/bin/bash

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Simple Background Menu: sbgmenu is a dmenu interface for viewing
# images and/or selecting a new wallpaper from the user's "Pictures"
# directory.  This script is part of my dotfiles and is primarily
# intended for use in my custom BSPWM session:
# https://gitlab.com/protesilaos/dotfiles
#
# Dependencies on Debian Buster:
# 	sudo apt install suckless-tools
#
# For BSPWM sessions, you also need:
# 	sudo apt install feh
#
# Optional dependency (binary is fdfind):
# 	sudo apt install fd-find
#
# Assign this script to a key binding for your convenience.

# TODO is "Pictures" automatically localised?
# TODO consider adding notify-send actions

# pass custom colours to dmenu command
source "$HOME/.local/share/my_colours/active-tempus-theme.sh"

# get the system-wide custom font
source "$HOME/.local/share/my_custom_ui_font.sh"

# the customised `dmenu` command.  Accepts the prompt's text as the
# first argument and the height as a second.  If the latter is not
# defined, then dmenu displays options horizontally.
my_dmenu() {
	local prompt="$1"
	local height="$2"

	if [ -n "$2" ]; then
		dmenu -i -p "$prompt" -nb "$background" -nf "$foreground" \
		-sb "$color13" -sf "$background" -fn "$my_custom_ui_font" -l "$2"
	else
		dmenu -i -p "$prompt" -nb "$background" -nf "$foreground" \
		-sb "$color13" -sf "$background" -fn "$my_custom_ui_font"
	fi
}

# Here we ask the user whether they want to just view the selected
# image, or set it as the new wallpaper.
sbgmenu_mode="$(echo -e 'View images\nSet desktop wallpaper' | my_dmenu 'Choose the sbgmenu mode')"

find_pictures() {
	if [ "$(command -v fdfind 2> /dev/null)" ]; then
		fdfind 'jpg|jpeg|png' "$HOME/Pictures/"
	else
		find "$HOME/Pictures" -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \)
	fi
}

# Get image files that end in .{jpg,jpeg,png} from ~/Pictures and
# present them as dmenu items.
my_pictures_menu() {
	find_pictures | my_dmenu "$1" "$2"
}

# Act depending on the mode of action.
sbgmenu_action() {
	# If the option is to view an image, use the default image viewer
	# (user defined).
	if [ "$sbgmenu_mode" == 'View images' ]; then
		sbgmenu_output="$(my_pictures_menu 'View selected picture' 10)"
		echo "Opening file"
		xdg-open "$sbgmenu_output"
	# If the decision is to set a wallpaper, prepare the relevant
	# command based on the running session.  Note that the bspwm session
	# is not standardised.  Using `feh` is my way of doing things.
	elif [ "$sbgmenu_mode" == 'Set desktop wallpaper' ]; then
		sbgmenu_output="$(my_pictures_menu 'Set new desktop wallpaper' 10)"

		case "$DESKTOP_SESSION" in
			bspwm)
				# NOTE there is no standard way for defining a wallpaper in
				# BSPWM.  Using a hidden file from the home dir is specific
				# to my custom desktop session.
				#
				# Copy the selected image to the home directory, as a hidden
				# file.
				#
				# Dependency on Debian Buster:
				# 	sudo apt install feh
				cp -f "$sbgmenu_output" "$HOME/.wallpaper.jpg"
				feh --bg-fill "$HOME/.wallpaper.jpg"
				;;
			gnome)
				gsettings set org.gnome.desktop.background picture-uri "file://$sbgmenu_output"
				;;
			mate)
				gsettings set org.mate.background picture-filename "$sbgmenu_output"
				;;
			xfce)
				# TODO any way to define a wallpaper in Xfce without
				# specifying screen, monitor, workspace?
				xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/workspace0/last-image -s "$sbgmenu_output"
				;;
			*)
				echo "ERROR. Unknown Desktop Session."
				echo "Cannot set wallpaper."
				echo "Aborting..."
				exit 1
				;;
		esac

		echo "Changing wallpaper to $sbgmenu_output"
	# If neither of the available modes is selected, abort sbgmenu
	else
		exit 1
	fi
}

# Run the script
sbgmenu_action
