#!/bin/bash
#Script to toggle on/off pipewire, from the startup file (and reboot after doing that), createdy by PPC, 5/7/2023, for antiX-23, full GPL license

#Automatically get localized string for the word "reboot", from the translattion for "desktop-session-exit"
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=desktop-session-exit
label=$"Reboot"
echo $label > /tmp/reboot_button_text

#Set localization for the script
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=toggle_pipewire

main_window_on(){
#Make sure any previously existing window of this kind is killed, or they will stack up and fill the RAM	
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.1
	
reboot_button=$(cat /tmp/reboot_button_text)
tooltip=$"PipeWire audio server IS currently on the antiX startup and WILL start automatically the next time you restart antiX"
slider_text=$"PipeWire"
	#Close any previously running window for this GUI:
xdotool search "Generic Toggle for antiX" windowclose
	export -f main_window_off
switch="/usr/share/pixmaps/on.png"

#What the script should do when the switch is toggle to "on"
    #not found, this means that pipewire entry is commented out or missing from the startup file. Enable it at startup, uncommenting it's line:
    sed -i '/pipewire-start/s/^#//g' ~/.desktop-session/startup

	#check if pipewire is present on the startup file, if not, it's because the user removed it (and so, the previous command will do nothing). Append it to the end of the startup file (volume icon will not work automatically with PipeWire??? then)
    if ! grep -q pipewire-start ~/.desktop-session/startup;	then
		echo pipewire-start &>> ~/.desktop-session/startup
	fi
   
#Main window

yad --form --undecorated\
	--title='Generic Toggle for antiX' --center --window-icon="/usr/share/icons/papirus-antix/48x48/actions/cm_options.png"\
	--columns=1 \
	--field="$slider_text!$switch!$tooltip":BTN "bash -c main_window_off " \
	--button='x':1   --button="${reboot_button}":desktop-session-exit --undecorated --borders=10 --width=250        
	
}

main_window_off(){
#Make sure any previously existing window of this kind is killed, or they will stack up and fill the RAM	
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.1
	
reboot_button=$(cat /tmp/reboot_button_text)
tooltip=$"PipeWire audio server IS NOT currently on the antiX startup and WILL NOT start automatically the next time you restart antiX"
slider_text="PipeWire"
		#Close any previously running window for this GUI:
xdotool search "Generic Toggle for antiX" windowclose
		export -f main_window_on
switch="/usr/share/pixmaps/off.png"
		
#What the script should do when the switch is toggle to "off"
 #pipewire is loading at the startup. Will disable that, commenting its line:
    sed -i 's/^pipewire-start/#&/' ~/.desktop-session/startup
		
#Main window
yad --form --undecorated\
	--title='Generic Toggle for antiX' --center --window-icon="/usr/share/icons/papirus-antix/48x48/actions/cm_options.png"\
	--columns=1 \
	--field="$slider_text!$switch!$tooltip":BTN "bash -c main_window_on " \
	--button='x':1  --button="$reboot_button":desktop-session-exit --undecorated --borders=10 --width=250   
	
}

#Export functions, so they can be used from inside other functions
export -f main_window_off main_window_on

#Read the current state of what the GUI toogles on/off and show the relevant Main Window (with the switch toggled to on or to off): 
#Check if antiX pipewire script exists. If it does not, then probably pipewire is not installed: Warn user and exit the script:
if [ ! -f /usr/bin/pipewire ]; then
    yad --center --title="Pipewire" --window-icon=/usr/share/icons/papirus-antix/24x24/categories/multimedia-volume-control.png --text=$"PipeWire seems to not be installed" --button=" x "
    exit
fi

#Check if the startup file has a line that starts with pipewire-start (i.e.- pipewire-start is not commented out) and act acordingly
if grep -q ^pipewire-start ~/.desktop-session/startup; then
  main_window_on

 else
 
 main_window_off
	
fi

#Kill all yad windows of this kind and exit
eval kill $(ps aux| grep "Generic Toggle for antiX"| grep -v "grep" | awk '{print $2}')
sleep 0.5
#xdotool search "Generic Toggle for antiX" windowclose
exit
