#!/usr/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" ]; then
    echo "Enabling Automatic Updates"
    # 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
        systemctl enable --now yum-cron
    elif [ -f "/etc/dnf/automatic.conf" ]; then
        sed -i "s/download_updates = .*/download_updates = yes/g" /etc/dnf/automatic.conf
        sed -i "s/apply_updates = .*/apply_updates = yes/g" /etc/dnf/automatic.conf
        systemctl enable --now dnf-automatic.timer
    else
        echo "Unable to find a recognized automatic updater."
    fi
    
fi

