#!/usr/bin/env bash

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

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



check_superuser_privileges() {
    echo "Checking for superuser privileges..."
    if [ "$(id -u)" -ne 0 ]; then
        echo "Error: This script must be run with superuser privileges"
        return 1
    fi
    echo "Superuser privileges confirmed"
    return 0
}

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 ! check_superuser_privileges; then
    exit 14
fi
if [[ ! -z $FORCE ]]; then
    rm -rf /etc/yum/vars/elstoken
    rm -rf /etc/yum.repos.d/oraclelinux7-els.repo
fi

if [[ ! -z $DELETE ]]; then
    rm -rf /etc/yum/vars/elstoken
    rm -rf /etc/yum.repos.d/oraclelinux7-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 7"* ]]; then
    echo "This server is not Oracle Linux Server release 7"
    exit 1
fi

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

# check if els is installed
if [[ -f /etc/yum/vars/elstoken || -f /etc/yum.repos.d/oraclelinux7-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.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

# 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/oraclelinux7-els.repo
[oraclelinux7-els]
name = OracleLinux 7 Extended Lifecycle Support by TuxCare Updates
baseurl = https://repo.tuxcare.com/oraclelinux7-els/\$elstoken/updates/\$basearch/
enabled = 1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
gpgcheck=0

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

[oraclelinux7-els-src]
name = OracleLinux 7 Extended Lifecycle Support by TuxCare SRPMS
baseurl = https://repo.tuxcare.com/oraclelinux7-els/\$elstoken/updates/Sources/
enabled = 0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-TuxCare
gpgcheck=0
EOT

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

echo "OracleLinux ELS installed successfully"
