#!/bin/sh
#
# This script does a yum upgrade
#set -x

log() {
  DATE=`date`
  HOST=`hostname`
  BASE=`dirname $0`
  echo $*
  echo $DATE $* >> $BASE/$HOST.log
}

if [ -f /etc/nbe/mynbe.cfg ]
then
  . /etc/nbe/mynbe.cfg
  NEWREV=`curl -s "http://cms.oneip.nl/service/get_build.php?prod=$MYPROD&version=$MYPRODREL&track=$MYPRODSTABILITY"`
  log "prod=$MYPROD,version=$MYPRODREL,track=$MYPRODSTABILITY,newrev=$NEWREV" 
  if [ "$MYPRODSTABILITY" != "A" ]
  then
    if [ "$MYREVISION" != "$NEWREV" ]
    then
      log "going to update MYREVISION from $MYREVISION to $NEWREV"
      sed -i s/^MYREVISION=.*/MYREVISION=$NEWREV/g /etc/nbe/mynbe.cfg
    else 
      log "MYREVISION $MYREVISION == $NEWREV - doing nothing"
    fi
  else
    log "NOT going to update MYREVISION from $MYREVISION to $NEWREV - this is the A version"
  fi
fi

log "running yum-update-vm"

find /tmp -name yum-update-vm.lock -mtime +1 -exec rm -f {} \;
LOCKFILE=/tmp/yum-update-vm.lock
if [ -f $LOCKFILE ]
then
  log "locked by $LOCKFILE"
  exit
fi
touch $LOCKFILE
trap "{ rm -f $LOCKFILE ; exit 0; }" EXIT

if [ ! -f /usr/sbin/yum-complete-transaction ]
then
  yum -y install yum-utils
fi

if [ ! -f /usr/bin/git ]
then
  yum -y install git
fi

if [ ! -f /usr/bin/lsb_release ]
then
  yum -y install redhat-lsb-core
fi

RHVER=`lsb_release -r | awk '{ print $2; }' | cut -d. -f1`

yum-complete-transaction -y
yum -q clean all
yum -q -y install kernel-devel
if [ $RHVER -lt 6 ]
then
  yum -q -y install kernel-xen-devel kernel-PAE-devel
fi
yum -q -y update 

if [ -f /usr/bin/astium-bb ]
then
  chattr -i /usr/bin/astium-bb
  chmod -x /usr/bin/astium-bb
  chmod -x /usr/local/astium/cron/astium.minute
  chmod -x /usr/local/astium/cron/astium.daily
fi

# stop everything from running
for service in asterisk astcfgd astiumd postgresql dahdi dhcpd 3dm2 upwatch vsftpd smartd httpd 
do
  if [ -f /etc/init.d/$i ]
  then
    /sbin/chkconfig --del $service
    /sbin/service $service stop
  fi
done

rm -f $LOCKFILE

