#!/bin/sh

if [[ -e /etc/yum/vars/elstoken ]]; then
    echo "centos6-els-release has already been updated to the latest version"
    exit 0
fi

token=$(cat /etc/yum.repos.d/centos6-els.repo | grep 'baseurl' | rev | cut -d '/' -f 3 | rev);

echo "[centos6-els-release]" > /etc/yum.repos.d/centos-els-release-update.repo;
echo "name = CentOS 6 Extended Lifecycle Support by CloudLinux" >> /etc/yum.repos.d/centos-els-release-update.repo;
echo "baseurl = https://repo.cloudlinux.com/centos6-els/AUTH_TOKEN/centos-els-release/" >> /etc/yum.repos.d/centos-els-release-update.repo;
echo "enabled = 1" >> /etc/yum.repos.d/centos-els-release-update.repo;
echo "gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux" >> /etc/yum.repos.d/centos-els-release-update.repo;
echo "gpgcheck=1" >> /etc/yum.repos.d/centos-els-release-update.repo;

sed -i "s/AUTH_TOKEN/${token}/g" /etc/yum.repos.d/centos-els-release-update.repo;

yum update --disablerepo=* --enablerepo=*els-release --nogpgcheck -y centos-els-release;

rm -f /etc/yum.repos.d/centos-els-release-update.repo;
