#!/bin/bash

service ntpd stop
yum erase ntp -y

CURRENT_TIME=$(date)
CURRENT_SET_TIMEZONE_IN_LOCALTIME=$(ls -l /etc/localtime)
CURRENT_SET_TIMEZONE_IN_CLOCK=`cat /etc/sysconfig/clock`
echo "CURRENT_TIME: $CURRENT_TIME"
echo "CURRENT_SET_TIMEZONE_IN_LOCALTIME: $CURRENT_SET_TIMEZONE_IN_LOCALTIME"
echo "CURRENT_SET_TIMEZONE_IN_CLOCK: $CURRENT_SET_TIMEZONE_IN_CLOCK"

sed -i -e '/ZONE=/d' /etc/sysconfig/clock
sed -i -e '/UTC/d' /etc/sysconfig/clock
echo "ZONE=\"Europe/Berlin\"" >> /etc/sysconfig/clock
echo "UTC=true" >> /etc/sysconfig/clock
NEW_SET_TIMEZONE_IN_CLOCK=$(cat /etc/sysconfig/clock)
echo "NEW_SET_TIMEZONE_IN_CLOCK: $NEW_SET_TIMEZONE_IN_CLOCK"

rm -f /etc/localtime ; ln -s /usr/share/zoneinfo/Europe/Berlin /etc/localtime

yum install ntp -y
sed -i -e '/driftfile/a\logfile \/var\/log\/ntp.log' /etc/ntp.conf
sed -i -e '/^server/d' /etc/ntp.conf

echo "server 0.de.pool.ntp.org" >> /etc/ntp.conf
echo "server 1.de.pool.ntp.org" >> /etc/ntp.conf
echo "server 2.de.pool.ntp.org" >> /etc/ntp.conf
echo "server 3.de.pool.ntp.org" >> /etc/ntp.conf
echo "interface ignore wildcard" >> /etc/ntp.conf

sleep 5
service ntpd restart

ntpq -p
ntpdate -s 0.de.pool.ntp.org
ntpdate -db 0.de.pool.ntp.org
ntpdate -u europe.pool.ntp.org

chkconfig --add ntpd
chkconfig ntpd on

chkconfig --add ntpdate
chkconfig ntpdate on

sleep 30
echo "Date after all changes done: $(date)"
sleep 30

service rsyslog restart
hwclock --systohc
