#!/usr/bin/env bash

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

show_usage() {
    echo 'Usage: install-oraclelinux6-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'
}


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/oraclelinux6-els.repo
fi

if [[ ! -z $DELETE ]]; then
    rm -rf /etc/yum/vars/elstoken
    rm -rf /etc/yum.repos.d/oraclelinux6-els.repo
    yum remove els-define -y
    echo "OracleLinux ELS deleted successfully"
    exit
fi

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

# check OS version
oracle_release="$(cat /etc/oracle-release)"
if [[ ! "${oracle_release}" == *"Oracle Linux Server release 6"* ]]; then
    echo "This server is not Oracle Linux Server release 6"
    exit 1
fi

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

# check if els is installed
if [[ -f /etc/yum/vars/elstoken || -f /etc/yum.repos.d/oraclelinux6-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
wget -nv 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")
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/oraclelinux6-els.repo
[oraclelinux6-els]
name = OracleLinux 6 Extended Lifecycle Support by TuxCare Updates
baseurl = https://repo.cloudlinux.com/oraclelinux6-els/\$elstoken/updates/\$basearch/
enabled = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
gpgcheck=0

[oraclelinux6-els-testing]
name = OracleLinux 6 Extended Lifecycle Support by TuxCare Updates-Testing
baseurl = https://repo.cloudlinux.com/oraclelinux6-els/\$elstoken/updates-testing/\$basearch/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
gpgcheck=0

[oraclelinux6-els-src]
name = OracleLinux 6 Extended Lifecycle Support by TuxCare SRPMS
baseurl = https://repo.cloudlinux.com/oraclelinux6-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

echo "OracleLinux ELS installed successfully"
