#!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Time functions
#
# 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/>.

# Why use Google as a time source?
# The thinking here is that it's likely to be reliable and fast.
# The ping doesn't reveal any information other than that the server
# is running, and if anyone maliciously alters the time on Google's
# servers then that would certainly be newsworthy and they'd be
# likely to do something about it quickly.
# If you have better time sources then change them here.
TLS_TIME_SOURCE1="google.com"
TLS_TIME_SOURCE2="www.ptb.de"

# Time synchronisation
TLSDATE_REPO="https://github.com/bashrc/tlsdate"
TLSDATE_COMMIT='505e31540eebde8074e7dc93b29be0d848def06a'

function check_date {
	curr_date=$(date)
	if [[ $curr_date == *"1970"* ]]; then
		apt-get -y install ntp
	fi
}

function time_synchronisation {
	# mesh peers typically don't sync over the internet
	if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi

	if [ -f /usr/local/bin/${PROJECT_NAME}-update-date ]; then
		cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
	else
		cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
	fi
	chmod +x /usr/bin/updatedate

	if grep -Fxq "time_synchronisation" $COMPLETION_FILE; then
		return
	fi

	apt-get -y install tlsdate
	apt-get -y remove ntpdate

	function_check cron_add_mins
	cron_add_mins 15 '/usr/bin/updatedate'

	systemctl restart cron

	echo 'time_synchronisation' >> $COMPLETION_FILE
}

function time_synchronisation_tlsdate {
	# mesh peers typically don't sync over the internet
	if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
		return
	fi

	if [ -f /usr/local/bin/${PROJECT_NAME}-update-date ]; then
		cp /usr/local/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
	else
		cp /usr/bin/${PROJECT_NAME}-update-date /usr/bin/updatedate
	fi
	chmod +x /usr/bin/updatedate

	if [ ! -d $INSTALL_DIR ]; then
		mkdir -p $INSTALL_DIR
	fi
	set_repo_commit $INSTALL_DIR/tlsdate "tlsdate commit" "$TLSDATE_COMMIT" $TLSDATE_REPO

	if grep -Fxq "time_synchronisation_tlsdate" $COMPLETION_FILE; then
		return
	fi

	apt-get -y remove tlsdate ntpdate
	apt-get -y install build-essential autoconf libevent-dev

	cd $INSTALL_DIR
	function_check git_clone
	git_clone $TLSDATE_REPO $INSTALL_DIR/tlsdate
	cd $INSTALL_DIR/tlsdate
	git checkout $TLSDATE_COMMIT -b $TLSDATE_COMMIT
	./autogen.sh
	./configure
	if [ ! "$?" = "0" ]; then
		echo $'Unable to configure tlsdate'
		exit 6825277
	fi
	make
	if [ ! "$?" = "0" ]; then
		echo $'Unable to build tlsdate'
		exit 3792726
	fi
	make install

	function_check cron_add_mins
	cron_add_mins 15 '/usr/bin/updatedate'

	echo 'time_synchronisation_tlsdate' >> $COMPLETION_FILE
}

# NOTE: deliberately no exit 0
