#!/bin/sh

UPSTREAM=https://packages.typst.org/preview/index.json
UPSTREAM_PREFIX=$(dirname $UPSTREAM)
XDIR=$(cd $(dirname $0) && pwd)

curl -o $XDIR/index.json $UPSTREAM

cat $XDIR/index.json |jq -c -r '.[] | .name+":"+.version+":"+((.categories|tostring) // "")' | \
while IFS=: read -r _NAME _VER _rest; do
	if [ ! -z "$_NAME" ]; then
		_FILE="$_NAME-$_VER.tar.gz"
		echo $_FILE
		curl -L -o $XDIR/$_FILE $UPSTREAM_PREFIX/$_FILE
	fi
done
