#!/usr/bin/env bash

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

show_usage() {
    echo 'Usage: install-centos8.5-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. Please use TuxCare portal or contact sales@tuxcare.com to cancel a license'
    echo '  -h, --help          Show this message and exit'
}

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

if [[ $# -eq 0 ]]; then
    echo "An option should be specified. See usage -h, --help."
    exit 1
fi

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/vars/elstoken
    rm -rf /etc/yum.repos.d/centos8.5-els.repo
fi

if [[ ! -z $DELETE ]]; then
    CLN_UNREGISTER=""
    if els_installed; then
        CLN_UNREGISTER=$(curl -i -s -X POST "$CLN_UNREGISTER_SERVER?token=$(cat /etc/yum/vars/elstoken)")
    fi
    if [[ ! "$CLN_UNREGISTER" == *"200 OK"* ]]; then
        if [[ -z $CLN_UNREGISTER ]]; then
            echo "DELETE: elstoken wasn't found. Server is not registered"
        else
            echo "Get incorrect status from CLN: $CLN_UNREGISTER"
        fi
        exit 1
    else
        echo "Unregister successfully"
    fi

    rm -rf /etc/yum/vars/elstoken
    rm -rf /etc/yum.repos.d/centos8.5-els.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 8.5"* ]]; then
    echo "This server is not CentOS Linux release 8.5"
    exit 1
fi

# check license key
if [[ -z $LICENSE ]]; then
    echo "License key should be specified with -l, --license-key."
    echo "See -h, --help."
    exit 1
elif [[ ! "$LICENSE" == *"ELS"* ]]; then
    echo "Incorrect license key"
    exit 1
fi

# check if els is installed
if els_installed; 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_REGISTER_SERVER")
CLN_CODE=$(head -n1 <<< "$CLN_REGISTER")

if [[ ! "$CLN_CODE" == *"200"* ]]; 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

echo "${TOKEN}" > /etc/yum/vars/elstoken

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

[centos8.5-els-testing]
name = CentOS 8.5 Extended Lifecycle Support by CloudLinux
baseurl = https://repo.cloudlinux.com/centos8.5-els/\$elstoken/updates-testing/\$basearch/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
gpgcheck=0

[centos8.5-els-src]
name = CentOS 8.5 Extended Lifecycle Support by CloudLinux SRPMS repo
baseurl = https://repo.cloudlinux.com/centos8.5-els/\$elstoken/updates/Sources/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
gpgcheck=0
EOT

yum clean all
yum install -y els-define --disablerepo=* --enablerepo=centos8.5-els

echo "CentOS ELS installed successfully"
