#!/bin/bash

set -e

##remove old sources file if its the old packaged version.

if [ "$2" = "23.7.01mx23" ] && [ -e /etc/chrony/sources.d/mx-datetime.sources ]; then
	if [ -n "$(grep '# MX Date & Time sources' /etc/chrony/sources.d/mx-datetime.sources)" ]; then
		rm -f /etc/chrony/sources.d/mx-datetime.sources
	elif [ -z "$(grep -E '^\s?(pool|server|peer)\s' /etc/chrony/chrony.conf)" ]; then
		if [ -e /usr/share/chrony/chrony.conf ]; then
			cp /usr/share/chrony/chrony.conf /etc/chrony/
			rm -f /etc/chrony/sources.d/mx-datetime.sources
		else
			echo "pool 2.debian.pool.ntp.org" > /etc/chrony/sources.d/mx-datetime.sources
		fi
		echo -e "\e[1;33mNOTE: Any custom changes made to the NTP sources list may have been lost in the upgrade."
		echo -e "To review the sources, run MX Date & Time (mx-datetime) and check the Network Time page.\e[0m"
	fi
	sync
	service chrony restart
fi

exit 0
