#!/bin/bash
#
# Copyright (c) 2009 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 3 (GPLv3). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
# along with this software; if not, see
# http://www.gnu.org/licenses/gpl-3.0.txt.
#
# Red Hat trademarks are not licensed under GPLv3. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

#uncomment this line if you do not want really remove your files.
#DEBUG="echo DEBUG:"

print_help () {
        cat <<HELP
usage: rpmconf [options]

options:
        -a, --all
                check configuration files of all packages
        -opackage, --owner=package
                check only configuration files of given package
        -ftype, --frontend=type
                to be done
HELP
        exit
}

function toUpper {
  echo $1 | tr "[:lower:]" "[:upper:]" 
} 

function handle_rpmnew {
        CONF_FILE=$1
        OTHER_FILE=$2
        if diff "$CONF_FILE" "$OTHER_FILE" &>/dev/null; then 
                $DEBUG rm "$OTHER_FILE"
                return
        fi
        OPTION=""
        while [ "$OPTION" != "Y" -a "$OPTION" != "I" -a "$OPTION" != "N" -a "$OPTION" != "O" ]; do
                cat <<ASK
Configuration file \`$CONF_FILE'
 ==> Package distributor has shipped an updated version.
   What would you like to do about it ?  Your options are:
    Y or I  : install the package maintainer's version
    N or O  : keep your currently-installed version
      D     : show the differences between the versions
      Z     : background this process to examine the situation
 The default action is to keep your current version.
*** aliases (Y/I/N/O/D/Z) [default=N] ? 
ASK
                read -u 1 -p "Your choice: " OPTION
                if [ "$OPTION" = "" ]; then
                        OPTION="N"
                fi
		OPTION=`toUpper "$OPTION"`
                if [ "$OPTION" = "D" ]; then
                        diff -u "$CONF_FILE" "$OTHER_FILE" |less
                elif [ "$OPTION" = "Z" ]; then
			kill -STOP $$
		fi
        done
        if [ "$OPTION" = "N" -o "$OPTION" = "O" ]; then
                $DEBUG rm "$OTHER_FILE"
        elif [ "$OPTION" = "Y" -o "$OPTION" = "I" ]; then 
                $DEBUG cp "$OTHER_FILE" "$CONF_FILE"
                $DEBUG rm "$OTHER_FILE"
        fi
}

function handle_rpmsave {
        CONF_FILE=$1
        OTHER_FILE=$2
        if diff "$CONF_FILE" "$OTHER_FILE" &>/dev/null; then
                $DEBUG rm "$OTHER_FILE"
                return
        fi
        OPTION=""
        while [ "$OPTION" != "Y" -a "$OPTION" != "I" -a "$OPTION" != "N" -a "$OPTION" != "O" ]; do
                cat <<ASK
Configuration file \`$CONF_FILE'
 ==> Package distributor has shipped an updated version.
 ==> Maintainer forced upgrade. Your old version has been backed up.
   What would you like to do about it ?  Your options are:
    Y or I  : install (keep) the package maintainer's version
    N or O  : return back to your original file
      D     : show the differences between the versions
      Z     : background this process to examine the situation
 The default action is to keep package maintainer's version.
*** aliases (Y/I/N/O/D/Z) [default=Y] ?
ASK
                read -u 1 -p "Your choice: " OPTION
                if [ "$OPTION" = "" ]; then
                        OPTION="Y"
                fi
		OPTION=`toUpper "$OPTION"`
                if [ "$OPTION" = "D" ]; then
                        diff -u "$OTHER_FILE" "$CONF_FILE" |less
		elif [ "$OPTION" = "Z" ]; then
                        kill -STOP $$
                fi
        done
        if [ "$OPTION" = "N" -o "$OPTION" = "O" ]; then
                $DEBUG cp "$OTHER_FILE" "$CONF_FILE"
                $DEBUG rm "$OTHER_FILE"
        elif [ "$OPTION" = "Y" -o "$OPTION" = "I" ]; then
                $DEBUG rm "$OTHER_FILE"
        fi
}

function handle_packages {
        PACKAGE=$1
        for CONFFILE in `LANG= rpm -qc $PACKAGE |grep -v '(contains no files)'`; do
                if [ -f "$CONFFILE.rpmnew" ]; then
                        handle_rpmnew "$CONFFILE" "$CONFFILE.rpmnew"
                fi
                if [ -f $CONFFILE.rpmsave ]; then
                        handle_rpmsave "$CONFFILE" "$CONFFILE.rpmsave"
                fi
                if [ -f $CONFFILE.rpmorig ]; then
                        handle_rpmsave "$CONFFILE" "$CONFFILE.rpmorig"
                fi
        done
}

if [ $# -eq 0 ]; then
        print_help;
fi
while [ $# -ge 1 ]; do
    case $1 in
            --help | -h)  print_help;;
            -o*) PACKAGE=$(echo $1 | cut -c3-); handle_packages $PACKAGE;;
            --owner=*) PACKAGE=$(echo $1 | cut -d= -f2-); handle_packages $PACKAGE;;
            --all | -a) handle_packages -a;;
            *) echo Error: Invalid option $1
    esac
    shift
done


