#!/bin/bash

# this script is part of mx-system-sounds and governed by that license
# mxlinux.org
# author:  dolphin_oracle

#command to check and see if sounds are enabled
#xfconf-query -c xsettings -p /Net/EnableEventSounds 
#command to check sound theme name
#xfconf-query -c xsettings -p /Net/SoundThemeName
#command to find desktop-login sound for a given theme
#find  /usr/share/sounds/theme-name/ |grep desktop-login


#check for sound server utility

if [ ! -x /usr/bin/pactl ]; then
	exit 1
fi

max_duration=60 # max duration to wait for wireplumber
elapsed=0

#wait for pulseaudio or pipewire to start
until pactl info |grep -q PulseAudio 2>/dev/null ; do
        sleep 1
        elapsed=$((elapsed + 1))
        if [ "elapsed" -ge "$max_duration"; then
                exit 1
        fi
done

customsound=""

configfile="$HOME/.config/MX-Linux/mx-system-sounds/startupsound.conf"
conffile="$HOME/.config/MX-Linux/mx-system-sounds/mx-login-logout_sounds.conf"

if [ -e "$conffile" ]; then

	customsound=$(cat "$configfile" 2>/dev/null)
	#echo custom sound is $customsound

	startup_enabled=$(grep startup "$conffile" |cut -d '=' -f2)
	#echo sound status is $startup_enabled

	soundthemename=$(xfconf-query -c xsettings -p /Net/SoundThemeName)
	#echo sound theme is $soundthemename

	defaultsound=$(find  /usr/share/sounds/$soundthemename/ |grep desktop-login)
	#echo default sound is $defaultsound

fi

if [ "$customsound" = "" ]; then
	if [ "$startup_enabled" = "true" ]; then
		play "$defaultsound" 
		exit 0
	else
		exit 0
	fi
else
	if [ "$startup_enabled" = "true" ]; then
		play "$customsound" 
		exit 0
	else
		exit 0
	fi
fi

exit 0
