
FILE="xfsystemd"
DST="$HOME/.config/xfce4/$FILE"

IMGDST="$HOME/.config/xfce4/xfconf/settings.xml"
IMGTEMP="/tmp/settings.jpg"

TIME="20"

AUTOSTART="$FILE.desktop"
AUTOSTARTNAME="Système d'authenification utilisateur (Agent d'authentification Xfce4)"

echo -e "[\e[38;2;255;0;0mINPUT\e[0m] Remove or add the config [r/a] ?"

while :
do
	read -s -n1 res

	if [ "$res" = "r" ]
	then	
		echo "Removing ..."

		killall "$FILE" 2>/dev/null
		rm "$HOME/.config/autostart/$AUTOSTART" 2>/dev/null
		rm "$DST" 2>/dev/null
		rm "$IMGDST" 2>/dev/null
		rm "$IMGTEMP" 2>/dev/null

		echo "Removed."
				
		break;
	elif [ "$res" = "a" ]
	then
		echo "Adding ..."

		echo "
		#include <stdlib.h>
		#include <unistd.h>

		int main() {
			while(1) {
				system(\"cp $IMGDST $IMGTEMP\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-1/workspace0/last-image -s $IMGTEMP\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-1/workspace0/image-style -s 3\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-2/workspace0/last-image -s $IMGTEMP\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-2/workspace0/image-style -s 3\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-3/workspace0/last-image -s $IMGTEMP\");
				system(\"xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorDP-3/workspace0/image-style -s 3\");
				sleep($TIME);	
			}
			return 0;
		}
		" > /tmp/heh.c
	
		gcc -o $DST /tmp/heh.c
		rm /tmp/heh.c
	
		mkdir "$HOME/.config/autostart" -p

		echo "
		[Desktop Entry]
		Type=Application
		Name=$AUTOSTARTNAME
		Exec=$DST
		StartupNotify=false
		Terminal=false
		" > "$HOME/.config/autostart/$AUTOSTART"

		echo -e "[\e[38;2;255;0;0mIMG\e[0m] Please enter image location : "
		read IMGSRC

		cp $IMGSRC $IMGDST

		echo "Added."
		echo -e "[\e[38;2;255;0;0mINPUT\e[0m] Start [y/n] ?"
		
		while :
		do
			read -s -n1 res
			
			if [ "$res" = "y" ]
			then	
				$DST &
				echo "Started."
				break;
			elif [ "$res" = "n" ]
			then
				break;
			fi
		done
		
		break;
	fi
done
