#! /bin/bash

mkdir -p /tmp/check4repositoriessupdates.$$
TMPFILE=`mktemp /tmp/check4repositoriessupdates.$$/XXXXXXXXXX` || exit 1
TMP1=`mktemp /tmp/check4repositoriessupdates.$$/XXXXXXXXXX` || exit 1
repositories=$(/sbin/e-smith/db configuration getprop check4updates repositories) || exit 1 
excludes=$(/sbin/e-smith/db configuration getprop check4updates excludes) || exit 1
MAILADDR=$(/sbin/e-smith/db configuration getprop check4updates emailaddress)|| exit 1

yum -e 0 -d 0 check-update --disablerepo=* --enablerepo=$repositories --exclude=$excludes > $TMP1
if [ $? = 100 ]; then
  echo -e "===\n=== yum reports available updates for $repositories :\n===" >> $TMPFILE
  cat $TMP1 >> $TMPFILE
 echo -e "\n    To apply all these updates, you can log on your server and run the following command :\n
    yum update --enablerepo=$repositories --exclude=$excludes\n" >> $TMPFILE
fi

if [ -s $TMPFILE ]; then
  if [ "$1" = "-m" ]; then
    mail -s "Repositories($repositories) Updates available for `hostname`" $MAILADDR < $TMPFILE
  else
    echo "Repositories($repositories) Updates available for `hostname`"
    cat $TMPFILE
  fi
fi

rm -f $TMPFILE $TMP1
rm -fr /tmp/check4repositoriessupdates.*

