#!/bin/sh
#
# $Id: pistopstart.in,v 1.22 2025-11-18 08:34:51+05:30 Cprogrammer Exp mbhangui $
#

systemctl=$(which systemctl)

suicide()
{
	echo "ARGH!!! Committing suicide. Going down on SIGTERM"
	exit 1
}

get_mpd_conf_value()
{
	if [ ! -f /etc/mpd.conf ] ; then
		return 1
	fi
    grep "^$1" /etc/mpd.conf|awk '{print $2}' | \
		sed -e 's{"{{g'
}

get_mdrive()
{
	d1=$(grep -v '^#' /etc/auto.master.d/mpd.autofs|awk '{print $1}')
	d2=$(grep -v '^#' /etc/auto.master.d/mpd.autofs|awk '{print $2}')
	if [ -n "$d2" ] ; then
		d3=$(grep -v '^#' $d2|awk '{print $1}')
		echo "$d1/$d3"
	fi
}

stop()
{
	if [ -n "$POWER_OFF" ] ; then
		echo "Shutting down $(uname -n) localtime $(date)"
	else
		echo "stopping $(uname -n) localtime $(date)"
	fi
	if [ $mpd_present -eq 1 ] ; then
		count=1
		while true
		do
			echo "Stopping mpd.service"
			if [ -n $systemctl ] ; then
				echo "$systemctl stop mpd.service mpd.socket"
				$systemctl stop mpd.service mpd.socket
			elif [ -d $servicedir/mpd ] ; then
				echo "svc -d $servicedir/mpd"
				svc -d $servicedir/mpd
			else
				break
			fi
			[ $? -eq 0 ] && break
			printf "stop mpd: %2d: failed\n" $count
			count=`expr $count + 1`
			sleep 1
		done
	fi
	if [ -n "$MDRIVE" ] ; then
		count=1
		if [ -x $HOME/.pistop/pre_unmount ] ; then
			echo "Executing user command pre_unmount"
			$HOME/.pistop/pre_unmount
		fi
		while true
		do
			if [ -n "$POWER_OFF" -a -n "$FORCE_UMOUNT" ] ; then
				if [ $count -eq 1 ] ; then
					echo "Force Unmounting MDrive with lazy option"
				fi
				[ $count -eq 1 ] && echo "umount -fl $MDRIVE"
				umount -fl $MDRIVE
			else
				if [ $count -eq 1 ] ; then
					echo "Force Unmounting MDrive"
				fi
				[ $count -eq 1 ] && echo "umount -f $MDRIVE"
				umount -f $MDRIVE
			fi
			if [ $? -eq 0 -o $count -eq 60 ] ; then
				if [ $? -eq 0 ] ; then
					printf "umount: %2d: succeeded\n" $count
				else
					printf "WARNING!!! 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
		if [ -x $HOME/.pistop/post_unmount ] ; then
			echo "Executing user command post_unmount"
			$HOME/.pistop/post_unmount
		fi
	fi
	if [ -x $systemctl ] ; then
		while true
		do
			echo "$systemctl stop 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 PowerOff
	else
		/usr/libexec/pistop/host_status MusicOff
	fi
	if [ -n "$POWER_OFF" ] ; then
		echo "shutdown $(uname -n) localtime $(date)"
		echo "Completed STOP Service and shutdown in progress..."
		shutdown -h now
	else
		echo "stopped $(uname -n) localtime $(date)"
		echo "Completed STOP Service"
	fi
}

start_mpd()
{
	mpd_up=0
	if [ -x $systemctl ] ; then
		echo "$systemctl status mpd.service"
		$systemctl status mpd.service >/dev/null 2>&1
		if [ $? -ne 0 ] ; then
			# trigger autofs to mount MDrive
			mpd_was_down=1
			echo "$systemctl start mpd.service"
			$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
	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
	fi
	[ $mpd_up -eq 1 ] && return 0 || return 1
}

give_up()
{
	echo "Giving up..."
	echo "svc -dx /service/fclient"
	svc -dx /service/fclient
	echo "$systemctl stop autofs"
	$systemctl stop autofs
	echo "umount $MDRIVE"
	umount $MDRIVE
}

start()
{
	mpd_up=0
	autofs_up=0
	count=1
	mpd_was_down=0
	autofs_was_down=0
	mdrive_mounted=0
	echo "Initializing $(uname -n) MDRIVE [$MDRIVE] localtime $(date)" 1>&7 # running under tcpclient
	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
				if [ $count -eq 60 ] ; then
					give_up
					break
				fi
				if [ $count -eq 0 -a -x $HOME/.pistop/pre_mount ] ; then
					echo "Executing user command pre_mount"
					$HOME/.pistop/pre_mount
				fi
				count=`expr $count + 1`
				autofs_was_down=1
				echo "$systemctl start autofs.service"
				$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
			if [ $count -eq 60 ] ; then
				give_up
				break
			fi
			count=`expr $count + 1`
			echo "MDrive [$MDRIVE] not yet mounted"
			sleep 5
			continue
		fi
		if [ $mdrive_mounted -eq 0 -a -x $HOME/.pistop/post_mount ] ; then
			echo "Executing user command post_mount"
			$HOME/.pistop/post_mount
		fi
		[ $mdrive_mounted -eq 0 ] && mdrive_mounted=1 && echo "MDrive mounted"
		if [ $mpd_present -eq 1 ] ; then
			start_mpd
			[ $? -eq 0 ] && break
		else
			break
		fi
		[ $count -eq 60 ] && give_up && break
		sleep 20
		count=`expr $count + 1`
	done
	/usr/libexec/pistop/host_status load
	[ -x $MDRIVE/bin/boot_sound ] && $MDRIVE/bin/boot_sound 2>/dev/null
	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
			echo "$systemctl start sound.service"
			$systemctl start sound.service
		fi
	else
		echo "sound.service not needed to be loaded"
	fi
	echo "Initialized  $(uname -n) localtime $(date)" 1>&7 # running under tcpclient
	echo "Completed START Service"
}

## Main ###
trap suicide TERM INT
trap '' PIPE
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
[ -f /etc/mpd.conf -o -L /etc/mpd.conf ] && mpd_present=1 || mpd_present=0
[ $mpd_present -eq 1 ] && MDRIVE=$(get_mdrive) || MDRIVE=""
if [ -z "$MDRIVE" ] ; then
	if [ -d /MDrive ] ; then
		MDRIVE=/MDrive
	elif [ -d /autofs/MDrive ] ; then
		MDRIVE=/autofs/MDrive
	elif [ -d /var/lib/mpd/MDrive/backup ] ; then
		MDRIVE=/var/lib/mpd/MDrive
	elif [ -d /home/pi/MDrive/backup ] ; then
		MDRIVE=/home/pi/MDrive
	fi
fi
[ -n "$MDRIVE" ] && echo "MDrive is $MDRIVE" || echo "MDrive not found"

case "$1" in
	start)
	start
	;;

	stop)
	stop
	;;

	sleep)
	sleep 20
	;;
esac

#
# $Log: pistopstart.in,v $
# Revision 1.22  2025-11-18 08:34:51+05:30  Cprogrammer
# display umount error in logs
#
# Revision 1.21  2025-03-18 00:22:05+05:30  Cprogrammer
# remove redirection to descriptor 7 during shutdown
#
# Revision 1.20  2024-09-12 08:55:42+05:30  Cprogrammer
# added pre_unmount, post_unmount, pre_mount, post_mount feature
#
# Revision 1.19  2024-07-29 08:40:41+05:30  Cprogrammer
# get MDRIVE from /etc/auto.master.d/mpd.autofs and /etc/autofs.mounts
#
# Revision 1.18  2024-04-28 12:32:14+05:30  Cprogrammer
# added more info statements
#
# Revision 1.17  2024-04-21 23:12:32+05:30  Cprogrammer
# send PowerOff, MusicOff notification as desktop notification
#
# Revision 1.16  2024-03-27 09:44:56+05:30  Cprogrammer
# ignore sigpipe
# umount MDrive without triggering autof
#
# Revision 1.15  2024-03-21 23:40:03+05:30  Cprogrammer
# display umount method in logs
#
# Revision 1.14  2024-03-15 12:11:01+05:30  Cprogrammer
# modified for config without mpd
#
# 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
#
# 
