#!/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

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

#Get elevated privileges, via GUI
gksudo "Pipewire/Alsa"

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
   
   	##Enable pipewire sound server
	#1- Create new links to the ALSA pipewire config files
	sudo ln -s /usr/share/alsa/alsa.conf.d/50-pipewire.conf /etc/alsa/conf.d/50-pipewire.conf
	sudo ln -s /usr/share/alsa/alsa.conf.d/99-pipewire-default.conf /etc/alsa/conf.d/99-pipewire-default.conf
	#2- Re-init ALSA card 0 for pipewire
	alsactl init 0
	#3- Start pipewire (which handles starting of pipewire-pulse and wireplumber, see ~/.config/pipewire/pipewire.conf)
	pipewire &
	#4. Restarting volumeicon
	pkill volumeicon 
	sleep 5 && volumeicon &
   
#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 --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/#&/' ~/.desktop-session/startup
    
    ## Disable pipewire, restore "Pure Alsa sound server"
	#1.kill pipewire processes
	pkill wire
	#2. Removing pipewire related files from /etc/alsa/conf.d/
	sudo rm /etc/alsa/conf.d/50-pipewire.conf /etc/alsa/conf.d/99-pipewire-default.conf
	#3. Restarting alsa
	sudo alsactl -f nrestore
	#4. Restarting volumeicon
	pkill volumeicon
	volumeicon &
    
		
#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 --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 ~/.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
