#!/bin/bash
# Copyright (c) Contributors to the Apptainer project, established as
#   Apptainer a Series of LF Projects LLC.
#   For website terms of use, trademark policy, privacy policy and other
#   project policies see https://lfprojects.org/policies
#
# Download additional source urls listed in rpm into directory in $1.
# Assumes being run from the top of the apptainer source directory.

if [ -n "$2" ] || [[ "$1" = -* ]]; then
    echo "Usage: $0 [downloaddir]" >&2
    echo "The default downloaddir is '.'" >&2
    exit 2
fi
DIR=.
if [ -n "$1" ]; then
    DIR="$1"
fi

set -ex
${0%/*}/clean-dependencies $1

SPEC=dist/rpm/apptainer.spec.in
SUBS="$(sed -n "s/.*%global //p" $SPEC)"
# Expected format for sources that have a different base path than the URL
# is a line beginning "# URL:" followed by the URL, then the source with just
# the base path.
sed -n -e '/^# URL:/{s/^# //;s/%%/%/g;p}' -e 's/^Source[1-9][0-9]*: //p' -e 's/^Patch[1-9][0-9]*: //p' $SPEC | while read -r LINE; do
    # first apply substitutions
    LINE=$(echo "$SUBS" | (while read -r FROM TO; do
            LINE="${LINE//%\{$FROM\}/$TO}"
          done
          echo "$LINE"))
    if [[ "$LINE" = *http* ]]; then
        URL="${LINE/*http/http}"
        if [[ "$LINE" = "URL:"*http* ]]; then
            # the file name is on the next line
            continue
        fi
        # take the file name from the base of the URL
        LINE="${LINE/*\//}"
    fi
    if [[ "$LINE" = *PRoot* ]]; then
        # skip architectures known to not be supported by PRoot
        case $(arch) in
            ppc*|s390*|riscv*) continue;;
            x86*) if [ -f /etc/os-release ]; then
                    . /etc/os-release
                    if [ "$ID" = fedora ] && [ "$VERSION_ID" -ge 45 ]; then
                        # On fedora45 x86_64 building PRoot dies with
                        # "relocation truncated to fit: R_X86_64_PC32 against `.rodata'"
                        # See https://github.com/proot-me/proot/issues/414
                        continue
                    fi
                fi
                ;;
        esac
    fi
    if [ -n "$GITHUB_TOKEN" ] && [[ "$URL" = *github.com/*.patch ]]; then
        # use github API instead because the public URL downloads
        # sometimes hit server busy (429) when run in github actions
        URL="$(echo "$URL"|sed -e 's,github.com/,api.github.com/repos/,' \
                               -e 's,/pull/,/pulls/,' -e 's,\.patch,,')"
        curl -f -L -sS -o $DIR/$LINE \
            -H "Accept: application/vnd.github.diff" \
            -H "Authorization: Bearer $GITHUB_TOKEN" $URL
    else
        curl -f -L -sS -o $DIR/$LINE $URL
    fi
done

