#!/usr/bin/env bash

show_usage() {
    echo 'Usage: install-centos7-els-repo-ip.sh [OPTION]...'
    echo ''
    echo '  -f, --force         Force re-register if ELS is already installed'
    echo '  -d, --delete        Delete ELS from server'
    echo '  -v, --validate      Check if ELS is installed'
    echo '  -h, --help          Show this message and exit'
}

els_installed() {
    if [[ -f /etc/yum.repos.d/centos7-els.repo ]]; then
        return 0
    else
        return 1
    fi
}

for opt in "$@"; do
    case ${opt} in
        -f|--force)
            FORCE=true ; shift ;;
        -d|--delete)
            DELETE=true ; shift ;;
        -v|--validate)
            VALIDATE=true ; shift ;;
        -h|--help)
            show_usage ; exit 0 ;;
        -*|--*)
            echo; echo "Unrecognized option: ${opt}"; show_usage ; exit 1 ;;
    esac
done

if [[ -n $VALIDATE ]]; then
    if els_installed; then
        echo "Server is registered"
        exit 0
    else
        echo "Server is not registered"
        exit 1
    fi
fi

if [[ -n $FORCE ]]; then
    rm -rf /etc/yum.repos.d/centos7-els.repo
    rm -rf /etc/yum.repos.d/centos7-els-rollout.repo
fi

if [[ -n $DELETE ]]; then
    rm -rf /etc/yum.repos.d/centos7-els.repo
    rm -rf /etc/yum.repos.d/centos7-els-rollout.repo
    yum remove els-define -y
    echo "CentOS ELS deleted successfully"
    exit
fi

# check centos-release file
if [[ ! -f /etc/centos-release ]]; then
    echo "This server is not RHEL based"
    exit 1
fi

# check centos version
centos_release="$(cat /etc/centos-release)"
if [[ ! "${centos_release}" == *"CentOS Linux release 7.9"* ]]; then
    echo "This server is not CentOS Linux release 7.9"
    exit 1
fi

# check if els is installed
if els_installed; then
    echo "This server has already installed ELS repo"
    echo "For re-registration run script with --force"
    exit 1
fi

# download and import GPG key
wget -nv https://repo.tuxcare.com/tuxcare/RPM-GPG-KEY-TuxCare -O /etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
[ $? -eq 0 ]  || exit 1

# create yum repo config
cat <<EOT >> /etc/yum.repos.d/centos7-els.repo
[centos7-els]
name = CentOS 7 Extended Lifecycle Support by TuxCare
baseurl = https://repo.tuxcare.com/centos7-els/updates/\$basearch/
enabled = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
gpgcheck=1

[centos7-els-testing]
name = CentOS 7 Extended Lifecycle Support by TuxCare
baseurl = https://repo.tuxcare.com/centos7-els/updates-testing/\$basearch/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
gpgcheck=1

[centos7-els-debug]
name = CentOS 7 Extended Lifecycle Support Debug by TuxCare
baseurl = https://repo.tuxcare.com/centos7-els/updates/\$basearch-debug/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
gpgcheck=1
EOT

cat <<EOT >> /etc/yum.repos.d/centos7-els-rollout.repo
[centos7els-rollout-1]
name=CentOS 7 ELS - Gradual Rollout Slot 1
baseurl=https://els-rollout.tuxcare.com/slot-1/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-1-source]
name=CentOS 7 ELS - Gradual Rollout Slot 1 Source
baseurl=https://els-rollout.tuxcare.com/slot-1/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-1-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 1 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-1-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-2]
name=CentOS 7 ELS - Gradual Rollout Slot 2
baseurl=https://els-rollout.tuxcare.com/slot-2/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-2-source]
name=CentOS 7 ELS - Gradual Rollout Slot 2 Source
baseurl=https://els-rollout.tuxcare.com/slot-2/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-2-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 2 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-2-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-3]
name=CentOS 7 ELS - Gradual Rollout Slot 3
baseurl=https://els-rollout.tuxcare.com/slot-3/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-3-source]
name=CentOS 7 ELS - Gradual Rollout Slot 3 Source
baseurl=https://els-rollout.tuxcare.com/slot-3/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-3-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 3 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-3-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-4]
name=CentOS 7 ELS - Gradual Rollout Slot 4
baseurl=https://els-rollout.tuxcare.com/slot-4/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-4-source]
name=CentOS 7 ELS - Gradual Rollout Slot 4 Source
baseurl=https://els-rollout.tuxcare.com/slot-4/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-4-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 4 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-4-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-5]
name=CentOS 7 ELS - Gradual Rollout Slot 5
baseurl=https://els-rollout.tuxcare.com/slot-5/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-5-source]
name=CentOS 7 ELS - Gradual Rollout Slot 5 Source
baseurl=https://els-rollout.tuxcare.com/slot-5/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-5-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 5 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-5-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-6]
name=CentOS 7 ELS - Gradual Rollout Slot 6
baseurl=https://els-rollout.tuxcare.com/slot-6/el/\$releasever/\$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-6-source]
name=CentOS 7 ELS - Gradual Rollout Slot 6 Source
baseurl=https://els-rollout.tuxcare.com/slot-6/el/\$releasever/src/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True

[centos7els-rollout-6-bypass]
name=CentOS 7 ELS - Gradual Rollout Slot 6 Bypass
baseurl=https://els-rollout.tuxcare.com/slot-6-bypass/el/\$releasever/\$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
skip_if_unavailable=True
EOT

yum clean all
yum install -y els-define --disablerepo=* --enablerepo=centos7-els

echo "CentOS 7 ELS installed successfully"
