#!/bin/bash
# Google periodicially, and for no apparent reason, returns "403 Forbidden"
# when attempting to download the published CSVs of the sheet. By adding some
# retry logic in here we can hopefully withstand this error.
#
# Usage: bin/wget-wrapper [arguments to wget]
set -euo pipefail

retries=3
while true; do
  wget -U OpenDisclosure "$@" && break

  exit_code=$?

  retries=$(($retries - 1))
  if [ $retries -lt 0 ]; then
    echo "Ran out of retries :("
    exit $exit_code
  fi

  echo "Error downloading. Waiting and retrying..." >&2

  # when retries = 2, sleep 1 second
  # when retries = 1, sleep 3 seconds
  # when retries = 0, sleep 5 seconds
  sleep $(($((2 - $retries)) * 2 + 1))
done
