#!/usr/bin/env bash
apt remove -y --purge pulseaudio pulseaudio-module-x11 pulseaudio-utils pavucontrol gstreamer1.0-pulseaudio pacman pavumeter pavucontrol

DEST=${1:-/etc/skel}

# Assume clean install
#kill -9 `ps aux | grep -v grep | grep start-pulseaudio | awk {'print $2'}`
#kill -9 `ps aux | grep -v grep | grep pulseaudio | awk {'print $2'}`

#bash -c "rm /etc/asound.conf"
#rm $HOME/.pulse-cookie
#rm -r $HOME/.config/pulse
#rm -rf /tmp/pulse*

apt install -y alsa-tools alsa-tools-gui alsa-utils alsa-oss alsamixergui alsa-firmware-loaders volumeicon-alsa libalsaplayer0 bluez-alsa-utils bluez-firmware
apt install -y pnmixer blueman

/etc/init.d/alsa-utils restart

#kill -HUP `ps aux | grep -v grep | grep pnmixer | awk {'print $2'}`
#sed -i 's,^\(VolumeControlCommand=\).*,\1'xfce4-mixer',' $HOME/.config/pnmixer/config
#pnmixer &

cat >/etc/udev/rules.d/00_alsa-usb.rules <<'EOF'
# Default to using additional (USB) sound cards when they are available.
KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"
EOF

# see also: https://panther.kapsi.fi/posts/2018-11-17_linux_bluetooth_audio

cat >/etc/init.d/bluealsa <<'EOF'
#!/bin/sh
### BEGIN INIT INFO
# Provides: bluez-alsa

# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6

# Required-Start:	$syslog $local_fs $remote_fs bluetooth 
# Required-Stop:	$syslog $local_fs $remote_fs 
# Short-Description: Bluealsa daemon
### END INIT INFO

. /lib/lsb/init-functions
prog=bluez-alsa
if test -f -/etc/default/bluez-alsa; then
	. -/etc/default/bluez-alsa 
fi

PIDFILE=/var/run/$prog.pid
DESC="Bluealsa daemon"
start() {
	log_daemon_msg "Starting $DESC" "$prog"
	if start_daemon -p $PIDFILE /usr/bin/bluealsa $OPTIONS; then
		log_end_msg 0
	else
		log_end_msg 1
	fi
}

stop() {
	log_daemon_msg "Stopping $DESC" "$prog"
	killproc -p $PIDFILE /usr/bin/bluealsa
	if [ $? -ne 0 ]; then
		log_end_msg 1
		exit 1
	fi
	if [ $? -eq 0 ]; then
		log_end_msg 0
	fi
}

force_reload() {
	stop
	start

}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	force-reload)
		force_reload
		;;
	restart)
		stop
		start
		;;

	*)
		echo "$Usage: $prog {start|stop|force-reload|restart}"
		exit 2
esac
EOF

DEVICE="XX:XX:XX:XX:XX:XX"

cat >${HOME}/.asoundrc <<EOF
#
#pcm.SoundCoreMini {
#        type bluealsa
#        device "${DEVICE}"
#        profile "a2dp"
#        hint { show on description "Sound Core Mini"}
#}
EOF

# Usage example
# mpv --audio-device=alsa/SoundCoreMini 'Sweet Georgia Brown 1.m4a'

## Existing asoundrc
#cat ~/.asoundrc
#SRV="org.bluealsa"
#DEV="16:01:06:35:14:79"
#PROFILE="a2dp"
#DELAY=20000
#BAT="yes"
#
## BlueALSA integration setup
#
#defaults.bluealsa.service $SRV
#defaults.bluealsa.profile $PROFILE
##defaults.bluealsa.delay $DELAY
#defaults.bluealsa.delay 20000
#defaults.bluealsa.battery $BAT
#defaults.bluealsa.device $DEV
#
### /etc/alsa/conf.d/20-bluealsa.conf
##ctl.bluealsa {
##	@args [ SRV BAT ]
##	@args.SRV {
##	        type string
##	        default {
##		                @func refer
##		                name defaults.bluealsa.service
##	        }
##	}
##	@args.BAT {
##	        type string
##	        default {
##		                @func refer
##		                name defaults.bluealsa.battery
##	        }
##	}
##	type bluealsa
##	service $SRV
##	battery $BAT
##}
##
##pcm.bluealsa {
##	@args [ SRV DEV PROFILE DELAY ]
##	@args.SRV {
##	        type string
##	        default {
##		                @func refer
##		                name defaults.bluealsa.service
##	        }
##	}
##	@args.DEV {
##	        type string
##	        default {
##		                @func refer
##		                name defaults.bluealsa.device
##	        }
##	}
##	@args.PROFILE {
##	        type string
##	        default {
##		                @func refer
##		                name defaults.bluealsa.profile
##	        }
##	}
##	@args.DELAY {
##	        type integer
##	        default {
##		                @func refer
##		                name defaults.bluealsa.delay
##	        }
##	}
##	type plug
##	slave.pcm {
##	        type bluealsa
##	        service $SRV
##	        device $DEV
##	        profile $PROFILE
##	        delay $DELAY
##	}
##	hint {
##	        show {
##		                @func refer
##		                name defaults.namehint.extended
##	        }
##	        description "Bluetooth Audio Hub"
##	}
##}
