#!/bin/bash

# Enables automatic updates by default
#
# This script can be given 0, 1 or 2 args:
# $1: installation type (i.e. new or upgrade)
# $2: version upgrading from (if installation type is upgrade)

# Set arbitrarily high version 99.99 if no version provided
PREV_VERSION=${2:-99.99}
MAJOR=${PREV_VERSION%%\.*}
REST=${PREV_VERSION/${MAJOR}\.}
MINOR=${REST%%\.*}

# Need to activate if new install or if they were previously running 3.3.2 or older
if [[ "$1" == "new" || $MAJOR -lt 3 || ( $MAJOR -eq 3 && $MINOR -lt 4 ) ]]; then
    echo "Enabling Automatic Updates"
    if type systemctl &>/dev/null; then
        systemctl enable yum-cron
    else
        chkconfig --add yum-cron
        chkconfig yum-cron on
    fi
fi

# Make sure download_updates and apply_updates are on in centos 7
if [ -f "/etc/yum/yum-cron.conf" ]; then
    sed -i "s/download_updates = .*/download_updates = yes/g" /etc/yum/yum-cron.conf
    sed -i "s/apply_updates = .*/apply_updates = yes/g" /etc/yum/yum-cron.conf
fi


# Handle updating configs here
grep "<service yum_cron>" /etc/perfsonar/toolkit/configdaemon.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>' /etc/perfsonar/toolkit/configdaemon.conf
fi
