#!/bin/bash

# Enables automatic updates by default

PREV_VERSION=$2

#Need to activate if new install or if they were previously running 3.3.2 or older
if [ "$1" == "new" -o `echo "${PREV_VERSION:0:3} < 3.4" | bc` -eq 1 ]; then
    echo "Enabling Automatic Updates"
    chkconfig --add yum-cron
    chkconfig yum-cron on
fi

#Handle updating configs here
grep "<service yum_cron>" /opt/perfsonar_ps/toolkit/etc/config_daemon.conf > /dev/null 2>&1
if [ $? != 0 ]; then
    sed -i '/<access>/a \\t<service yum_cron> \
		\t\trestart	\t\t 1 \
		\t\tstart \t\t 1 \
		\t\tstop \t\t 1 \
	\t</service>' /opt/perfsonar_ps/toolkit/etc/config_daemon.conf
fi

# Use the enabled_services file to check if we need to enable auto updates by
# default (i.e. it's an upgrade of a pre-3.4 host)
grep "yum_cron_enabled" /opt/perfsonar_ps/toolkit/etc/enabled_services > /dev/null 2>&1
if [ $? != 0 ]; then
    echo "" >> /opt/perfsonar_ps/toolkit/etc/enabled_services
    echo "yum_cron_enabled=enabled" >> /opt/perfsonar_ps/toolkit/etc/enabled_services
fi
