#!/bin/sh
###############################################################################
# This script will attempt to provide build information for each of the 
# files on the command line.
#
set -u # Undefined variables are errors.

# Should we print the config spec for each file?
PRINT_CONFIG_SPEC="no"

# Locate commands
if [ -f /opt/bin/gawk ] ;      then GAWK=/opt/bin/gawk;      else GAWK=/usr/bin/gawk; fi
if [ -f /bin/sed ] ;           then SED=/bin/sed;            else SED=/usr/bin/sed;   fi
if [ -f /bin/grep ] ;          then GREP=/bin/grep;          else GREP=/usr/bin/grep; fi
if [ -f /bin/cat ] ;           then CAT=/bin/cat;            else CAT=/usr/bin/cat;   fi
if [ -f /usr/local/bin/rpm ] ; then RPM=/usr/local/bin/rpm;   elif [ -f /bin/rpm ] ; then RPM=/bin/rpm; else RPM=/usr/bin/rpm;   fi
if [ -f /usr/bin/ident ] ;     then IDENT=/usr/bin/ident;    else IDENT=/bin/ident;   fi

# Validate we have at least one argument.
if [ "$#" == "0" ] ; then
    echo "Use: $0 file_to_query [-print-config-spec] [file_to_query ...]"
    exit 1
fi

# Process each file.
while [ "$#" != "0" ] ; do
    F="$1" ; shift;

    if [ "$F" == "-print-config-spec" ] ; then
        PRINT_CONFIG_SPEC="yes"
    else
        echo "##### $F #####"

        #################################################################
        # First let us look to the RPM information
        echo " RPM Reports"
        PKG=`$RPM --query --file $F`

        if [ "$?" == "0" ] ; then
            echo " - RPM: ${PKG}"

            # See: http://www.novell.com/coolsolutions/feature/16238.html
            # for a nice description of the format of DELTAS
            DELTAS=`$RPM --verify --file $F | $GAWK '{print $1}'`
            IS_DIF="no"
            SHOW_BUILD_INFO="yes"

            if [ "`echo ${DELTAS} | $GREP S`" != "" ] ; then
                echo " - Size is different."
                IS_DIF="yes"
                SHOW_BUILD_INFO="no"
            fi
            if [ "`echo ${DELTAS} | $GREP M`" != "" ] ; then
                echo " - Permissions or type is different."
                IS_DIF="yes"
            fi
            if [ "`echo ${DELTAS} | $GREP 5`" != "" ] ; then
                echo " - MD5 checksum is different."
                IS_DIF="yes"
                SHOW_BUILD_INFO="no"
            fi
            if [ "`echo ${DELTAS} | $GREP L`" != "" ] ; then
                echo " - Link path difference."
                IS_DIF="yes"
                SHOW_BUILD_INFO="no"
            fi
            if [ "`echo ${DELTAS} | $GREP U`" != "" ] ; then
                echo " - User is different."
                IS_DIF="yes"
            fi
            if [ "`echo ${DELTAS} | $GREP G`" != "" ] ; then
                echo " - Group is different."
                IS_DIF="yes"
            fi
            if [ "`echo ${DELTAS} | $GREP T`" != "" ] ; then
                echo " - Timestamp is different."
                IS_DIF="yes"
            fi
            if [ "${IS_DIF}" == "no" ] ; then
                echo " - File matches RPM."
            fi

            if [ "${SHOW_BUILD_INFO}" == "yes" ] ; then
                BUILD_INFO_FILE="/build_info/${PKG}"

                if [ -r "${BUILD_INFO_FILE}" ] ; then
                    PRINT="yes"

                    # We must supress the config spec content.
                    # Convert spaces to ___ to preserve each line.
                    for line in `$CAT "${BUILD_INFO_FILE}" | $SED 's/ /___/g'` ; do
                        if [ "${PRINT_CONFIG_SPEC}" != "yes" ] ; then
                            # We are not meant to print the config spec.  
                            # If we have reached the first line then disable printing.
                            if [ "$line" == "====___Config___Spec___====" ] ; then
                                PRINT="no"
                            fi
                        fi
                        if [ "${PRINT}" == "yes" ] ; then
                            # Convert the ___ back to space as we print.
                            echo "   $line" | $SED 's/___/ /g'
                        fi
                    done
                else
                    echo " - Cannot read ${BUILD_INFO_FILE}"
                fi
            fi
        else
            echo " - Not part of an RPM"
        fi

        #################################################################
        echo " Ident Reports"
        IDENT_REPLY=`$IDENT -q $F | $GREP -e ":[^\s]"`
 
        if [ "${IDENT_REPLY}" != "" ] ; then
            echo "${IDENT_REPLY}" | $SED 's/^ *//' | $SED 's/^/   /'
        else
            echo " - No ident information"
        fi
        echo ""
    fi
done

