Brian Stinson 396276
#!/bin/bash
Brian Stinson 396276
set -x -e
Brian Stinson 396276
Brian Stinson 396276
# Took this from rhpkg-simple internally owned by lsedlar@redhat.com
Brian Stinson 396276
pkgname=$(basename "$PWD")
Brian Stinson 396276
# If running in a git checkout, there will be a config file with url to the
Brian Stinson 396276
# remote. We can extract package namespace and name from it. If the file is
Brian Stinson 396276
# missing (which it should not really be), let's default to rpms/ namespace.
Brian Stinson 396276
ns_pkgname="rpms/$pkgname"
Brian Stinson 396276
if [ -r .git/config ]; then
Brian Stinson 396276
    url=$(grep " *url *= *" .git/config | cut -d= -f2- | tr -d ' ')
Brian Stinson 396276
    namespace=$(basename "$(dirname "$url")")
Brian Stinson 396276
    ns_pkgname="$namespace/$pkgname"
Brian Stinson 396276
fi
Brian Stinson 396276
Brian Stinson 396276
if [ -d SPECS ]; then
Brian Stinson 396276
    # We are in the SIG/Exploded SRPM layout so call get_sources.sh
Brian Stinson 396276
    exec /usr/bin/get_sources.sh
Brian Stinson 396276
fi
Brian Stinson 396276
Brian Stinson 396276
Brian Stinson 396276
## This part is also from rhpkg-simple
Brian Stinson 396276
Brian Stinson 396276
if [ -s sources ]; then
Brian Stinson 396276
    baseurl=https://sources.stream.rdu2.redhat.com/sources
Brian Stinson 396276
Brian Stinson 396276
    # curl arguments:
Brian Stinson 396276
    #   -L          follow redirects
Brian Stinson 396276
    #   -H Pragma:  disable caching
Brian Stinson 396276
    #   -o          filename where to save results
Brian Stinson 396276
    #   -R          use timestamp from remote server
Brian Stinson 396276
    #   -S          display error if there is a problem
Brian Stinson 396276
    #   --fail      do now write HTML page when there is error
Brian Stinson 396276
    #   --retry     up to 5 retries for transient errors
Brian Stinson 396276
    #   --max-time  wait for up to 15 seconds on network error
Brian Stinson 396276
Brian Stinson 396276
    # Read first word of first line. For old MD5 format it's the 32 character
Brian Stinson 396276
    # hash. Otherwise let's assume the sources have the BSD format where lines
Brian Stinson 396276
    # start with hash type.
Brian Stinson 396276
    hashtype="$(head -n1 sources | cut -d' ' -f1 | tr '[:upper:]' '[:lower:]')"
Brian Stinson 396276
    if [ "${#hashtype}" -ne 32 ]; then
Brian Stinson 396276
        # The format is
Brian Stinson 396276
        #   SHA512 (filename) = ABCDEF
Brian Stinson 396276
        # We don't care about the equals sign. We also assume that all hashes
Brian Stinson 396276
        # are of the same type, so we don't have to read it again for each
Brian Stinson 396276
        # line.
Brian Stinson 396276
        while read -r _ filename _ hash || [[ -n "$filename" && -n "$hash" ]]; do
Brian Stinson 396276
            if [ -z "$filename" ] || [ -z "$hash" ]; then
Brian Stinson 396276
                continue
Brian Stinson 396276
            fi
Brian Stinson 396276
            # Remove parenthesis around tarball name
Brian Stinson 396276
            filename=${filename#(}
Brian Stinson 396276
            tarball=${filename%)}
Brian Stinson 396276
            curl -L -H Pragma: -o "./$tarball" -R -S --fail --retry 5 "$baseurl/$ns_pkgname/$tarball/$hashtype/$hash/$tarball"
Brian Stinson 396276
        done < sources
Brian Stinson 396276
        "${hashtype}sum" -c sources
Brian Stinson 396276
    else
Brian Stinson 396276
        # Ok, we're working with MD5.
Brian Stinson 396276
        while read -r md5sum tarball || [[ -n "$md5sum" && -n "$tarball" ]]; do
Brian Stinson 396276
            if [ -z "$md5sum" ] || [ -z "$tarball" ]; then
Brian Stinson 396276
                continue
Brian Stinson 396276
            fi
Brian Stinson 396276
            curl -L -H Pragma: -o "./$tarball" -R -S --fail --retry 5 "$baseurl/$ns_pkgname/$tarball/$md5sum/$tarball"
Brian Stinson 396276
        done < sources
Brian Stinson 396276
        md5sum -c sources
Brian Stinson 396276
    fi
Brian Stinson 396276
fi