#!/bin/sh

export TEXTDOMAIN=wlrclose
export OUTPUT_CHARSET=UTF-8

[ ! "`which wlrctl 2>/dev/null`" ] && Xdialog --title "$(gettext 'Alert')" --beep \
 --msgbox "\n  $(gettext 'Please install wlrctl')  \n" 0 0 && exit 0

Xdialog --title "$(gettext 'Alert')" --cancel-label "$(gettext 'No')" \
 --ok-label "$(gettext 'Yes')" --yesno "\n   $(gettext 'Close all desktop applications ?')   \n" 0 0
if [[ $? == 0 ]]; then
	[ -f $HOME/.config/autostart/spacefm.desktop ] && SPACEFM_DESKTOP=yes
	WIN_LIST=$(wlrctl window list | cut -d':' -f1 | sed -e '$a\gtkdialog' -e 's/firefox.*$/firefox/g')
	for i in $WIN_LIST; do
		kill -s 15 `pgrep $i`
		sleep 0.1
	done
	sleep 0.5
	[ -n "$SPACEFM_DESKTOP" ] && spacefm --desktop &
fi
