#!/bin/bash -e

. ./xi-sys.cfg
. ./functions.sh

# Check if user already configured repos
if [ -f installed.repos ]; then
    echo "Repos already configured - exiting."
    exit 0
fi
echo "Configuring Repos..."

if [ "$distro" == "Ubuntu" ]; then 
    echo "Repos configured OK"
    touch installed.repos
    exit 0
fi

if [ "$distro" == "Debian" ]; then
    ./debianmods
    echo "Repos configured OK"
    touch installed.repos
    exit 0
fi

# Detect RHUI (Red Hat Update Infrastructure) - cloud instances
RHUI_DETECTED=0
if rpm -qa | grep -qE '(rh-amazon-rhui-client|rhui-azure-rhel|rhui-google-rhel)'; then
    RHUI_DETECTED=1
    echo "RHUI detected via package check"
fi

# Check that the Red Hat installation is valid (skip for RHUI systems)
if [ "$RHUI_DETECTED" -eq 0 ] && [ "$distro" == "RedHatEnterpriseServer" ] && [ -x /usr/sbin/rhn_check ] && ! /usr/sbin/rhn_check >/dev/null 2>&1; then
    ## Fails initial check with newer subscription method, nested if to check for subscription and proceed correctly if found -SR
    if [ -x /usr/bin/subscription-manager ] && subscription-manager list | grep Status: | grep -qF Subscribed; then
        echo "Passed New Subscription Check"
    else
        echo "Your Red Hat Installation is not registered or does not have proper entitlements." 
        echo "Please register or enable entitlements at rhn.redhat.com." 
        exit 1;
    fi
fi

if [ ! -f $INSTALL_PATH/offline ]; then
    # Enable codeready builder repo
    if [ "$RHUI_DETECTED" -eq 1 ] && [ "$distro" == "RedHatEnterpriseServer" ]; then
        # RHUI systems - try both naming conventions (reason being I just don't which one would work)
        if [ "$dist" == "el9" ]; then
            yum-config-manager --enable rhui-codeready-builder-for-rhel-9-rhui-rpms 2>/dev/null || \
            yum-config-manager --enable codeready-builder-for-rhel-9-rhui-rpms 2>/dev/null || \
            echo "Note: Could not enable CodeReady Builder on RHUI - it may already be enabled or use a different name"
        elif [ "$dist" == "el8" ]; then
            yum-config-manager --enable rhui-codeready-builder-for-rhel-8-rhui-rpms 2>/dev/null || \
            yum-config-manager --enable codeready-builder-for-rhel-8-rhui-rpms 2>/dev/null || \
            echo "Note: Could not enable CodeReady Builder on RHUI - it may already be enabled or use a different name"
        fi
    elif [ "$distro" == "RedHatEnterpriseServer" ] && [ "$dist" == "el9" ]; then
        subscription-manager repos --enable codeready-builder-for-rhel-9-x86_64-rpms
    elif [ "$distro" == "RedHatEnterpriseServer" ] && [ "$dist" == "el8" ]; then
        subscription-manager repos --enable codeready-builder-for-rhel-8-x86_64-rpms
    elif [ "$distro" == "OracleServer" ] && [ "$dist" == "el8" ]; then
        yum config-manager --set-enabled ol8_codeready_builder
    elif [ "$distro" == "OracleServer" ] && [ "$dist" == "el9" ]; then
        yum config-manager --set-enabled ol9_codeready_builder
    fi

    # Fail if codeready builder is not enabled (skip check for RHUI systems where repo names vary)
    if [ "$RHUI_DETECTED" -eq 0 ] && [ "$distro" == "RedHatEnterpriseServer" ]; then
        if subscription-manager repos --list-enabled | grep -q 'codeready' ; then
            echo "Passed Code Ready Repo Check"
        else
            echo "ERROR: Please add the 'codeready-builder' repo to your Red Hat systems subscriptions." 
            echo "       You can do so with the following command:" 
            echo "       subscription-manager repos --enable <repo name>" 
            echo "" 
            echo "       You can list all repos available to you to find the codeready-builder by running:"
            echo "       subscription-manager repos"
            exit 1
        fi
    elif [ "$RHUI_DETECTED" -eq 1 ]; then
        echo "RHUI system detected - skipping subscription-manager checks"
        # Verify we have at least some repos available
        if ! yum repolist 2>/dev/null | grep -qE '(rhui|codeready|rhel)'; then
            echo "WARNING: No RHUI repositories detected. Installation may fail."
        else
            echo "RHUI repositories available"
        fi
    fi
fi
    
# Make sure everything needed is still present, and we should be working 
# with a current system anyway
if [ "$distro" == "CentOS" ] || [ "$distro" == "RedHatEnterpriseServer" ] || [ "$distro" == "OracleServer" ]; then
    if [ "$1" != noupdate ]; then
        echo "Updating system..."
        yum -y update
    fi
fi

##########################################
# INSTALL DEPENDANT REPOS
##########################################

# See if we need to install RPMForge...
if [ "$distro" == "CentOS" ] || [ "$distro" == "RedHatEnterpriseServer" ] || [ "$distro" == "OracleServer" ]; then

    # Install centos stream codeready builder
    if [ "$dist" == "el9" ] && rpm -q centos-stream-release ; then
        dnf config-manager --set-enabled crb
    fi

    if ! rpm -q nagios-repo &>/dev/null; then

        echo "Enabling Nagios repo..."
        PKG="packages/nagios-repo-$ver-latest.el$ver.noarch.rpm"
        echo "Installing Nagios Repo PKG: $PKG"
        rpm -Uvh "$PKG"
        rm "$PKG"
        unset PKG
        
        if [ ! -f $INSTALL_PATH/offline ]; then
            yum check-update || true
        fi
    fi

    # Try to install epel release package from the repo
    set +e
    yum install epel-release -y
    set -e

    # See if we need to install EPEL...
    if ! rpm -q epel-release &>/dev/null; then

        echo "Enabling EPEL repo..."
        PKG="./packages/epel-release-latest-$ver.noarch.rpm"

        echo "Installing EPEL PKG: $PKG"
        rpm -Uvh "$PKG"

        rm "$PKG"
        unset PKG
        
        if [ ! -f $INSTALL_PATH/offline ]; then
            yum check-update || true
        fi
        
    fi

    # Check to make sure RPM was installed
    if rpm -q epel-release &>/dev/null; then
        echo "epel-release RPM installed OK"
    else
        echo "ERROR: epel-release RPM was not installed - exiting." 
        exit 1
    fi

fi

# Checks if offline, if so, installs all needed packages now to avoid issues
if [ -f $INSTALL_PATH/offline ]; then
    echo "Updating RPMS, this may take a moment." 
    rpm -Uvh $INSTALL_PATH/packages/offlineinstall/rpms/upgrade/*.rpm
    echo "Installing RPMS, this may take a moment." 
    rpm -ivh $INSTALL_PATH/packages/offlineinstall/rpms/*.rpm
fi

echo "Repo configured OK"
touch installed.repos
