#!/usr/bin/env bash
# booth — post media to The Booth (dead simple). A booth is just a folder under
# $BOOTH_DATA_DIR; this is sugar over mkdir/cp so you get the URL back.
#
#   booth new  <name>                 make an empty booth, print its URL
#   booth add  <name> <file>...       copy files into a booth (creates it), print URL
#   booth url  <name>                 print a booth's URL
#   booth ls                          list booths
#   booth rm   <name>                 wipe a booth now (TTL would eventually anyway)
#
# On a host that is NOT nh3-dev, rsync into the data dir instead, e.g.:
#   rsync -a ./out/  nh3-dev:booth-data/my-run/
set -euo pipefail

DATA="${BOOTH_DATA_DIR:-$HOME/booth-data}"
URL="${BOOTH_URL:-http://10.100.10.50:8090}"

usage() { echo "usage: booth {new <name>|add <name> <file>...|url <name>|ls|rm <name>}" >&2; exit 2; }

cmd="${1:-}"; shift || true
case "$cmd" in
  new)
    [ $# -ge 1 ] || usage
    mkdir -p -- "$DATA/$1"
    echo "$URL/b/$1/"
    ;;
  add)
    [ $# -ge 2 ] || usage
    name="$1"; shift
    mkdir -p -- "$DATA/$name"
    cp -- "$@" "$DATA/$name/"
    echo "$URL/b/$name/"
    ;;
  url)
    [ $# -ge 1 ] || usage
    echo "$URL/b/$1/"
    ;;
  ls)
    ls -1 -- "$DATA" 2>/dev/null || true
    ;;
  rm)
    [ $# -ge 1 ] || usage
    rm -rf -- "${DATA:?}/$1"
    echo "wiped $1"
    ;;
  *) usage ;;
esac
