#!/bin/bash

BACKUPDIR=/var/lib/ntp2chrony/backup
LOGFILE=/var/log/migrate-ntp2chrony.log

# if this directory exist, we did already migrate
test -d ${BACKUPDIR} && exit 0

# if ntp is not installed, exit
rpm -q --quiet ntp || exit 0

# if chrony is not installed, exit
rpm -q --quiet chrony || exit 0

# ntpd is not enabled, exit
systemctl is-enabled -q ntpd || exit 0

# ntpd is enabled, chrony installed. Backup
# configuration, convert configuration, disable
# ntpd, enable chrony, but do not restart.
mkdir -p ${BACKUPDIR} || exit 1
cp -a /etc/ntp.* ${BACKUPDIR}/
cp -a /etc/chrony.* ${BACKUPDIR}/
echo "Migrate /etc/ntp.conf to /etc/chrony.conf (`date \"+%Y-%m-%d %H:%M:%S\"`)" | tee -a ${LOGFILE}
echo "Ignored lines:" | tee -a ${LOGFILE}
/usr/sbin/ntp2chrony --backup --ignored-lines |& tee -a ${LOGFILE}
if [ ${PIPESTATUS[0]} -ne 0 ]; then
  exit 1
fi
systemctl disable ntpd |& tee -a ${LOGFILE}
systemctl enable chronyd |& tee -a ${LOGFILE}

