#!/usr/bin/sh
#
# $Id: initialize,v 1.10 2026-07-26 18:49:17+05:30 Cprogrammer Exp mbhangui $
#
servicedir="/service"
home=$(getent passwd 1000|awk -F: '{print $6}')
[ ! -d $home/.audio_selector ] && mkdir -p $home/.audio_selector
cd $home/.audio_selector
for i in restart update initialize
do
	if [ ! -f $i ] ; then
		cp /usr/lib/audio_selector/$i .
		continue
	fi
	if [ -f $home/.audio_selector/$i -a ! -f $home/.audio_selector/$i.nooverwrite ] ; then
		ts1=$(stat -c "%W" $home/.audio_selector/$i)
		ts2=$(stat -c "%W" /usr/lib/audio_selector/$i)
		cmp -s $HOME/.audio_selector/$i /usr/lib/audio_selector/$i 2>/dev/null
		if [ $? -ne 0 ] ; then
			if [ -n "$ts1" -a -n "$ts2" -a $ts2 -gt $ts1 ] ; then
				echo "Updating script $home/.audio_selector/$i"
				if [ "$i" = "initialize" ] ; then
					mv $home/.audio_selector/update $home/.audio_selector/update.bak.
				fi
				cp /usr/lib/audio_selector/$i $home/.audio_selector/$i
			fi
		fi
	fi
done
if [ ! -f services.list ] ; then
	echo mpd > services.list
fi
devices=$(aplay -l 2>/dev/null)
if [ -z "$devices" ] ; then
	exit 0
fi
list=""
for i in librespot squeezelite shairport-sync
do
	if [ ! -x /usr/bin/$i ] ; then
		list="$list $i"
	fi
done
if [ -n "$list" ] ; then
		if [ -f /etc/debian_version ] ; then
			env DEBIAN_FRONTEND=noninteractive apt update -y && \
				env DEBIAN_FRONTEND=noninteractive apt install $list
		elif [ -x /usr/bin/dnf ] ; then
			dnf -y install $list --refresh
		fi
fi
created=0
list=""
for i in librespot squeezelite shairport-sync
do
	if [ ! -x /usr/bin/$i ] ; then
		list="$list $i"
	fi
	if [ ! -d /service/$i -a -x /usr/bin/$i ] ; then
		created=1
		t=$(systemctl is-enabled $i)
		if [ "$t" = "enabled" ] ; then
			sudo systemctl disable $i
			sudo systemctl stop $i
		fi
		if [ -f /usr/lib/systemd/system/$i.service -o -f /etc/systemd/system/$i.service ] ; then
			sudo systemctl mask $i
		fi
		sudo /usr/lib/audio_selector/create_service "--$i"
	fi
done
if [ $created -eq 1 ] ; then
	pidfile=""
	if [ -n "$servicedir" ] ; then
		if [ ! -f $servicedir/.svscan.pid ] ; then
			echo "$servicedir/.svscan.pid: No such file or directory" 1>&2
			exit 1
		fi
		pidfile=$servicedir/.svscan.pid
	fi
	if [ -z "$pidfile" ] ; then
		if [ -d /run/svscan ] ; then
			pidfile=/run/svscan/.svscan.pid
		elif [ -d /var/run/svscan ] ; then
			pidfile=/var/run/svscan/.svscan.pid
		else
			pidfile=/service/.svscan.pid
		fi
	fi
	if [ -f $pidfile ] ; then
		svpid=$(sed -n '$p' $pidfile)
	elif [ -x /usr/sbin/pidof ] ; then
		svpid=$(pidof svscan)
	elif [ -x /usr/bin/pidof ] ; then
		svpid=$(pidof svscan)
	elif [ /usr/bin/pgrep ] ; then
		svpid=$(pgrep -fn /usr/sbin/svscan)
	fi
	if [ -n "$svpid" ] ; then
		echo "Sending HUP to svscan"
		sudo kill -1 $svpid
	fi
fi
exit 0

#
# $Log: initialize,v $
# Revision 1.10  2026-07-26 18:49:17+05:30  Cprogrammer
# use sudo to call systemd, create_service
#
# Revision 1.9  2026-07-26 02:25:04+05:30  Cprogrammer
# use create time
#
# Revision 1.8  2026-07-26 01:58:33+05:30  Cprogrammer
# skip installation if no alsa devices found
#
# Revision 1.7  2026-07-26 01:17:34+05:30  Cprogrammer
# replace /bin/sh with /usr/bin/sh
#
# Revision 1.6  2026-07-25 23:58:29+05:30  Cprogrammer
# copy only when files are different
#
# Revision 1.5  2026-07-25 22:44:23+05:30  Cprogrammer
# added update code for initialize, update, restart
#
# Revision 1.4  2026-07-25 18:37:24+05:30  Cprogrammer
# removed deletion of update_progress.log
#
# Revision 1.3  2026-07-25 09:44:16+05:30  Cprogrammer
# added initialize to copy script list
#
# Revision 1.2  2026-07-25 09:40:49+05:30  Cprogrammer
# install librespot, squeezlite, shairport-sync if missing
#
# Revision 1.1  2026-07-25 00:35:29+05:30  Cprogrammer
# Initial revision
#
#
