#! /bin/sh
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Burchard Steinbild, 1996-97
#         Florian La Roche, 1996
# Please send feedback to http://www.suse.de/feedback
#

test -z "$MD5DIR" && {
    MD5DIR=/var/adm/SuSEconfig/md5
    echo "Warning! MD5DIR is not set: you probably called this script outside SuSEconfig...!"
    echo "Using MD5DIR=\"$MD5DIR\"..."
}

echo_warning ()        # Usage: echo_warning out_file dependency-string
{
    echo "#"
    echo "# `dirname $1`/`basename $1 .SuSEconfig`"
    echo "#"
    echo "# Automatically generated by SuSEconfig on $(date)."
    echo "#"
    echo "# PLEASE DO NOT EDIT THIS FILE!"
    echo "#"
    echo "$2"
    echo "#"
    echo "#" 
}

check_md5_and_move() # Usage: check_md5_and_move file_name-without.SuSEconfig
{
    # This function checks the existence of a file (specified without the
    # extension .SuSEconfig and without "$r") and a corresponding md5 checksum
    # and tests whether the time stamp of the file has changed.
    # If it has, nothing further will happen. If not, the "file.SuSEconfig"
    # will be moved to "file".

    FILE=$1
    if test -n "$r" ; then
        RELPATH=`echo $FILE | sed -e"s:^$r::"`
    else
        RELPATH=$FILE
    fi
    MD5FILE=$MD5DIR/$RELPATH
    #
    # make sure that the directory exists
    mkdir -p `dirname $MD5FILE`
    NEWMD5SUM="`cat $FILE.SuSEconfig | grep -v "^#" | md5sum`"
    if test ! -s $FILE ; then
        touch $FILE
        rm -f $MD5FILE
    fi
    if test "$FORCE_REPLACE" = true ; then
        cp -p $FILE.SuSEconfig $FILE
    fi
    USERMD5SUM="`cat $FILE | grep -v "^#" | md5sum`"
    test -e $MD5FILE || echo "$USERMD5SUM" > $MD5FILE
    OLDMD5SUM="`cat $MD5FILE`"
    if test "$USERMD5SUM" != "$OLDMD5SUM" -a \
            "$USERMD5SUM" != "$NEWMD5SUM" ; then
        echo
        echo "ATTENTION: You have modified $RELPATH.  Leaving it untouched..."
        echo "You can find my version in $FILE.SuSEconfig..."
        echo
    else
        if test "$USERMD5SUM" != "$NEWMD5SUM" -o "$FORCE_REPLACE" = true ; then
            echo "Installing new $RELPATH"
            cp -p $FILE.SuSEconfig $FILE
        else
            test "$VERBOSE" = false || echo "No changes for $RELPATH"
        fi
        rm -f $FILE.SuSEconfig
    fi
    rm -f $MD5FILE
    echo "$NEWMD5SUM" > $MD5FILE
}

my_test_write () {  # function to test if a directory is really writable
                    # (think about ro mounted nfs systems)
    local TESTFILE=$1/.my_test_write.SuSEconfig
    local RETURN=false
    touch $TESTFILE 2> /dev/null && RETURN=true
    rm -f $TESTFILE $TESTFILE.*
    eval $RETURN
}

my_test_for_space () { # test if we have enough space
    local RETURN=true
    my_test_write $1 && {
      dd if=/dev/zero of=$1/testforspace.SuSEconfig bs=1k count=50 \
        2> /dev/null  || {
        rm -f $1/testforspace.SuSEconfig
        echo
        echo
        echo We do not have enough disk space in $1 or /dev/zero
        echo does not exist. Exit.
        echo
        RETURN=false
      }
      rm -f $1/testforspace.SuSEconfig
    }
    eval $RETURN
}
