#!/usr/bin/env bash
set -xeuo pipefail
#MISE depends=["docs:build"]
#MISE dir="docs"
#MISE description="Release documentation site to production or remote"

aws --version
export AWS_REGION=auto
export AWS_ENDPOINT_URL=https://6e243906ff257b965bcae8025c2fc344.r2.cloudflarestorage.com
export AWS_RETRY_MODE=standard
export AWS_MAX_ATTEMPTS=10

if [ "${DRY_RUN:-true}" = "true" ]; then
	aws() {
		echo "DRY RUN: aws $*"
	}
fi

# The theme song videos are too large for the repo and are uploaded to the
# bucket by hand (docs/mise-en-place.md plays them from mise.jdx.dev). Refuse to
# publish a page whose player and downloads would 404.
for key in mise-run.mp4 mise-run-4k.mp4; do
	aws s3api head-object --bucket mise --key "$key" >/dev/null
done

if [ $((RANDOM % 30)) -eq 0 ]; then
	# delete old assets only roughly 1/30 times
	# deleting old assets can break the site for people currently on it
	# but it's also good to keep things tidy
	aws s3 rm --recursive s3://mise/assets/
	aws s3 rm --recursive --exclude "*" --include "*.html" s3://mise/
fi

aws s3 cp --recursive --checksum-algorithm CRC32 .vitepress/dist s3://mise/
