#!/bin/sh
set -e
url="$1"
filepath="$(echo "$url" | cut -d'=' -f2)"

if [ -f "$filepath" ]; then
    echo already exists: $filepath
    exit 0
fi

echo downloading: $filepath

s3_url="$(curl "$url" | jq to_entries | jq -r .[].value)"
if [ -z "$s3_url" ]; then
    echo "$filepath does not exist"
    exit 0
fi

mkdir -p "$(dirname "$filepath")"
wget "$s3_url" -nc -O "$filepath"
