#!/usr/bin/env bash
cd "$(dirname "$0")"
API_URL="https://api.github.com/repos/icykoneko/ff14-fish-tracker-app/actions/artifacts?per_page=2"

[ -z "$1" ] && echo missing token && exit 1
json=$(curl -fsSL "$API_URL")
id=$(jq -r '.artifacts[0].id' <<<"$json")
[ -f "${id}.zip" ] && echo exiting && exit 1

echo "Downloading build $id"
curl -L $(jq -r '.artifacts[0].archive_download_url' <<<"$json")  --header "Authorization: Bearer $1" -o "${id}.zip"

unzip "${id}.zip"
tar -xf artifact.tar -C ff14-fish-tracker-app/
rm artifact.tar

