#!/usr/bin/bash


help() 
{
  cat << EOF

$cmd is an script to help to identify modifications introduced on a DevEnv machine with respect to the official installation. It can be executed as any user.

Please notice some limitations: 
  - RPMs specific to REALTIME are excluded from the comparison. 
  - There is a reduced set of RPMs where release-numbers are never compared, 
    i.e. CollabNetSubversion-client, oracle-jdk, eclipse-mars, waf, cppcheck, 
    gtest, cpplint, gcovr, checstyle, findbugs, anaconda-eso, llvm-es, vlt-ds9,
    cpl-eso, elt-common, protobuf-*, TestNG-java and wget.
  - Some differences may appear with RPM modules that allows several releases 
    to coexist in parallel. E.g. your system may have kernel-rel-1 and 
    kernel-rel.2, but the official RPM list refers to the last one. 
  - A return "RPM list totally complaint" is not a certificate of compliance 
    with the release. For such compliance, execute as root the command:
    /root/elt/puppet-check        # to see if it is compliant
    /root/elt/puppet-force-aline  # to force to be compliant. 

Options:
$cmd -i|--info  compares the RPM list in this machine with the 
                official RPM list for this DevEnv release. 
                If the diff is 0 the command will report "Totally compliant" 
                and it exits with 0.
                Otherwise it will report the differences with lines like:
                Found: package-rel-X.Y.Z      # in this machine
                Expected: package-rel.a.b.c   # in the official release
$cmd -h|--help  prints this text.
EOF
}

info()  {
mkdir /tmp/$$

DevEnvRel=`rpm -q puppet-elt | sed -e 's/puppet-elt-//' -e 's/.noarch//'`
echo "ELT Dev Env version: $DevEnvRel"

rm -f /tmp/$$/res
#echo "Latest release of the ELT DevEnv:"
yum check-update puppet-elt > /tmp/$$/res 2>&1
res=$?

if [ $res -eq 1 ]; then
  echo "Error running \"yum check-update puppet-elt\""
  cat /tmp/$$/res
  rm -rf /tmp/$$
  exit 1
elif [ $res -ne 0 ]; then
  echo -n "ELT Dev Env lastest version available: "
  tail -n 1 /tmp/$$/res | gawk '{print $2}' | sed 's/-2//'
fi

# Retrieve official list
wget http://www.eso.org/~eltmgr/rpm-list/rpm-list-$DevEnvRel >/dev/null 2>&1
mv rpm-list-$DevEnvRel /tmp/$$
if [ ! -f /tmp/$$/rpm-list-$DevEnvRel ]; then
  echo "Official rpm-list for release $DevEnvRel does not exist"
  rm -rf /tmp/$$
  exit 1
fi

rpm -qa | sed \
  -e 's/\(CollabNetSubversion-client\).*/\1/' \
  -e 's/\(oracle-jdk\).*/\1/' \
  -e 's/\(eclipse-mars\).*/\1/' \
  -e 's/\(eclipse-eclipse\).*/\1/' \
  -e 's/\(waf\).*/\1/' \
  -e 's/\(cppcheck\).*/\1/' \
  -e 's/\(gtest\).*/\1/' \
  -e 's/\(cpplint\).*/\1/' \
  -e 's/\(gcovr\).*/\1/' \
  -e 's/\(checkstyle\).*/\1/' \
  -e 's/\(findbugs\).*/\1/' \
  -e 's/\(anaconda-eso\).*/\1/' \
  -e 's/\(llvm-eso\).*/\1/' \
  -e 's/\(vlt-ds9\).*/\1/' \
  -e 's/\(cpl-eso\).*/\1/' \
  -e 's/\(elt-common\).*/\1/' \
  -e 's/\(protobuf\).*/\1/' \
  -e 's/\(protobuf-compiler\).*/\1/' \
  -e 's/\(protobuf-devel\).*/\1/' \
  -e 's/\(protobuf-java\).*/\1/' \
  -e 's/\(TestNG-java\).*/\1/' \
  -e 's/\(wget\).*/\1/' | sort > /tmp/$$/rpm-list-current

# If this a REALTIME remove those RPMs before comparing
if [ "$ELT_ROLE" = "REALTIME" ]; then
  cat /tmp/$$/rpm-list-current | \
  egrep -v "kernel-rt|tuna|tuned-profiles|rteval|trace-cmd|rt-setup|rt-tests|rtcheck|rtctl" > /tmp/$$/rpm-list-current.tmp
  mv /tmp/$$/rpm-list-current.tmp /tmp/$$/rpm-list-current
fi 

cat /tmp/$$/rpm-list-$DevEnvRel | sed \
  -e 's/\(CollabNetSubversion-client\).*/\1/' \
  -e 's/\(oracle-jdk\).*/\1/' \
  -e 's/\(eclipse-mars\).*/\1/' \
  -e 's/\(waf\).*/\1/' \
  -e 's/\(cppcheck\).*/\1/' \
  -e 's/\(gtest\).*/\1/' \
  -e 's/\(cpplint\).*/\1/' \
  -e 's/\(gcovr\).*/\1/' \
  -e 's/\(checkstyle\).*/\1/' \
  -e 's/\(findbugs\).*/\1/' \
  -e 's/\(anaconda-eso\).*/\1/' \
  -e 's/\(llvm-eso\).*/\1/' \
  -e 's/\(vlt-ds9\).*/\1/' \
  -e 's/\(cpl-eso\).*/\1/' \
  -e 's/\(elt-common\).*/\1/' \
  -e 's/\(protobuf\).*/\1/' \
  -e 's/\(protobuf-compiler\).*/\1/' \
  -e 's/\(protobuf-devel\).*/\1/' \
  -e 's/\(protobuf-java\).*/\1/' \
  -e 's/\(TestNG-java\).*/\1/' \
  -e 's/\(wget\).*/\1/' | sort > /tmp/$$/rpm-list-official

# Compare both official with current

diff /tmp/$$/rpm-list-current /tmp/$$/rpm-list-official > /tmp/$$/rpm-diff
res=$?
if [ $res -eq 0 ]; then
  echo "Status: fully compatible"
  echo ""
  rm -rf /tmp/$$
  exit 0
fi

echo "Status: modifications found"
# Clean the diff for those RPM that are allowed to change
cat /tmp/$$/rpm-diff | egrep '^[<>]'  > /tmp/$$/rpm-diff-clean

cat /tmp/$$/rpm-diff-clean | sed -e 's/< /Found: /' -e 's/> /Expected: /'
echo ""
rm -rf /tmp/$$
exit 1
}


#
# MAIN main
#
cmd=`basename $0`

if [ -z "$1" ]; then
  echo "Error: at least one argument is needed"
  echo "Usage: $cmd [-h|--help|-i|--info]"
  exit 1
fi

#
# Read arguments
#
while test $# != 0
do
  case $1 in
  -h|--help) help; exit 0 ;;
  -i|--info) info; shift ;;
  *) echo "$cmd: argument $1 unexpected" 
     echo "Usage: $cmd [-h|--help|-i|--info]"; exit 1 ;;
  esac
done


