#! /bin/bash
# Copyright (c) 2002 SuSE GmbH Nuernberg, Germany.
#
# Author: Michael Schroeder <feedback@suse.de>
#
# /etc/init.d/rpmconfigcheck
# /usr/sbin/rcrpmconfigcheck
#
# Script to scan for unresolved .rpmnew, .rpmorig, and .rpmsave files
#
### BEGIN INIT INFO
# Provides: rpmconfigcheck
# Required-Start: $remote_fs
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Description: rpm config file scan
### END INIT INFO

. /etc/rc.status

# First reset status of this service
rc_reset

configcheckfile=/var/adm/rpmconfigcheck
packages=/var/lib/rpm/Packages

test -z "$1" && set start

case "$1" in
    start|restart|try-restart|reload|force-reload)
	if test -s $packages -a \( ! -e $configcheckfile -o -s $configcheckfile -o ! $packages -ot $configcheckfile \) ; then
	    echo -n "Searching for unresolved configuration files"
	    if test ! -e $configcheckfile -o ! $packages -ot $configcheckfile ; then
		test -e $configcheckfile && mv -f $configcheckfile $configcheckfile.old
		rpm -qalc | sort | perl -lne '-e "$_.rpmnew" and print "$_.rpmnew"; -e "$_.rpmorig" and print "$_.rpmorig"; -e "$_.rpmsave" and print "$_.rpmsave"' > $configcheckfile
	    else
		mv -f $configcheckfile $configcheckfile.old
		while read l; do
		    test -e $l && echo $l
		done < $configcheckfile.old > $configcheckfile
		true
	    fi
	    rc_status -v
	    if test -s $configcheckfile; then
		echo "Please check the following files (see /var/adm/rpmconfigcheck):"
		sed -e 's/^/    /' < $configcheckfile
		touch $configcheckfile.old
		cat $configcheckfile $configcheckfile.old | sort | uniq -d > $configcheckfile.dup
		cat $configcheckfile $configcheckfile.dup | sort | uniq -u > $configcheckfile.new
		if test -s $configcheckfile.new ; then
		    (
		    echo "----------------------------------------------------------------------"
		    echo "----------------------------------------------------------------------"
		    echo "rpmconfigcheck"
		    date
		    echo "----------------------------------------"
		    echo "This is a warning message."
		    echo "rpmconfigcheck has found the following new unresolved config files"
		    echo "(all files are listed in /var/adm/rpmconfigcheck):"
		    cat $configcheckfile.new
		    echo "----------------------------------------"
		    ) >> /var/log/update-messages
		fi
	    fi
	    rm -f $configcheckfile.old $configcheckfile.dup $configcheckfile.new
	fi
	;;
    stop|status)
	;;
     *)
	echo "Usage: $0 {start}"
	exit 1
	;;
esac
rc_exit
