#!/bin/sh
#
#  yum-autoupdate
#	Designed to only send mail when something has been updated or installed
#
#  Written by Troy Dawson <dawson@fnal.gov>
#	Changed - May 7, 2003  by Troy Dawson <dawson@fnal.gov>
#	Rewrite - May 14, 2004  by Troy Dawson <dawson@fnal.gov>
#	Rewrite - March 1, 2005  by Troy Dawson <dawson@fnal.gov>
#	Rewrite - January 25, 2011  by Troy Dawson <dawson@fnal.gov>
#     This rewite (version 2) used a central config file for everything
#
# Copyright 2003, 2004, 2005, 2011 Troy Dawson
#
# This copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the GNU
# General Public License v.2.  This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 
#
########################3
# VARIABLES
# Variables such as debug, and who to send the email to
#  are now in the config file /etc/sysconfig/yum-autoupdate
# There should be no reason to change this script unless
#  you are changing the functionality
########################3

. /etc/sysconfig/yum-autoupdate

#*******************************************
#sleep for a random amount of minutes
#*******************************************	
random_sleep() {
	utime=`cat /proc/uptime | cut -d'.' -f1`
	if [ $utime -ge 72000 ] ; then
		RAN=$RANDOM
		if [ "$RAN" = "0" ] ; then
			RAN=1
		fi
		if [ $MAXWAITTIME -le 0 ] ; then
			MAXWAITTIME=1
		fi
		SLEEPTIME=$(($RAN % $MAXWAITTIME))
		if [ "$DEBUG" = "true" ] ; then
			echo "  Sleeping for $SLEEPTIME minutes"
		fi
		sleep ${SLEEPTIME}m
	fi
}

#*******************************************
# check to see if yum is running, and if it is, for how long
# If it has been running close to a day, it's time
# to kill it and try again.
#*******************************************	
check_yum() {
	if [ -f /var/run/yum.pid ] ; then
		YUMPID=`cat /var/run/yum.pid`
		if [ -d /proc/${YUMPID} ] ; then
			OLDTIME=`cat /proc/${YUMPID}/stat | cut -d' ' -f22`
			NOWTIME=`cat /proc/self/stat | cut -d' ' -f22`
			let DIFFTIME=${NOWTIME}-${OLDTIME}
			if [ ${DIFFTIME} -ge 5000000 ] ; then
				if [ "$DEBUG" = "true" ] ; then
					echo "  Yum PID ${YUMPID}, Running for ${DIFFTIME} jiffies"
					echo "    Started ${OLDTIME}, Now ${NOWTIME}"
					echo "    This is more than the limit of 5000000 jiffies"
					echo "    Killing process ${YUMPID}"
				fi	
				#one last sanity check
				grep -q yum /proc/${YUMPID}/stat
				if [ "$?" = "0" ] ; then
					kill -9 ${YUMPID}
				fi
			else
				if [ "$DEBUG" = "true" ] ; then
					echo "  Yum PID ${YUMPID}, Running for ${DIFFTIME} jiffies"
					echo "    This is less than the limit of 5000000 jiffies"
				fi	
			fi
		else
			if [ "$DEBUG" = "true" ] ; then
				echo "  Yum PID ${YUMPID}, but no process running"
			fi	
		fi
	else
		if [ "$DEBUG" = "true" ] ; then
			echo "  No other yum process running."
		fi	
	fi
}

#******************************
#Start the program
#But only if we're supposed to
#******************************
if [ "$ENABLED" = "true" ]; then
	if [ "$DEBUG" = "true" ] ; then
		echo "Yum crontab starting"
	fi
	#****************************
	#VARIABLES
	#****************************
	CHANGE=0
	TEMPFILE="/tmp/yum.temp"
	TEMPCONFIGFILE="/tmp/yum.temp.config"
	TEMPMAILFILE="/tmp/yum.temp.mail"
	TEMPRANDFILE="/tmp/yum.temp.rand"
	TODAY="$(date +%Y-%m-%d)"
		
	
	#****************************
	#Setup Enviroment
	#****************************
	rm -f $TEMPMAILFILE
	rm -f $TEMPCONFIGFILE
	rm -f $TEMPFILE

	#****************************
	#We are doing our own sleep here
	#instead of in yum so that each
	#of the modules can be added
	#without worrying which one
	#has the randome time in it
	#****************************
	random_sleep
	#****************************
	#We are are checking the yum process
	#here because we were finding that 
	#yum would get stuck with the right
	#ftp problems
	#****************************
	check_yum
	

  #****************************
  #START Errata MODULE
  #****************************
	#The errata module is to install
	#errata updates to the packages
	#already on a machine.
	#****************************
	MODULE="security"
	REPOFILE="security"
	HEAD="baseurl="
	TAIL="/errata/SL/RPMS/"
	
	if [ "$DEBUG" = "true" ] ; then
		echo "********** Starting ***** $MODULE ***** STARTING ********"
		echo "   Creating Config File"
	fi
	#****************************
	# Create yum.conf for this module
	# We will do that by simply taking the original yum.conf file
	# and adding the yum.cron.excludes to it
	#****************************
	rm -f $TEMPCONFIGFILE
	grep -q "exclude=" $CONFIGFILE
	if [ $? -eq 0 ] ; then
		excludelist="$EXCLUDE"
		cat $CONFIGFILE | sed -e "s:exclude=.*:& $excludelist:" > $TEMPCONFIGFILE
	else
		excludelist="exclude=$EXCLUDE"
		cat $CONFIGFILE | sed -e "s:\[main\].*:&\n$excludelist:" > $TEMPCONFIGFILE	
	fi
	if [ "$DEBUG" = "true" ] ; then
		echo "*********START** $MODULE YUM.CONF FILE **START**********"
		cat $TEMPCONFIGFILE
		echo "**********END*** $MODULE YUM.CONF FILE ***END***********"
	fi
	#****************************
	#Run yum and record if anything happens
	#****************************
	if [ "$DEBUG" = "true" ] ; then
		echo "    Starting Yum with command"
		echo "     /usr/bin/yum -c $TEMPCONFIGFILE -e 0 -d 1 -y update"		
	fi
	/usr/bin/yum -c $TEMPCONFIGFILE -e 0 -d 1 -y update > $TEMPFILE 2>&1
	if [ -s $TEMPFILE ] ; then
		if [ "$DEBUG" = "true" ] ; then
			echo "    We had changes"
			echo "*********START** ${MODULE} CHANGES FILE **START**********"
			cat $TEMPFILE
			echo "**********END*** ${MODULE} CHANGES FILE ***END***********"
		fi
		CHANGE=1
		echo " --------------------" >> $TEMPMAILFILE
		echo " YUM - ${MODULE}" >> $TEMPMAILFILE
		echo " --------------------" >> $TEMPMAILFILE
		cat $TEMPFILE >> $TEMPMAILFILE
	fi
	#****************************
	#Cleanup ${MODULE}
	#****************************
	if [ "$DEBUG" = "true" ] ; then
		echo " Cleaning Up ${MODULE}"
	fi
	yum -e 0 -d 0 -c $TEMPCONFIGFILE clean packages
	mv -f $TEMPCONFIGFILE /tmp/yum.conf.${MODULE}
  #****************************
  #END Errata MODULE
  #****************************

	
	#****************************
	#Send Mail If Needed
	#****************************
	if [ $CHANGE -eq 1 ] ; then
		if [ "$DEBUG" = "true" ] ; then
			echo " Mail Needs To Be Sent"
			echo "  /bin/mail -s \"YUM:$HOSTNAME:$TODAY\" $MAILLIST "
		fi
		/bin/mail -s "YUM:$HOSTNAME:$TODAY" $MAILLIST < $TEMPMAILFILE
	fi

	#****************************
	#Cleanup
	#****************************
	if [ "$DEBUG" = "true" ] ; then
		echo " Cleaning Up Interactive Yum"
	fi
	yum -e 0 -d 0 clean packages
	yum -e 0 -d 0 provides xyz > /dev/null 2>&1
	rm -f $TEMPMAILFILE
	rm -f $TEMPCONFIGFILE
	rm -f $TEMPRANDFILE
	rm -f $TEMPFILE
	if [ "$DEBUG" = "true" ] ; then
		echo "Yum-Autoupdate Exiting succesfully"
	fi
else
	if [ "$DEBUG" = "true" ] ; then
		echo "Yum-Autoupdate not enabled.  Exiting"
	fi
	exit 1
fi
exit 0
