#!/bin/sh

set -e


tmp_file=$(mktemp --suffix=openlitespeed)
trap 'rm -f "${tmp_file}"' EXIT

for v in $(curl -s -f https://api.github.com/repos/litespeedtech/openlitespeed/releases | jq -r 'map(.tag_name) | .[]' | grep '^v1.[0-9.]\+$' | tr -d v | sort -V | tail -n 2); do
	short_v=$(echo "${v}" | grep -m 1 -o '^[0-9]\+\(\.[0-9]\+\(\.[0-9]\+\)\?\)\?')
	for arch in x86_64 aarch64; do
		echo "Downloading ${v} ($arch)"
		curl --silent --location --fail -o "${tmp_file}" "https://github.com/litespeedtech/openlitespeed/releases/download/v${v}/openlitespeed-${short_v}-${arch}-linux.tgz"
		chmod 0644 "${tmp_file}"
		mv "${tmp_file}" "openlitespeed-${v}-${arch}-linux.tgz"
	done
done
