#!/bin/bash
#   Copyright (C) 2013  Red Hat, Inc.
#   Copyright (C) 2015  Till Maas
#   Copyright (C) 2016  Ben Rosser
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Abort on errors
set -e

baseurl=pkgs.rpmfusion.org
pkgname=$(basename $PWD)

# Determine if the package is in free or in nonfree.
# If we fail for whatever reason (.git/config not present, remote info not
# in that file), default to free namespace. XXX: is there a smarter way?
rooturl=$baseurl/free/$pkgname
if [[ -s .git/config ]]; then
    # Use grep and cut to read the right URL out of .git/config.
    giturl=`grep ${baseurl} .git/config`
    if [[ ${giturl} == *@* ]]; then
        giturl=https://`echo ${giturl} | cut -f2 -d"@"`
    else
        # Support anonymous (HTTP) checkouts properly.
        giturl=`echo ${giturl} | cut -f3 -d" "`
        giturl=`echo ${giturl} | sed "s,git/,,g"`
    fi

    # Then, insert the "repo/pkgs" string between pkgs.rpmfusion.org and the namespace
    rooturl=`echo ${giturl} | sed "s,${baseurl},${baseurl}/repo/pkgs,"`
fi

# Download sources.
if [[ -s sources ]]; then
    while read md5sum tarball; do

        # Pick the type of hash by looking at the length of the sum.
        hashtype=""
        sumlength=`echo -n $md5sum | wc -m`
        case $sumlength in
            32)  hashtype="md5" ;;
            40)  hashtype="sha1" ;;
            56)  hashtype="sha224" ;;
            64)  hashtype="sha256" ;;
            96)  hashtype="sha384" ;;
            128) hashtype="sha512" ;;
            *)   echo "Error: unrecognized hash function when downloading file ${tarball} with checksum ${md5sum}"
                 exit 1 ;;
            esac

        # Now, attempt download depending on the value of namespace.
        # We extracted the URL containing the namespace value from .git/config
        # automatically. If that failed, rfpkg-minimal will default to "free".
        curl -H Pragma: -o ./$tarball -R -S --fail ${rooturl}/$tarball/$hashtype/$md5sum/$tarball || true

        # Unfortunately we *also* need to try and download wihout $hashtype, if the hash is MD5
        # This is support for legacy entries in the lookaside cache
        # Only do this if we failed to download already.
        if [[ $hashtype == "md5" ]] && [ ! -f ./$tarball ]; then
            curl -H Pragma: -o ./$tarball -R -S --fail ${rooturl}/$tarball/$md5sum/$tarball || true
        fi

        # Complain if the download failed (i.e. file not present) and exit.
        if [ ! -f ./$tarball ]; then
            echo "Error: download of ${tarball} failed."
            exit 1
        fi

        # Check each line in the file individually in case files *don't* consistently
        # use the same set of checksums.
        # Since each checksum program consists of the name of the checksum followed by
        # the string 'sum', we can avoid reproducing the above table.
        echo "${md5sum}  ${tarball}" | ${hashtype}sum -c

    done < sources
fi
