#!/bin/sh
#
# $Id: pistopstart.in,v 1.13 2023-07-04 16:12:54+05:30 Cprogrammer Exp mbhangui $
#

systemctl=$(which systemctl)

suicide()
{
	if [ -n "$CAT_PID" ] ; then
		kill $CAT_PID 2>/dev/null
	fi
	echo "ARGH!!! Committing suicide. Going down on SIGTERM"
	exit 1
}

get_mpd_conf_value()
{
    grep "^$1" /etc/mpd.conf|awk '{print $2}' | \
    sed -e 's{"{{g'
}

stop()
{
	while true
	do
		echo "Stopping mpd.service"
		if [ -n $systemctl ] ; then
			$systemctl stop mpd.service mpd.socket
		elif [ -d $servicedir/mpd ] ; then
			svc -d $servicedir/mpd
		else
			break
		fi
		if [ $? -eq 0 ] ; then
			echo "stopped mpd"
			break
		fi
	done
	# MDrive on RPI has the directory sounds
	if [ -d $MDRIVE/sounds ] ; then
		echo "Unmounting MDrive"
		count=1
		while true
		do
			if [ -n "$POWER_OFF" ] ; then
				umount -f -l $MDRIVE >/dev/null 2>&1
			else
				umount -f $MDRIVE >/dev/null 2>&1
			fi
			if [ $? -eq 0 -o $count -eq 60 ] ; then
				if [ $? -eq 0 ] ; then
					printf "umount: %2d: succeeded\n" $count
				else
					printf "umount: %2d: threshold exceeded. Quitting without umount\n" $count
				fi
				break
			fi
			drive_mounted=$(df -k |grep MDrive)
			if [ -z "$drive_mounted" ] ; then
				echo "unmounted MDrive"
				break
			fi
			printf "umount: %2d: failed\n" $count
			count=`expr $count + 1`
			sleep 1
		done
	fi
	if [ -x $systemctl ] ; then
		while true
		do
			echo "Stopping autofs.service"
			$systemctl stop autofs.service
			if [ $? -eq 0 ] ; then
				echo "stopped autofs"
				break
			fi
		done
	elif [ -x /bin/launchctl ] ; then
		while true
		do
			echo "Stopping automountd"
			launchctl stop automountd 
			if [ $? -eq 0 ] ; then
				break
			fi
		done
	fi
	if [ -n "$POWER_OFF" ] ; then
		/usr/libexec/pistop/host_status "3 0 3:$(uname -n) PowerOff"
	else
		/usr/libexec/pistop/host_status "3 0 3:$(uname -n) Off"
	fi
	echo "Completed STOP Service"
	if [ -n "$POWER_OFF" ] ; then
		echo "shutdown -h now"
		shutdown -h now
	fi
}

start()
{
	mpd_up=0
	autofs_up=0
	count=1
	mpd_was_down=0
	autofs_was_down=0
	mdrive_mounted=0
	echo "Initializing $(uname -n) localtime $(date)" 1>&7
	while true
	do
		# 1. we assume we are running on Linux mostly
		# 2. If on mac/freebsd, we assume autofs has been setup
		if [ -x $systemctl ] ; then
			$systemctl status autofs.service >/dev/null 2>&1
			if [ $? -ne 0 ] ; then
				autofs_was_down=1
				echo "starting automount filesystems"
				$systemctl start autofs.service
				sleep 1
				continue
			else
				autofs_up=1
				if [ $autofs_was_down -eq 1 ] ; then
					echo "started automount filesystems"
					autofs_was_down=0
				fi
			fi
		fi
		if [ ! -f $MDRIVE/data/stats.db ] ; then
			echo "MDrive [$MDRIVE] not yet mounted"
			sleep 5
			continue
		fi
		if [ $mdrive_mounted -eq 0 ] ; then
			mdrive_mounted=1
			echo "MDrive mounted"
		fi
		if [ -x $systemctl ] ; then
			$systemctl status mpd.service >/dev/null 2>&1
			if [ $? -ne 0 ] ; then
				# trigger autofs to mount MDrive
				mpd_was_down=1
				echo "starting music player daemon"
				$systemctl start mpd.service
			else
				mpd_up=1
				if [ $mpd_was_down -eq 1 ] ; then
					echo "started music player daemon"
					mpd_was_down=0
				fi
			fi
			[ $mpd_up -eq 1 ] && [ $autofs_up -eq 1 ] && break
		else
			svstat $servicedir/mpd >/dev/null 2>&1
			if [ $? -ne 0 ] ; then
				# trigger autofs to mount MDrive
				mpd_was_down=1
				echo "starting music player daemon"
				svc -u $servicedir/mpd
			else
				mpd_up=1
				if [ $mpd_was_down -eq 1 ] ; then
					echo "started music player daemon"
					mpd_was_down=0
				fi
			fi
			[ $mpd_up -eq 1 ] && break
		fi
		if [ $count -eq 60 ] ; then
			echo "Giving up..."
			svc -dx /service/fclient
			$systemctl stop autofs
			umount $MDRIVE
			break
		fi
		sleep 20
		count=`expr $count + 1`
	done
	/usr/libexec/pistop/host_status load
	if [ -x $MDRIVE/bin/boot_sound ] ; then
		$MDRIVE/bin/boot_sound >/dev/null 2>&1
	fi
	if [ -n "DEST" -a -n "$BACKUP" -a -d $MDRIVE/backup ] ; then
		echo "backing up configuraton"
		/usr/libexec/pistop/backup
	else
		echo "Skipping backup of configuration"
	fi
	$systemctl list-unit-files sound.service >/dev/null 2>&1
	if [ $? -eq 0 ] ; then
		line=$(systemctl list-units --all sound.service|sed -n 2p)
		if [ "$2" != "loaded" ] ; then
			$systemctl start sound.service
		fi
	else
		echo "sound.service not needed to be loaded"
	fi
	echo "Completed START Service"
}

trap suicide TERM INT
if [ -n "$TCPREMOTEIP" ] ; then
	if [ $# -gt 0 ] ; then
		echo "Connected to $TCPREMOTEIP, executing $0 $*"
	else
		echo "Connected to $TCPREMOTEIP, executing $0"
	fi
else
	if [ $# -gt 0 ] ; then
		echo "Connected to remote, executing $0 $*"
	else
		echo "Connected to remote, executing $0"
	fi
fi
servicedir=/service
music_dir=$(get_mpd_conf_value music_dir)
t=$(dirname $music_dir)
if [ -n "$t" ] ; then
	MDRIVE=$t
	echo "MDrive from mpd.conf is $MDRIVE"
else
	echo "MDrive variable set is $MDRIVE"
fi

case "$1" in
	start)
	start
	;;

	stop)
	stop
	;;

	sleep)
	sleep 20
	;;
esac

#
# $Log: pistopstart.in,v $
# Revision 1.13  2023-07-04 16:12:54+05:30  Cprogrammer
# use umount -f -l to prevent umount hanging
#
# Revision 1.12  2023-06-30 10:08:09+05:30  Cprogrammer
# send text to LCD display using host_status script
#
# Revision 1.11  2023-06-29 22:45:10+05:30  Cprogrammer
# updated text message sent to lcdhost
#
# Revision 1.10  2023-06-29 10:30:07+05:30  Cprogrammer
# use LCD_HOST if defined instead of lcdhost in /etc/hosts
#
# Revision 1.9  2023-06-28 23:23:18+05:30  Cprogrammer
# use timeout for nc command
#
# Revision 1.8  2023-06-28 21:35:40+05:30  Cprogrammer
# use /proc/loadavg instead of uptime command for consistent output
#
# Revision 1.7  2023-06-28 12:34:27+05:30  Cprogrammer
# send startup/shutdown text to lcdhost
#
# Revision 1.6  2023-06-26 19:53:16+05:30  Cprogrammer
# Use MDRIVE from variables if not from /etc/mpd.conf
#
# Revision 1.5  2023-05-18 08:16:54+05:30  Cprogrammer
# increased duration of sleep if disk mount fails
#
# Revision 1.4  2023-05-03 16:36:46+05:30  Cprogrammer
# removed hardcoded systemctl path
#
# Revision 1.3  2022-12-27 10:36:13+05:30  Cprogrammer
# added debug log
#
# Revision 1.2  2022-07-13 17:16:29+05:30  Cprogrammer
# fixed power off when fserver service is shutdown
#
# Revision 1.1  2022-07-12 17:32:59+05:30  Cprogrammer
# Initial revision
#
# 
