#!/ffp/bin/sh

# PROVIDE: mpd

name="mpd"
command="/ffp/bin/$name"
home_dir="/ffp/opt/mpd"
configfile="/ffp/etc/$name.conf"
required_dirs="$home_dir"
required_files="$command $configfile"
runas_user="mpd"

# WARNING: Stop mpd service, before changing mpd_flags variables !!!
# Otherwise status, start, stop, restart and reload commands of this script might fail.
mpd_flags="$configfile"

if	[ -n "$mpd_flags" ]
then
	command="$command $mpd_flags"
fi

# Check required_files
for req_file in $required_files
do
	if	[ ! -r "$req_file" ]
	then
		echo -e "\e[1;31;20m$req_file: Required file not found or not readable!\e[0m"
		exit 1
	fi
done

# Check required_dirs
for req_dir in $required_dirs
do
	if	[ ! -d "$req_dir" ]
	then
		echo -e "\e[1;31;20m$req_dir: Required directory not found!\e[0m"
		exit 1
	fi
done

add_user()
{
	/ffp/bin/id -g "$runas_user" &>/dev/null || /ffp/sbin/groupadd -r "$runas_user" >/dev/null
	/ffp/bin/id -u "$runas_user" &>/dev/null || /ffp/sbin/useradd -r -g "$runas_user" -d "$home_dir" -s /ffp/bin/false -c MusicPlayerDaemon "$runas_user" >/dev/null
}

check_home_dir()
{
	# Checking. Do the $home_dir and files in it, is owned by $runas_user?
	# If not, change owner to $runas_user
	if	[ "$(stat -c '%U' "$home_dir")" != "$runas_user" ]
	then
		chown -R "$runas_user":"$runas_user" "$home_dir"
	else
		find "$home_dir" ! -user "$runas_user" -exec chown "$runas_user":"$runas_user" {} \;
	fi
}

check_status()
{
	status=$(pgrep -fx "$command")
	retvalue=$?
}

mpd_start()
{
	check_status
	if	[ "$retvalue" = "1" ]; then
		add_user
		modprobe snd-usb-audio index=0
		mknod /dev/mixer c 14 0 > /dev/null 2>&1
		mknod /dev/dsp c 14 3 > /dev/null 2>&1
		modprobe snd-pcm-oss
		modprobe snd-mixer-oss
		
		check_home_dir && echo -n "Starting $name..."
		$command
		sleep 1
		check_status
		if	[ "$retvalue" = "0" ]; then
			echo -e "done! \e[1;32;20m[ OK ]\e[0m"
		else
			echo -e "\e[1;31;20m[ Fail!!! ] ERROR WARNING: $name can not start!!!\e[0m"
			exit 1
		fi
	else
		if	[ "$retvalue" = "0" ]; then
			echo -e "PROCESS STATUS INFO: \e[1;33;20m$name is already running with process ID(s):$status\e[0m"
		else
			echo -e "\e[1;31;20mERROR WARNING: Can not determine running status of $name-start command failed!!!\e[0m"
			exit 1
		fi
	fi
}

mpd_stop()
{
	check_status
	if	[ "$retvalue" = "0" ]; then
		echo -n "Stopping $name..."
		pkill -fxP1 "$command" -u "$runas_user"
		local timeout=0
		while [ $timeout -lt 5 ]; do
			sleep 1
			check_status
			if	[ "$retvalue" = "1" ]; then
				echo -e "done! \e[1;32;20m[ OK ]\e[0m"
				break
			fi
			let timeout=timeout+1
		done
		if	[ "$retvalue" = "0" ]; then
			echo -e "\e[1;31;20m[ Fail!!! ] ERROR WARNING: $name failed to exit cleanly within $timeout seconds. Timeout reached!\e[0m"
			echo -n "Stopping $name by force..."
			pkill -9 -fx "$command"
			local timeout=0
			while [ $timeout -lt 2 ]; do
				check_status
				if	[ "$retvalue" = "1" ]; then
					echo -e "done! \e[1;32;20m[ OK ]\e[0m"
					break
				fi
				sleep 1
				let timeout=timeout+1
			done
			if	[ "$retvalue" = "0" ]; then
				echo -e "\e[1;31;20m[ Fail!!! ] ERROR WARNING: $name can not be stopped!!!\e[0m"
				exit 1
			fi
		fi
	else
		if	[ "$retvalue" = "1" ]; then
			echo -e "PROCESS STATUS INFO: \e[1;33;20m$name is not running\e[0m"
			exit 1
		else
			echo -e "\e[1;31;20mERROR WARNING: Can not determine running status of $name-stopping command failed!!!\e[0m"
			exit 1
		fi
	fi
}

mpd_status()
{
	check_status
	echo -n "PROCESS STATUS INFO: "
	if	[ "$retvalue" = "0" ]; then
		echo -e "\e[1;33;20m$name is running with process ID(s):$status\e[0m"
	else
		if	[ "$retvalue" = "1" ]; then
			echo -e "\e[1;33;20m$name is not running\e[0m"
		else
			echo -e "\e[1;31;20mERROR WARNING: Can not determine running status of $name-status command failed!!!\e[0m"
			exit 1
		fi
	fi
}

mpd_reload()
{
	# This command will reload mpd config file (-SIGUSR1 don't)
	check_status
	if	[ "$retvalue" = "0" ]
	then
		echo -n "Reloading $name config file: $configfile..."
		pkill -HUP -fxP1 "$command" -u "$runas_user"
		if	[ "$?" = "0" ]
		then
			echo -e "done! \e[1;32;20m[ OK ]\e[0m"
		else
			echo -e "\e[1;31;20mERROR WARNING:[ FAIL ]!!!\e[0m"
		fi
	else
		if	[ "$retvalue" = "1" ]
		then
			echo -e "PROCESS STATUS INFO: \e[1;33;20m$name is not running\e[0m"
		else
			echo -e "\e[1;31;20mERROR WARNING: Can not determine running status of $name-reload command failed!!!\e[0m"
			exit 1
		fi
	fi
}

mpd_debug()
{
	check_status
	local mpd_flags+="--no-daemon"
	local command="/ffp/bin/$name $mpd_flags"
	if	[ "$retvalue" = "1" ]
	then
		add_user
		check_home_dir && echo -n "Starting $name..."
		$command
	else
		if	[ "$retvalue" = "0" ]
		then
			echo -e "PROCESS STATUS INFO: \e[1;33;20m$name is already running with process ID(s):$status\e[0m"
		else
			echo -e "\e[1;31;20mERROR WARNING: Can not determine running status of $name-start command failed!!!\e[0m"
			exit 1
		fi
	fi
}

case "$1" in
		start)
				mpd_start
				;;
		stop)
				mpd_stop
				;;
		restart)
				mpd_stop
				sleep 1
				mpd_start
				;;
		status)
				mpd_status
				;;
		reload)
				mpd_reload
				;;
		debug)
				mpd_debug
				;;
		*)
				echo "Usage: $0 (start|stop|restart|status|reload|debug)"
				exit 1
				;;
esac
