#!/bin/bash
# Script to toggle pipewire on/off.
# Will also edit the desktop-session startup if present.
# Createdy by PPC, 5/7/2023, for antiX-23, full GPL license
# Edited by abc-nix folowing anti-apXos' suggestions

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

# pipewire is running?
PIPEWIRE_STATUS="false"
pidof -q pipewire && PIPEWIRE_STATUS="true"

edit_startup () {
	PIPEWIRE_STATUS="${1}"
	STARTUP_FILE="$HOME/.desktop-session/startup"
	PIPEWIRE_COMMAND="pidof -q pipewire || pipewire"
	
	# return if the file doesn't exist
	[ ! -f "$STARTUP_FILE" ] && return 1
	
	# dealing with pipewire-start
	if grep -q "pipewire-start" "$STARTUP_FILE"; then
		# comment it out if not already
		sed -i '/^pipewire-start/s/^/#/g' "$STARTUP_FILE"
		# Replace pipewire-start with pipewire & only if pipewire doesn't exist
		if ! grep -q "pipewire &" "$STARTUP_FILE"; then
			sed -i -e "/^#pipewire-start/s/^#.*/#${PIPEWIRE_COMMAND} \&/" "$STARTUP_FILE"
		fi
	fi
	
	# If pipewire doesn't exist in the startup file, add it
	if ! grep -q "pipewire &" "$STARTUP_FILE"; then
		echo "$PIPEWIRE_COMMAND &" >> "$STARTUP_FILE"
	fi
	
	# comment or uncomment the pipewire command
	if $PIPEWIRE_STATUS; then
		if ! grep -q "^#.*pipewire &" "$STARTUP_FILE"; then
			sed -i '/pipewire &/s/^/#/g' "$STARTUP_FILE"
		fi
	else
		sed -i '/pipewire &/s/^#//g' "$STARTUP_FILE"
	fi
}

# This function is run by root and not by user. Beware!
alsa_plus_pipewire () {
	# symlink pipewire configs for alsa if not already there
	if [ ! -e "/etc/alsa/conf.d/50-pipewire.conf" ]; then
		if [ -f "/usr/share/alsa/alsa.conf.d/50-pipewire.conf" ]; then
			ln -s "/usr/share/alsa/alsa.conf.d/50-pipewire.conf" "/etc/alsa/conf.d/50-pipewire.conf"
		fi
	fi
	if [ ! -e "/etc/alsa/conf.d/99-pipewire-default.conf" ]; then
		if [ -f "/usr/share/alsa/alsa.conf.d/99-pipewire-default.conf" ]; then
			ln -s "/usr/share/alsa/alsa.conf.d/99-pipewire-default.conf" "/etc/alsa/conf.d/99-pipewire-default.conf"
		fi
	fi
	
	# restart alsa
	alsactl -F nrestore
	
	exit 0
}

# This function is run by root and not by user. Beware!
alsa_minus_pipewire () {
	# remove pipewire config symlinks for alsa if they exist
	if [ -e "/etc/alsa/conf.d/50-pipewire.conf" ]; then
		rm "/etc/alsa/conf.d/50-pipewire.conf"
	fi
	
	if [ -e "/etc/alsa/conf.d/99-pipewire-default.conf" ]; then
		rm "/etc/alsa/conf.d/99-pipewire-default.conf"
	fi
	
	# restart alsa
	alsactl -F nrestore
	
	exit 0
}

main_switch () {
	PIPEWIRE_STATUS="${1}"
	if $PIPEWIRE_STATUS; then
		#echo "remove pipewire from alsa conf"
		{ gksudo "toggle_pipewire --remove-alsa-conf"; } || return 1
		# Fix xmms and cmus config to use alsa
		[ -w $HOME/.xmms/config ] && sed -i 's/libxmms-pulse.so/libALSA.so/' $HOME/.xmms/config
		[ -w $HOME/.config/cmus/rc ] && sed -i 's/output_plugin=pulse/output_plugin=alsa/' $HOME/.config/cmus/rc
		# Stop pipewire
		pkill wire
	else
		#echo "add pipewire to alsa conf"
		{ gksudo "toggle_pipewire --add-alsa-conf"; } || return 1
		# Fix xmms and cmus config to use pulse
		[ -w $HOME/.xmms/config ] && sed -i 's/libALSA.so/libxmms-pulse.so/' $HOME/.xmms/config
		[ -w $HOME/.config/cmus/rc ] && sed -i 's/output_plugin=alsa/output_plugin=pulse/' $HOME/.config/cmus/rc
		# start pipewire
		pipewire &
	fi
	sleep 1
	# edit startup
	edit_startup "$PIPEWIRE_STATUS"
	# manage volumeicon
	if [ -x /usr/bin/volumeicon ]; then
		VI_CONF="$HOME/.config/volumeicon/volumeicon"
		VI_MIXER="urxvt -e alsamixer"
		# Replace volumeicon onclick command
		if [ -f "$VI_CONF" ] && grep -q "onclick=" "$VI_CONF"; then
			if [ -x /usr/bin/pavucontrol ] && ! $PIPEWIRE_STATUS; then
				VI_MIXER="pavucontrol"
			fi
			sed -i -e "/^onclick=/s/=.*/=${VI_MIXER}/" "$VI_CONF"
		fi
		# restart volumeicon
		{ pkill volumeicon && sleep 5 && volumeicon & } &
	fi
	toggle_pipewire
}

main_window () {
	if $PIPEWIRE_STATUS; then
		tooltip=$"PipeWire audio server IS currently on the antiX startup and WILL start automatically the next time you restart antiX"
		switch="/usr/share/pixmaps/on.png"
	else
		tooltip=$"PipeWire audio server IS NOT currently on the antiX startup and WILL NOT start automatically the next time you restart antiX"
		switch="/usr/share/pixmaps/off.png"
	fi
	slider_text=$"PipeWire"
	export PIPEWIRE_STATUS
	export -f main_switch edit_startup
	yad --form --undecorated --on-top \
	--title='Generic Toggle for antiX' --center --columns=1 \
	--window-icon="/usr/share/icons/papirus-antix/48x48/actions/cm_options.png" \
	--field="$slider_text!$switch!$tooltip":BTN "bash -c 'main_switch $PIPEWIRE_STATUS'" \
	--button='x':1 --undecorated --borders=10 --width=250
}


# Check if advance option (only root)
if [[ $EUID -eq 0 ]]; then
	case $1 in
		--add-alsa-conf) alsa_plus_pipewire
			;;
		--remove-alsa-conf) alsa_minus_pipewire
			;;
	esac
fi

#Export functions, so they can be used from inside other functions
export -f main_window main_switch edit_startup

#Check if pipewire exists. If it does not, then probably pipewire is not installed: Warn user and exit the script:
if [ ! -x /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

# Close window if it already exists
pkill -f "Generic Toggle for antiX"

#Run main window
main_window

exit
