#!/bin/sh
#
#
# backup_rc.config.
#
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
#
# please send bugfixes or comments to http://www.suse.de/feedback.
#
# paranoia settings
#
umask 022

PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

#
# get information from /etc/sysconfig
#
if [ -f /etc/sysconfig/backup ] ; then
    . /etc/sysconfig/backup
fi

#
# create backups archive of sysconfig files
#
if test -n "$RCCONFIG_BACKUP_DIR" -a -e /etc/sysconfig ; then
    mkdir -p $RCCONFIG_BACKUP_DIR
    OLD_MD5=""
    ETC_RCCONFIG=""
    test -f /etc/rc.config && ETC_RCCONFIG="/etc/rc.config"
    test -e $RCCONFIG_BACKUP_DIR/sysconfig_recent_md5 && \
        OLD_MD5="`cat $RCCONFIG_BACKUP_DIR/sysconfig_recent_md5`"
    NEW_MD5="`find $ETC_RCCONFIG /etc/sysconfig -type f | xargs cat | md5sum`"
    if test "$OLD_MD5" != "$NEW_MD5" ; then
        DATESTRING=`date +"%Y%m%d"`

        NEWNAME=$RCCONFIG_BACKUP_DIR/sysconfig-$DATESTRING.tar.gz
        NUMBER=2
        while [ -e $NEWNAME ] ; do
            NEWNAME=$RCCONFIG_BACKUP_DIR/sysconfig-$DATESTRING-$NUMBER.tar.gz
            NUMBER=`expr $NUMBER + 1`
        done

        if tar czfp $NEWNAME $ETC_RCCONFIG /etc/sysconfig 2>/dev/null ; then
            echo "$NEW_MD5" > $RCCONFIG_BACKUP_DIR/sysconfig_recent_md5
            test "$MAX_RCCONFIG_BACKUPS" -gt 0 2> /dev/null || MAX_RCCONFIG_BACKUPS=0
            NUMBER=1
            for BACKUPFILE in `ls -1 -t $RCCONFIG_BACKUP_DIR/sysconfig-*` ; do
                if test "$NUMBER" -gt "$MAX_RCCONFIG_BACKUPS" ; then
                    rm -f $BACKUPFILE
                fi
                NUMBER=`expr $NUMBER + 1`
            done
        else
            echo "ERROR!! can not backup sysconfig files"
	    echo "to $RCCONFIG_BACKUP_DIR."
            echo "Maybe there is not enough disk space."
            rm -f $NEWNAME
        fi
    fi
fi

exit 0
