#!/bin/bash

# wayland wallpapers - 'wwals'

# choose a wallpaper
# (c) Michael Amadio (@01micko) 2021 GPL (/usr/share/doc/legal)
# this changes walls for wayland wlroots compositors
# requires 'swaybg', 'wlr-randr', 'gtkdialog', 'bash'
# thanks to @johanmalm for testing and input

# setup config #------------------
rm -f /tmp/buttons 2>/dev/null # in case of a crash
BGDIR=${BGDIR:-/usr/share/backgrounds}
export CONFDIR=${XDG_CONFIG_HOME:-$HOME/.config}/wwals
[ -d "$CONFDIR" ] || mkdir -p $CONFDIR
CONF=$CONFDIR/wwals.conf
if [ ! -f "$CONF" ];then
	echo "BGDIR=$BGDIR" > $CONF
fi
. $CONF
export CMD_FILE=$CONFDIR/cmd
export BGDIR

## setup vars #------------------
a=$(wlr-randr | while read l m n o p
do [ "${p:1:9}" = 'preferred' ] && echo $l && break
done)
NUM_MONS=1
MONITOR=
# for gui on main monitor
SCRX=${a%x*}
SCRY=${a#*x}

# get number of monitors and output names
MONITORS="$(while read -r MON REST
do
	case "${MON:0:3}" in
		[0-9]*|Phy|Ena|Mod|Pos|Tra|Sca)continue;;
	esac
	echo $MON
done <<<$(printf "$(wlr-randr)"))"

echo $MONITORS
e=0
NUM_MONS=$(for i in $MONITORS;do e=$(($e + 1));done;echo $e)

# bail out if in portrait (tablets)
[ $SCRX -lt $SCRY ] && echo '<window  title="Background Choice" width-request="400" resizable="false">
	<vbox>
	<hbox space-expand="true" space-fill="true">
		<pixmap icon_size="6">
			<input file stock="gtk-dialog-warning"></input>
		</pixmap>
		<text space-expand="true" space-fill="true">
			<label>Please switch back into Landscape mode to choose a Wallpaper</label>
		</text>
	</hbox>
	<hbox>
		<button ok></button>
	</hbox>
	</vbox>
	</window>'|gtkdialog -s -c && exit

export W=$((7 * $SCRX / 10)) #70%
export H=$((6 * $SCRY / 10)) #60%
export PH=$(($H - 70))

## functions #------------------
set_wall() { # single wallpaper
	wall="${BGDIR}/$@"
	# build command
	printf %s "swaybg -i \"$wall\" >/dev/null 2>&1 &" > $CMD_FILE # so compositor can set bg at start up
}

set_multi() { # diferent wallpapers per monitor
	echo "$1"
	# build command
	echo -n "--output $2 -i \"$BGDIR/$1\" " >> $CMD_FILE
}

multimon() {
	export Q='<window  title="Multiple Monitors" width-request="400" resizable="false">
	<vbox>
	<hbox space-expand="true" space-fill="true">
		<pixmap icon_size="6">
			<input file stock="gtk-dialog-info"></input>
		</pixmap>
		<text space-expand="true" space-fill="true" use-markup="true">
			<label>"You have '$1' monitors connected. Would you like the same background on each of them? If you choose <b>No</b> then you will be given the option to use different backgrounds on each screen."</label>
		</text>
	</hbox>
	<hbox>
		<button yes></button><button no></button>
	</hbox>
	</vbox>
	</window>'
	eval `gtkdialog -p Q -c`
	echo $EXIT
	case $EXIT in
		Yes)return 1;;
		No)return 0;;
		*)exit 0;;
	esac
}

button_gui() {
	# populate main gui
	WALLS=$(ls -1 $BGDIR)
	c=1
	while read WALL
	  do 
		echo '<button image-position="top" tooltip-text="Click to set '"${WALL##*/}"' as wallpaper!">
	        <label>'"$WALL"'</label>
			<variable>'"BG${c}"'</variable>
			<input file>'$BGDIR'/'"$WALL"'</input>
			<height>'$PH'</height>
			<action>'$1 "\"$WALL\"" $2'</action>
			<action type="exit">exit</action>
	      </button>' >> /tmp/buttons
	      c=$((${c}+1))
	  done <<<$WALLS
}

main_gui() {
BUTTONS=`cat /tmp/buttons`
export GUI='<window title="Background Choice '$1'">
  <hbox height-request="'$H'" width-request="'$W'">
    <hbox scrollable="true">
      '$BUTTONS'
    </hbox>
  </hbox>
</window>'
gtkdialog -p GUI -c || printf "$GUI" # printf for debug
rm -f /tmp/buttons
}

export -f set_wall
export -f set_multi
export -f multimon
export -f button_gui
export -f main_gui

# main #------------------
SET_CMD=set_wall

if [ $NUM_MONS -gt 1 ]; then
	if multimon $NUM_MONS ; then
		echo $?
		SET_CMD=set_multi
		echo -n 'swaybg ' > $CMD_FILE
		for MONITOR in $(echo $MONITORS); do
			button_gui $SET_CMD $MONITOR
			main_gui " - Monitor: $MONITOR"
		done
		if [ -e "$CMD_FILE" ]; then
			echo -n ' >/dev/null 2>&1 &' >> $CMD_FILE
		fi
	else
		button_gui $SET_CMD
		main_gui
	fi
else
	button_gui $SET_CMD
	main_gui
fi
if [ -e "$CMD_FILE" ]; then
	killall swaybg >/dev/null 2>&1
	. $CMD_FILE
else
	exit 1
fi
exit 0
