#!/bin/bash

if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then
    set -x
fi
set -eu
set -o pipefail

if [[ (${DISTRO_NAME} =~ "almalinux" || ${DISTRO_NAME} =~ "centos" || ${DISTRO_NAME} =~ "rocky") ]]; then
    # Centos has "epel-release" in extras, which is default enabled.
    ${YUM} install -y epel-release
else
    echo "$DISTRO_NAME is not supported"
    # Not really a failure; we just don't do anything
    exit 0
fi

if [ ${DIB_EPEL_DISABLED:-0} -ne 0 ]; then
    if [[ ${YUM} == "dnf" ]]; then
        rpm  -q dnf-plugins-core || dnf install -y dnf-plugins-core
        dnf config-manager --set-disabled "epel*"
    else
        # Cannot rely on package-installs, it is executed later
        rpm  -q yum-utils || yum install -y yum-utils
        yum-config-manager --disable "epel*"
    fi
fi

DIB_EPEL_MIRROR=${DIB_EPEL_MIRROR:-}
[ -n "$DIB_EPEL_MIRROR" ] || exit 0

# Set the EPEL mirror to use
sed -e "s|^#baseurl=http[s]*://.*/pub/epel|baseurl=$DIB_EPEL_MIRROR|;/^mirrorlist=/d;/^metalink=/d" -i /etc/yum.repos.d/epel.repo

