# -*- mode: shell-script -*-
# Shell functions for downloading and extracting a RHEL or RHEL clone RPM

# Arguments:
# $1: Linux distribution name
# $2: Linux distribution version (major.minor)
# $3: architecture
function get_srpm_urls() {
    local arch distro releasever releasevermajor releaseverminor

    distro="$1"
    releasever="$2"
    arch="$3"
    set -- ${2//./ }
    releasevermajor="$1"
    releaseverminor="$2"

    case "$distro" in
	"CentOS"*)
	    case $releasever in
		5.*|6.[01])
		    echo "http://vault.centos.org/${releasever}/os/SRPMS"
		    echo "http://vault.centos.org/${releasever}/updates/SRPMS";;
		[67]*)
		    echo "http://vault.centos.org/${releasever}/os/Source/SPackages"
		    echo "http://vault.centos.org/${releasever}/updates/Source/SPackages";;
		8*)
		    echo "http://vault.centos.org/${releasever}/BaseOS/Source/SPackages/";;
	    esac
	    ;;
	"Red Hat Enterprise Linux"*)
	    case $releasever in
		[56].*)
		    echo "http://ftp.redhat.com/pub/redhat/linux/enterprise/${releasevermajor}Server/en/os/SRPMS"
		    echo "http://ftp.redhat.com/redhat/rhel/rc/7/Server/source/tree/Packages";;
	    esac
	    ;;
	"AlmaLinux")
	    case $releasever in
		[89]*)
		    echo "https://repo.almalinux.org/vault/${releasever}/BaseOS/Source/Packages/";;
	    esac
	    ;;
	"Oracle Linux Server"|"UEK")
	    echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/${releaseverminor}/base/${arch}/getPackageSource"
	    echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/latest/${arch}/getPackageSource"
	    for ((i=4;i<=7;i++)); do
		echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/UEKR$i/${arch}/getPackageSource"
	    done
	    ;;
	"Scientific Linux")
	    case $releasever in
		5.*)
		    echo "http://ftp.scientificlinux.org/linux/scientific/$releasevermajor$releaseverminor/SRPMS/vendor";;
		6.*)
		    echo "http://ftp.scientificlinux.org/linux/scientific/$releasever/SRPMS/vendor";;
	    esac
	    ;;
	*)
	    echo "Unknown type of distribution: $distro" >&2
	    return 1
	    ;;
    esac
}

# Arguments:
# $1: Linux distribution name
# $2: Linux distribution version (major.minor)
# $3: architecture
function get_rpm_urls() {
    local arch distro releasever releasevermajor releaseverminor

    distro="$1"
    releasever="$2"
    arch="$3"
    set -- ${2//./ }
    releasevermajor="$1"
    releaseverminor="$2"

    case "$distro" in
	"CentOS"*)
	    case $releasever in
		5.*)
		    echo "http://vault.centos.org/${releasever}/os/${arch}/CentOS http://vault.centos.org/${releasever}/updates/${arch}/RPMS";;
		6.[01])
		    echo "http://vault.centos.org/${releasever}/os/${arch}/Packages"
		    echo "http://vault.centos.org/${releasever}/updates/${arch}/Packages";;
		[67]*)
		    echo "https://mirrors.edge.kernel.org/centos/${releasever}/updates/${arch}/Packages"
		    echo "http://vault.centos.org/${releasever}/updates/${arch}/Packages"
		    echo "http://vault.centos.org/${releasever}/os/${arch}/Packages http://vault.centos.org/${releasever}/updates/${arch}/Packages";;
		8*)
		    echo "https://mirrors.edge.kernel.org/centos/8-stream/BaseOS/${arch}/os/Packages";;
	    esac
	    ;;
	"Red Hat Enterprise Linux"*)
	    echo "";;
	"AlmaLinux")
	    case $releasever in
		[89]*)
		    echo "https://mirror.yandex.ru/almalinux/${releasever}/BaseOS/${arch}/os/Packages/";;
	    esac
	    ;;
	"Oracle Linux Server"|"UEK")
	    echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/${releaseverminor}/base/${arch}/getPackageSource"
	    echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/latest/${arch}/getPackageSource"
	    for ((i=4;i<=7;i++)); do
		echo "http://public-yum.oracle.com/repo/OracleLinux/OL${releasevermajor}/UEKR$i/${arch}/getPackageSource"
	    done
	    ;;
	"Scientific Linux")
	    case $releasever in
		5.*)
		    echo "http://ftp.scientificlinux.org/linux/scientific/$releasevermajor$releaseverminor/SRPMS/vendor";;
		6.*)
		    echo "http://ftp.scientificlinux.org/linux/scientific/$releasever/SRPMS/vendor";;
	    esac
	    ;;
	*)
	    echo "Unknown distribution type: $distro" >&2
	    return 1
	    ;;
    esac
}
