#!/bin/bash
set -e
script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source "$script_dir/functions"

for builder in $tb_builders jb
do
  for file in sha256sums-unsigned-build.txt sha256sums-unsigned-build.incrementals.txt
  do
    tmpfile=$(mktemp)
    chmod 644 "$tmpfile"
    if test "$builder" = 'jb'; then
      file="$file.gpg"
      urls=("https://cdn.stagemole.eu/hashes/$SIGNING_PROJECTNAME/$tbb_version-$tbb_version_build/$file")
    else
      file="$file.asc"
      urls=( \
        "https://people.torproject.org/~$builder/builds/$SIGNING_PROJECTNAME/$tbb_version-$tbb_version_build/$file" \
        "https://people.torproject.org/~$builder/builds/$SIGNING_PROJECTNAME/$tbb_version_type/$tbb_version-$tbb_version_build/$file" \
        "https://tb-build-02.torproject.org/~$builder/builds/$SIGNING_PROJECTNAME/$tbb_version_type/unsigned/$tbb_version-$tbb_version_build/$file" \
        "https://tb-build-03.torproject.org/~$builder/builds/$SIGNING_PROJECTNAME/$tbb_version_type/unsigned/$tbb_version-$tbb_version_build/$file" \
      )
    fi
    for url in "${urls[@]}"
    do
      if wget -q -O "$tmpfile" "$url"; then
        mv "$tmpfile" "$signed_version_dir/$file-$builder"
        echo "Added $file-$builder"
        break
      fi
    done
  done
done
exit 0
