#!/bin/sh
# srht-deploy -- deploy a directory to sr.ht pages
set -eu

if [ -z "${1:-}" ] || [ -z "${2:-}" ]; then
    echo "Usage: srht-deploy <domain> <directory>" >&2
    exit 1
fi

DOMAIN="$1"
DIR="$2"

if [ ! -d "$DIR" ]; then
    echo "Not a directory: $DIR" >&2
    exit 1
fi

TMPFILE=$(mktemp /tmp/srht-deploy-XXXXXX.tar.gz)
trap 'rm -f "$TMPFILE"' EXIT

tar -C "$DIR" -czf "$TMPFILE" .

if command -v hut >/dev/null 2>&1; then
    hut pages publish -d "$DOMAIN" "$TMPFILE"
else
    TOKEN=$(cat ~/.config/srht/token)
    curl -s --oauth2-bearer "$TOKEN" \
      -F "content=@$TMPFILE" \
      "https://pages.sr.ht/publish/$DOMAIN"
fi

echo "[+] Published to $DOMAIN"
