#!/bin/bash
# Find installed RPM packages which have been locally modified and list the 
# differences from the original
# Authors: gcuni@cells.es, spuso@cells.es  (2016)

# find package $PACKAGE or the closest match, set is as $RPMPACKAGE
function find_package {

  FINDPACKAGE=`rpm -q $PACKAGE | grep -v "not installed"`;
  if [[ "x$FINDPACKAGE" = "x" ]] 
  then
      # Package name is not exact, looking for a match
      FINDPACKAGE=`rpm -qa | /usr/bin/grep -i -E "^$PACKAGE" `;
      if [[ "x$FINDPACKAGE" = "x" ]] 
      then
	echo "Package $PACKAGE not found in the list of installed packages"
	exit 1
      else
        RPMPACKAGE=`echo "$FINDPACKAGE" | head -n 1`	  
	echo "Package $PACKAGE not found in the list of installed packages, using closest match $RPMPACKAGE"
      fi
  else
      # Exact match for package name
      RPMPACKAGE=$FINDPACKAGE
  fi

}

# print the usage description and finish the script
function usage {
  echo "Usage: "
  echo "alba_find_package_modified				List all packages which have been locally modified"
  echo "alba_find_package_modified rpm_package_name 		List all locally modified files in package rpm_package_name"
  echo "alba_find_package_modified -d rpm_package_name 		Diff for each of the locally modified files in package rpm_package_name"
  exit 1
}


if grep -q -e 'openSUSE 11' -e 'openSUSE Leap 42.1' /etc/issue
then 
    # rpm verify in 11 and 42.1 (Leap) returns the number 5 in the third character
    REGEXPMD5MODIFIED='^..5..... '
else
    # rpm verify in 12.1 returns the number 5 in the first character
    REGEXPMD5MODIFIED='^5....... '
fi


if [ $# == 0 ];
then
  # For every package installed with RPM and ALBA-Controls vendor
  # verify it's MD5 flag ignoring the lines ending with .pyc
  for pack in `rpm -qa --queryformat "%{Vendor}\t%{Name}-%{Version}-%{Release}\n"  |grep "^ALBA-Controls" | cut -f 2-|sort`; do
      MODIFIED=`rpm -V --nodeps $pack | /usr/bin/grep -v \.pyc$ | /usr/bin/grep -E $REGEXPMD5MODIFIED`;
      if [[ ! "x$MODIFIED" = "x" ]] 
      then
	  if ! grep -q 'openSUSE 11'  /etc/issue
	  then
	    # Strip the final x86_64/i586 suffix from openSUSE 12 and later
	    pack=`echo $pack | sed 's/.x86_64$//'`
	    pack=`echo $pack | sed 's/.i586$//'`
	  fi
	  echo ""
	  echo "Package $pack has been locally modified"
	  echo "$MODIFIED"
	  echo ""
      fi
  done
  exit 0
fi


case $1 in
  -d)

    # Check bad usage: diff invoked but missing package name
    if [ $# == 1 ]; then usage; fi

    # Check bad usage: too many arguments
    if [ $# -gt 2 ]; then usage; fi

    PACKAGE=$2
    find_package

    ARCH=${RPMPACKAGE##*.}

    RPM_URL=http://controls01/testrepo/openSUSE_Leap_42.1/$ARCH/$RPMPACKAGE.rpm


    TDIR=`mktemp -d`

    trap "{ cd - ; rm -rf $TDIR; exit 255; }" SIGINT

    cd $TDIR
    PATHORIGINALRPM=$TDIR
    if ! wget -q -O $RPMPACKAGE.rpm $RPM_URL 
    then
	echo "ERROR: Could not download RPM package: $RPM_URL"
	exit 1
    fi
    rpm2cpio $RPMPACKAGE.rpm | cpio --quiet -vid &>/dev/null
    rm $RPMPACKAGE.rpm 


    
    # get list of modifiedfiles
    MODIFIEDFILES=`rpm -V --nodeps $RPMPACKAGE | /usr/bin/grep -v \.pyc$ | /usr/bin/grep -E $REGEXPMD5MODIFIED | cut -c 14-`

    if [[ "x$MODIFIEDFILES" = "x" ]]
    then
          echo "Package $RPMPACKAGE has not been locally modified"
    else
          echo "Package $RPMPACKAGE has been locally modified"
    fi


    # diff every modified file
    for mypkgfile in $MODIFIEDFILES
    do
        echo ""
        echo '#######' DIFFERENCES FOUND: $mypkgfile 
        diff -u $PATHORIGINALRPM/$mypkgfile $mypkgfile
        echo ""
    done
    
    # Cleanup - Remove tmp directory, etc..
    cd - >/dev/null
    rm -rf $TDIR

    exit 0
    ;;
  *)
    # Check bad usage: too many arguments
    if [ $# -gt 1 ]; then usage; fi

    PACKAGE=$1
    find_package

    MODIFIEDFILES=`rpm -V --nodeps $RPMPACKAGE | /usr/bin/grep -v \.pyc$ | /usr/bin/grep -E $REGEXPMD5MODIFIED`
    if [[ "x$MODIFIEDFILES" = "x" ]]
    then
          echo "Package $RPMPACKAGE has not been locally modified"
    else
          echo "Package $RPMPACKAGE has been locally modified"
          echo "$MODIFIEDFILES"
    fi


    exit 0
    ;;
esac


