#!/sbin/openrc-run
# Copyright 1999-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

extra_commands="watchdogrestart"

. /etc/conf.d/vdr.watchdogd

common_init() {
	vdr_home=$(eval echo ~vdr)
	cd "${vdr_home}"

	. /usr/share/vdr/inc/functions.sh
	include rc-functions
	include plugin-functions
	include svdrpcmd
	VDR_LOG_FILE="${vdr_home}/tmp/vdr-start-log"
}

clear_logfile() {
	rm -f "${VDR_LOG_FILE}"
	printf "" > "${VDR_LOG_FILE}"
}

#
# Used to log error-messages in startscript to show them on
# OSD later when choosing appropriate point in commands.
#

vdr_log()
{
	echo "$@" >> ${VDR_LOG_FILE}
}

depend() {
	[ "${IR_CTRL}" = "lirc" ] && need lirc
	use lirc
	after checkroot
}

start_vdr() {
	einfo "Preparing start of vdr:"
	clear_logfile
	init_params
	add_daemonctrl_param --start --chdir ~vdr --exec ${VDR_BIN}
	init_plugin_loader start

	svdrp_command

	load_addons_prefixed pre-start || return 1

	ebegin "Starting ${VDRNAME}"
	unset MAIL
	export LOGNAME=vdr USER=vdr HOME="${vdr_home}"

	local _openvt= openvt_opts= TERM_DEV=/dev/tty${TERMINAL}
	if [ -n "${TERMINAL}" -a -e "${TERM_DEV}" ]; then
		yesno "${SWITCH_TO_TERMINAL}" && openvt_opts="-s"
		_openvt="openvt -c ${TERMINAL} ${openvt_opts} --"

		{
			clear
			einfo "Starting ${VDRNAME}"
		} >${TERM_DEV}
	else
		add_param --daemon
	fi

	debug_msg "  CMDLINE: ${_openvt} start-stop-daemon ${daemonctrl_opts} -- ${vdr_opts}"

	eval ${_openvt} start-stop-daemon ${daemonctrl_opts} -- ${vdr_opts}
	vdr_exitcode=$?
	eend $vdr_exitcode "Failed to start vdr."

	# TODO: Anything todo if starting failed? cleanups?

	if [ $vdr_exitcode -lt 128 ]; then
		load_addons_prefixed post-start || vdr_exitcode=1
	fi

	if [ $vdr_exitcode != 0 ]; then
		# Make sure vdr really does not run
		kill_vdr
	fi

	# show messages if we have any
	if [ "${vdr_exitcode}" = "0" -a -s "${VDR_LOG_FILE}" ]; then
		/usr/share/vdr/bin/vdr-bg.sh ${SVDRPCMD} mesg "Errors: Go to Commands/View VDR Start Log"
	fi

	return $vdr_exitcode
}

stop_vdr() {
	init_plugin_loader stop

	load_addons_prefixed pre-stop

	ebegin "Stopping ${VDRNAME}"
	# Use --name here to allow us to kill vdr even after a new emerge
	start-stop-daemon --stop --quiet --retry 15 --exec ${VDR_BIN}
	exitcode=$?
	eend $exitcode "Failed to stop vdr."

	if [ $exitcode != 0 ]; then
		kill_vdr
	fi

	load_addons_prefixed post-stop
	return 0
}

kill_vdr() {
	killall ${VDR_BIN}
	if test_vdr_process; then
		sleep 2
		test_vdr_process && killall -9 ${VDR_BIN}
	fi
}


start_watchdog() {
	yesno "${ENABLE_EXTERNAL_WATCHDOG:-yes}" || return 0
	ebegin "Starting vdr watchdog"
	start-stop-daemon \
		--start \
		--background \
		--make-pidfile \
		--pidfile /var/run/vdrwatchdog.pid \
		--exec /usr/sbin/vdr-watchdogd \
		--name vdr-watchdogd
	eend $? "failed starting vdr watchdog"
	return 0
}


stop_watchdog() {
	if yesno "${ENABLE_EXTERNAL_WATCHDOG:-yes}"; then
		ebegin "Stopping vdr watchdog"
		start-stop-daemon --stop --pidfile /var/run/vdrwatchdog.pid --name vdr-watchdogd
		eend $? "failed stopping watchdog"
	else
		# Also stop watchdog if conf was changed to disabled while it was running
		start-stop-daemon --stop --pidfile /var/run/vdrwatchdog.pid --name vdr-watchdogd --quiet
	fi
	return 0
}

start() {
	common_init
	start_vdr || return 1
	start_watchdog || return 2
}

stop() {
	common_init
	stop_watchdog || return 2
	stop_vdr
}

# gets called by watchdog to restart vdr
# and possibly reload modules
watchdogrestart() {
	common_init

	( stop_vdr )

	load_addons_prefixed watchdog-restart

	( start_vdr )

	svdrp_command

	${SVDRPCMD} mesg "Warning: VDR process died."
}
