#!/bin/bash
########################################################
#               Mate Random Background                 #   
# Simple gui to change backgrounds in timed intervals  # 
#------------------------------------------------------#
#       © 2022 WTFPL lxgator@gmail.com 	               #
########################################################
VERSION='1.2'

ICON='/usr/share/icons/hicolor/48x48/apps/random-background.png'
DIR=$HOME/'Wallpapers'
CONF="$HOME/.random-background"

if ! test -d $HOME/Wallpapers  ; then
mkdir -p $HOME/Wallpapers
notify-send -t 10000 -i $ICON "Add background images in ~/Wallpapers"
fi
if ! test -d $HOME/.config/autostart  ; then
mkdir -p $HOME/.config/autostart
fi
if ! test -d $CONF ; then
mkdir -p $CONF
fi
if ! test -f $CONF/"Random Autostart".desktop ; then
`echo \
'[Desktop Entry]
Name=Random Autostart
Comment=Change backgrounds in your desktop 
Exec=.random-background/AutoStart
Terminal=false
Type=Application
Icon=random-background
StartupNotify=true
Categories=GTK;Graphics;
Name[en_US]=Random Autostart' | awk '{print $0}' > $CONF/"Random Autostart".desktop`
chmod a+x $CONF/"Random Autostart".desktop
fi
if ! test -f $CONF/menu ; then
# Create a file for menu
`echo \
"Change!sh -c /usr/share/random-background/data/change!/usr/share/icons/hicolor/16x16/apps/random-background.png
Autostart!/home/ken/.random-background/autostart!/usr/share/icons/mate/16x16/apps/preferences-desktop-theme.png
About!/usr/share/random-background/data/about!/usr/share/icons/mate/16x16/apps/help-browser.png
Quit!bash -c 'LEFT_CLICK'!/usr/share/icons/mate/16x16/actions/process-stop.png" \
| awk '{print $0}' > $CONF/menu`
fi
# Create a autostart file
if ! test -f $CONF/AutoStart ; then
# Create a file for menu
touch $CONF/AutoStart
chmod a+x $CONF/AutoStart
fi
if ! test -f $CONF/autostart ; then
`echo \
'#! /usr/bin/gtkdialog -f
<window window_position="3" title="" icon-name="random-background" allow-grow="false">
<vbox spacing="5">
<text>
<label>"Autostart Preference"</label>
</text>
<button>
<input file icon="gtk-add"></input>
<label>10 Min</label>
<action>
echo "#!/bin/bash /usr/share/random-background/data/random10" | awk 1 RS=" |\n" > "$HOME/.random-background/AutoStart" | ln -s $HOME/.random-background/"Random Autostart".desktop $HOME/.config/autostart | notify-send -t 6000 -i "random-background" "Random Background" "Autostart set at 10 minutes" | chmod a+x "$HOME/.random-background/AutoStart" | killall "autostart"&</action>
</button>
<button>
<input file icon="gtk-add"></input>
<label>30 Min</label>
<action>
echo "#!/bin/bash /usr/share/random-background/data/random30" | awk 1 RS=" |\n" > "$HOME/.random-background/AutoStart" | ln -s $HOME/.random-background/"Random Autostart".desktop $HOME/.config/autostart | notify-send -t 6000 -i "random-background" "Random Background" "Autostart set at 30 minutes" | chmod a+x "$HOME/.random-background/AutoStart" | killall "autostart"&</action>
</button>
<button>
<input file icon="gtk-add"></input>
<label>1 Hour</label>
<action>echo "#!/bin/bash /usr/share/random-background/data/random60" | awk 1 RS=" |\n" > "$HOME/.random-background/AutoStart" | ln -s $HOME/.random-background/"Random Autostart".desktop $HOME/.config/autostart | notify-send -t 6000 -i "random-background" "Random Background" "Autostart set at 1 hour" | chmod a+x "$HOME/.random-background/AutoStart" | killall "autostart"&</action>
</button>
<button>
<input file icon="gtk-add"></input>
<label>2 Hours</label>
<action>echo "#!/bin/bash /usr/share/random-background/data/random120" | awk 1 RS=" |\n" > "$HOME/.random-background/AutoStart" | ln -s $HOME/.random-background/"Random Autostart".desktop $HOME/.config/autostart | notify-send -t 6000 -i "random-background" "Random Background" "Autostart set at 2 hours" | chmod a+x "$HOME/.random-background/AutoStart" | killall "autostart"&</action>
</button>
<button>
<input file icon="gtk-delete"></input>
<label>Remove</label>
<action>rm -f "$HOME/.random-background/AutoStart" | rm -f $HOME/.config/autostart/"Random Autostart".desktop | notify-send -t 6000 -i "random-background" "Random Background" "Autostart is stopped" | killall "autostart"&</action>
</button>
<button>
<input file icon="gtk-quit"></input>
<label>Close</label>
</button>
</vbox>
</window>' | awk '{print $0}' > $CONF/autostart`
chmod a+x $CONF/autostart
fi
while [ $? = 0 ]; 
do
rm -f  $CONF/menutray.*  > /dev/null 2>&1
KL=`cat $CONF/pid | awk 'NR==1{print $0}'`
GET_MENU="$CONF/menu"
function START_MENU () {
MENU_ITEMS="`cat "$GET_MENU" | tr '\n' '|' | sed '$s/.$//'`"
TRAY_ICON='/usr/share/icons/hicolor/16x16/apps/random-background.png'
POPUP_TEXT="Random Background"
PIPE_FIFO=$(mktemp -u $CONF/menutray.XXXXXXXX)
function LEFT_CLICK () {
  exec 3<> $PIPE_FIFO
  echo "quit" >&3
kill -9 $KL 2>/dev/null  
pkill -SIGKILL random-background 2>/dev/null
pkill -SIGKILL random10 2>/dev/null
pkill -SIGKILL random30 2>/dev/null
pkill -SIGKILL random60 2>/dev/null
pkill -SIGKILL random120 2>/dev/null
pkill -SIGKILL change 2>/dev/null
}
export -f LEFT_CLICK
export PIPE_FIFO
mkfifo $PIPE_FIFO
exec 3<> $PIPE_FIFO
GUI=$(yad --notification --no-middle --icon-size=24 --kill-parent --listen \
--image="$TRAY_ICON" \
--text="$POPUP_TEXT" <&3 ) &
echo "menu:$MENU_ITEMS" >&3
}
if [ -z "$GET_MENU" ]; then
 echo "traymenu usage: $0 $GET_MENU" && exit
else
START_MENU
fi
exit 0
done





