#!/usr/bin/env bash

CLN_SERVER="https://cln.cloudlinux.com/cln/api/els/server/register"
LICENSE=""
HOSTNAME=`hostname`

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


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

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

if [[ ! -z $DELETE ]]; then
    rm -rf /etc/yum.repos.d/centos6-els.repo
    rm -rf /etc/yum.repos.d/centos6-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 release 6"* ]]; then
    echo "This server is not CentOS Linux 6"
    exit 1
fi

# check license key
if [[ ! "$LICENSE" == *"ELS"* ]]; then
    echo "Incorrect license key"
    exit 1
fi

# check if els is installed
if [[ -f /etc/yum.repos.d/centos6-els.repo ]]; then
    echo "This server has installed ELS repo and token"
    echo "For re-registration license run script with --force"
    exit 1
fi

# download and import GPG key
curl -sSL https://repo.cloudlinux.com/cloudlinux/security/RPM-GPG-KEY-CloudLinux -o /etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
[ $? -eq 0 ]  || exit 1

# get token
CLN_REGISTER=$(curl -i -X POST -H "Content-Type: application/json" -H "accept: */*" -d "{\"key\": \"$LICENSE\", \"host_name\": \"$HOSTNAME\"}" "$CLN_SERVER")

if [[ ! "$CLN_REGISTER" == *"200 OK"* ]]; then
    echo "Get incorrect status from CLN: $CLN_REGISTER"
    exit 1
fi

TOKEN=$(echo "$CLN_REGISTER" | grep -oP '"token":"\K[\w\d-]*')

if [[ -z $TOKEN ]]; then
    echo "Something went wrong. Token not defined"
    exit 1
fi

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

[centos6-els-src]
name = CentOS 6 Extended Lifecycle Support by CloudLinux SRPMS repo
baseurl = https://repo.cloudlinux.com/centos6-els/$TOKEN/updates/SRPMS/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
gpgcheck=1

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

cat <<EOT >> /etc/yum.repos.d/centos6-els-rollout.repo
[ELS6-rollout-1]
name=CentOS 6 ELS - Gradual Rollout Slot 1
baseurl=https://els-rollout.cloudlinux.com/slot-1/el/6/\$basearch/$TOKEN
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

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

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

[ELS6-rollout-2]
name=CentOS 6 ELS - Gradual Rollout Slot 2
baseurl=https://els-rollout.cloudlinux.com/slot-2/el/6/\$basearch/$TOKEN
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

[ELS6-rollout-2-source]
name=CentOS 6 ELS - Gradual Rollout Slot 2 Source
baseurl=https://els-rollout.cloudlinux.com/slot-2/el/6/src/$TOKEN
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

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

[ELS6-rollout-3]
name=CentOS 6 ELS - Gradual Rollout Slot 3
baseurl=https://els-rollout.cloudlinux.com/slot-3/el/6/\$basearch/$TOKEN
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

[ELS6-rollout-3-source]
name=CentOS 6 ELS - Gradual Rollout Slot 3 Source
baseurl=https://els-rollout.cloudlinux.com/slot-3/el/6/src/$TOKEN
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

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

[ELS6-rollout-4]
name=CentOS 6 ELS - Gradual Rollout Slot 4
baseurl=https://els-rollout.cloudlinux.com/slot-4/el/6/\$basearch/$TOKEN
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

[ELS6-rollout-4-source]
name=CentOS 6 ELS - Gradual Rollout Slot 4 Source
baseurl=https://els-rollout.cloudlinux.com/slot-4/el/6/src/$TOKEN
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

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

[ELS6-rollout-5]
name=CentOS 6 ELS - Gradual Rollout Slot 5
baseurl=https://els-rollout.cloudlinux.com/slot-5/el/6/\$basearch/$TOKEN
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

[ELS6-rollout-5-source]
name=CentOS 6 ELS - Gradual Rollout Slot 5 Source
baseurl=https://els-rollout.cloudlinux.com/slot-5/el/6/src/$TOKEN
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
skip_if_unavailable=True

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

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

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

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

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