#!/bin/bash

set -o nounset


prog=${0##*/}

fail () {
    echo "$prog:" "$@" >&2
    exit 1
}

usage () {
    echo "Usage: $prog <OSG VERSION> [<EXTRA REPOS...>]"
    exit 2
}

required_install () {
    yum install -y "$@" || fail "couldn't yum install $*"
}

OSG=${1?`usage`}
if [[ $OSG = 2* && ! $OSG = *-main ]]; then
    OSG=${OSG}-main
fi
shift
EXTRA_REPOS=($*)

command -v rpm &>/dev/null || fail "not an rpm-based distribution"

EL=$(rpm -E '%{?rhel}')
[[ -n $EL ]] || fail "not a rhel-compatible distribution"

if [[ $EL -lt 8 ]]; then
    required_install yum-plugin-priorities
fi
required_install yum-utils epel-release https://repo.osg-htc.org/osg/$OSG/osg-${OSG}-el${EL}-release-latest.rpm
crb enable

set +u  # apparently some versions of bash consider an empty array as unbound
for r in "${EXTRA_REPOS[@]}"; do
    yum-config-manager --enable "$r"
done


# vim:et:sw=4:sts=4:ts=8
