#!/bin/ash

BACKUPDIR=/mnt/disc0_1/temploggerd/rrd
DESTDIR=/var/lib/temploggerd/www/temploggerd/rrd

if [ ! -d $BACKUPDIR ]; then
	echo "Backup-dir $BACKUPDIR doesn't exist"
	exit 0
fi

YEAR=`date +"%Y"`
if [ $YEAR -lt 2007 ]; then
    date >>/tmp/ntp_restarted
    if [ -x /etc/init.d/S60ntp ]; then
	echo >>/tmp/ntp_restarted "ntpclient restarted from temploggerd-script"
	/etc/init.d/S60ntp start
    fi
    sleep 5

    YEAR=`date +"%Y"`
    if [ $YEAR -lt 2007 ]; then
	echo >>/tmp/ntp_restarted "Date is probably not set."
	/sbin/ifconfig >>/tmp/ntp_restarted
    else
	echo >>/tmp/ntp_restarted "Date seems to be correct now."
    fi
    date >>/tmp/ntp_restarted
fi

if [ ! -d $DESTDIR ]; then
	mkdir -p $DESTDIR
fi

ls $BACKUPDIR/*.rrd | while read file ; do
    #echo "file=$file"
    BASENAME=`basename $file`

    if [ -f $DESTDIR/$BASENAME -a $DESTDIR/$BASENAME -nt $BACKUPDIR/$BASENAME ]; then
	# backup is old
	echo "backup-file $BASENAME is old"
    else
	echo "Restore backup-file $BASENAME"
	cp -p $BACKUPDIR/$BASENAME $DESTDIR
    fi

done

exit 0

