#!/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/>.       #
#                                                                             #
###############################################################################

ongreen=1
green=$(cat /var/ipfire/ethernet/settings | grep GREEN_NETADDRESS)
if [ -z "$green" ]; then
	:		# no Green subnet
else
	green=${green##*=}; green="${green%.*}.[0-9]"
	if [ -n "$(cat /var/ipfire/time/settime.conf | grep $green)" ]; then
		ongreen=0	# NTP on Green
	fi
fi

onorange=1
orange=$(cat /var/ipfire/ethernet/settings | grep ORANGE_NETADDRESS)
if [ -z "$orange" ]; then
	:		# no Orange subnet
else
	orange=${orange##*=}; orange="${orange%.*}.[0-9]"
	if [ -n "$(cat /var/ipfire/time/settime.conf | grep $orange)" ]; then
		onorange=0	# NTP on Orange
	fi
fi

# find out language
tlanguage=$(cat /var/ipfire/main/settings | grep LANGUAGE)
tlanguage=${tlanguage##*=}

if [ -n "$1" ]; then
	if [ -e /var/ipfire/red/active ] || [ "$ongreen" -eq 0 ] || [ "$onorange" -eq 0 ]; then
		/usr/bin/ntpdate -su $1 $2
		ntpdatetest=$?

		if [ "$ntpdatetest" -eq 0 ]; then
			case $tlanguage in
				en ) date "+%X %Z on %d %B %Y" > /var/lock/time/lastset ;
					logger -t ipfire "NTP synchronisation event" ;;
				de ) date "+%X Uhr am %d.%m.%Y" > /var/lock/time/lastset ;
					logger -t ipfire "NTP Synchronisierung" ;;
				*  ) date "+%X %Z, %Y-%m-%d" > /var/lock/time/lastset ;
					logger -t ipfire "NTP synchronisation" ;;
			esac

			/sbin/hwclock --systohc
			rm -f /var/lock/time/settimenow
			# reset counter variable
			cp /var/ipfire/time/counter.conf /var/lock/time/counter
		else
			case $tlanguage in
				de ) logger -t ipfire "ntpdate fehlerhaft" ;;
				*  ) logger -t ipfire "ntpdate error" ;;
			esac
		fi
	fi
else
	echo Missing parameter
fi
