#!/usr/bin/env bash
# Uploads the Top Shelf hero video + poster to the jkaindl.de Hetzner webspace over SFTP,
# using the deploy SSH key (~/.ssh/jkaindl_de_deploy — no password needed at runtime), then
# verifies the public URLs (HTTP, content-type, byte-range support that tvOS streaming needs).
#
# Usage: scripts/deploy-topshelf-to-hetzner.sh <sftp-host> <sftp-user> [remote-webroot]
# <sftp-host> e.g. wXXXXXXXX.kasserver.com (from konsoleH FTP/SSH access)
# <sftp-user> the SFTP/FTP username (from konsoleH)
# [remote-webroot] OPTIONAL docroot, e.g. /htdocs — leave empty if the SFTP login already
# lands in the web root (then we upload relative to the home). If the
# verify step 404s, re-run with the right webroot.
set -euo pipefail
cd "$(dirname "$0")/.."
HOST="${1:?usage: $0 <sftp-host> <sftp-user> [remote-webroot]}"
USER="${2:?need the SFTP username (arg 2)}"
WEBROOT="${3:-}"
KEY="$HOME/.ssh/jkaindl_de_deploy"
HERO="public/topshelf/hero.mp4"
POSTER="public/topshelf/poster.png"
[ -f "$KEY" ] || { echo "error: deploy key $KEY missing (run the keygen step first)" >&2; exit 1; }
[ -f "$HERO" ] && [ -f "$POSTER" ] || { echo "error: local assets missing — run scripts/shoot-topshelf-hero.sh first" >&2; exit 1; }
if [ -n "$WEBROOT" ]; then PAR="${WEBROOT%/}/physarum"; DIR="${WEBROOT%/}/physarum/topshelf"; else PAR="physarum"; DIR="physarum/topshelf"; fi
echo "==> SFTP ${USER}@${HOST} -> ${DIR}/ (hero.mp4 + poster.png)"
sftp -o StrictHostKeyChecking=accept-new -o IdentitiesOnly=yes -i "$KEY" "${USER}@${HOST}" <<SFTP
-mkdir ${PAR}
-mkdir ${DIR}
put ${HERO} ${DIR}/hero.mp4
put ${POSTER} ${DIR}/poster.png
chmod 644 ${DIR}/hero.mp4
chmod 644 ${DIR}/poster.png
ls -l ${DIR}
bye
SFTP
echo "==> verify public URLs (HTTP 200 + correct type expected)"
ok=1
for f in hero.mp4 poster.png; do
out=$(curl -sS -o /dev/null -w "%{http_code} %{content_type}" -I "https://jkaindl.de/physarum/topshelf/$f" 2>/dev/null || echo "000 -")
echo " $f -> HTTP $out"
case "$out" in 200*) ;; *) ok=0 ;; esac
done
echo "==> byte-range support (HTTP 206 = tvOS can stream it)"
rcode=$(curl -sS -o /dev/null -w "%{http_code}" -r 0-1023 "https://jkaindl.de/physarum/topshelf/hero.mp4" || echo 000)
echo " hero.mp4 range -> HTTP $rcode"
[ "$rcode" = "206" ] || echo " (note: not 206 — range may be unsupported; usually still fine for short clips)"
if [ "$ok" = "1" ]; then
echo "DONE — assets live at https://jkaindl.de/physarum/topshelf/"
else
echo "Uploaded, but the public URL 404s — the webroot is probably different. Re-run with the docroot, e.g.: $0 $HOST $USER /htdocs" >&2
exit 2
fi