#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Watchdog functions to keep systems running even if they crash
# systemd itself may be able to handle this to a large extent
#
# License
# =======
#
# Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# name of a script which keeps running processes going even if they crash
WATCHDOG_SCRIPT_NAME="keepon"

function install_watchdog_script {
	if grep -Fxq "install_watchdog_script" $COMPLETION_FILE; then
		return
	fi
	echo '#!/bin/bash' > /usr/bin/$WATCHDOG_SCRIPT_NAME
	echo 'LOGFILE=/var/log/keepon.log' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
	echo 'CURRENT_DATE=$(date)' >> /usr/bin/$WATCHDOG_SCRIPT_NAME
	# application specific stuff is added later
	chmod +x /usr/bin/$WATCHDOG_SCRIPT_NAME

	function_check cron_add_mins
	cron_add_mins 1 '/usr/bin/$WATCHDOG_SCRIPT_NAME'

	echo 'install_watchdog_script' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
