#!/bin/sh
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU 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 General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

if [ -e /var/ipfire/time/enable ]; then
	if [ $(cat /var/ipfire/time/settings | grep period) ] && [ -e /var/lock/time/counter ]; then
		if [ $(cat /var/lock/time/counter) -gt 0 ]; then
			echo $(($(cat /var/lock/time/counter)-5)) > /var/lock/time/counter
		else
			touch /var/lock/time/settimenow
			rm -f /var/lock/time/counter
		fi
	else
		# EAO added 2003-08-14 for v1.3.1
		# if settings are restored from a backup
		# /var/lock/time/counter will not exist
		if [ $(cat /var/ipfire/time/settings | grep periodic) ]; then
			touch /var/lock/time/settimenow
		fi
	fi

	if [ -e /var/lock/time/settimenow ]; then
		if 	[ -e /var/ipfire/time/settime.conf ]; then
			/usr/local/bin/settime $(cat /var/ipfire/time/settime.conf)
		fi
	fi
fi
