#!/bin/sh

LOCKFILE=/var/lock/yum-cron.lock

# Only run if this flag file is set (by /etc/rc.d/init.d/yum-cron)
if [ ! -f /var/lock/subsys/yum-cron ]; then
  exit 0
fi

# Try mkdir for the lockfile, will test for and make it in one atomic action
mkdir $LOCKFILE 2>/dev/null || exit
# and clean up that lockfile if the script exits or is killed  
trap "{ rmdir $LOCKFILE 2>/dev/null ; exit 255; }" INT TERM EXIT

# Grab config settings
if [ -f /etc/sysconfig/yum-cron ]; then
  source /etc/sysconfig/yum-cron
fi

# if DOWNLOAD_ONLY is set then we force CHECK_ONLY too.
# Gotta check before one can download!
if [[ "x$DOWNLOAD_ONLY" == "xyes" ]]; then
  CHECK_ONLY=yes
fi

# Then check for updates and/or do them, as configured
if [[ "x$CHECK_ONLY" == "xyes" ]]; then
  if (! /usr/bin/yum -R 120 -e 0 -d 0 -y check-update >/dev/null); then
    echo "New updates available for host `/bin/hostname`";
    echo -e "`/usr/bin/yum -e 0 -d 0 -y -C check-update`\n"
    if [[ "x$DOWNLOAD_ONLY" == "xyes" ]]; then
      /usr/bin/yum -e 0 -d 0 -y --downloadonly update>/dev/null
      echo "Updates downloaded, use \"yum -C update\" manually to install them."
    fi
  fi
else
  /usr/bin/yum -R 120 -e 0 -d 0 -y update yum
  /usr/bin/yum -R 10 -e 0 -d 0 -y shell /etc/yum/yum-daily.yum
fi

exit 0
