#!/usr/bin/env bash
# Upload Projects/x → public share · HTML5 watch polls JSON · NO python · NO xdg-open
#
#   bash Build/bgrtx-share-upload-x.sh
#
# Progress JSON: /var/www/bgrtx-share/upload-progress.json
# Watch:         https://share.biggrinrtx.com/upload-watch.html
# Tree:          https://share.biggrinrtx.com/x/
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SRC="$ROOT"
DEST="/var/www/bgrtx-share/x"
PROG="/var/www/bgrtx-share/upload-progress.json"
LOG="$ROOT/out/share-upload-x.log"
mkdir -p "$ROOT/out" "$DEST" 2>/dev/null || true

# exclude thrash/secrets · still full source desk
EXCLUDES=(
  --exclude='.git/'
  --exclude='__pycache__/'
  --exclude='node_modules/'
  --exclude='.venv/'
  --exclude='venv/'
  --exclude='OpenPaperWork/'
  --exclude='*.pyc'
  --exclude='*.pyo'
  --exclude='*.o'
  --exclude='*.so'
  --exclude='*.log'
  --exclude='passwords.csv'
  --exclude='*.pass'
  --exclude='xai.key'
  --exclude='.wp-oauth.json'
  --exclude='wp-oauth.json'
  --exclude='*_private.key'
  --exclude='secrets/'
)

write_prog() {
  # args as env-like key=val lines via python-free printf JSON
  local pct=${1:-0} msg=${2:-} done=${3:-false} running=${4:-true}
  local files_done=${5:-0} files_total=${6:-0} bytes_done=${7:-0} bytes_total=${8:-0}
  local current=${9:-}
  # escape current for JSON
  current=${current//\\/\\\\}
  current=${current//\"/\\\"}
  msg=${msg//\\/\\\\}
  msg=${msg//\"/\\\"}
  cat >"$PROG" <<EOF
{
  "ts": "$(date -Iseconds)",
  "src": "$SRC",
  "dest": "$DEST",
  "public": "https://share.biggrinrtx.com/x/",
  "watch": "https://share.biggrinrtx.com/upload-watch.html",
  "wp": "https://biggrinrtx.com/share/",
  "email_wp": "https://biggrinrtx.com/email/",
  "running": $running,
  "done": $done,
  "ok": true,
  "phase": "$([ "$done" = true ] && echo done || echo upload)",
  "pct": $pct,
  "message": "$msg",
  "files_done": $files_done,
  "files_total": $files_total,
  "bytes_done": $bytes_done,
  "bytes_total": $bytes_total,
  "current": "$current",
  "errors": [],
  "error_n": 0,
  "law": "NO python · NO xdg-open · HTML5/JS watch · JSON progress · free thrift"
}
EOF
}

say() { echo "[$(date -Iseconds)] $*" | tee -a "$LOG"; }

if [[ ! -d "$SRC" ]]; then
  echo "missing $SRC" >&2
  exit 1
fi

write_prog 0 "scanning…" false true 0 0 0 0 ""
say "scan $SRC → $DEST"

# file list via find (no python)
LIST=$(mktemp)
find "$SRC" -type f \
  ! -path '*/.git/*' \
  ! -path '*/__pycache__/*' \
  ! -path '*/node_modules/*' \
  ! -path '*/OpenPaperWork/*' \
  ! -path '*/.venv/*' \
  ! -path '*/venv/*' \
  ! -path '*/secrets/*' \
  ! -name '*.pyc' ! -name '*.pyo' ! -name '*.o' ! -name '*.so' ! -name '*.log' \
  ! -name 'passwords.csv' ! -name '*.pass' ! -name 'xai.key' \
  ! -name '.wp-oauth.json' ! -name 'wp-oauth.json' ! -name '*_private.key' \
  >"$LIST" 2>/dev/null || true

files_total=$(wc -l <"$LIST" | tr -d ' ')
bytes_total=$(cat "$LIST" | tr '\n' '\0' | du -cb --files0-from=- 2>/dev/null | tail -1 | awk '{print $1}')
bytes_total=${bytes_total:-0}
say "files=$files_total bytes=$bytes_total"

write_prog 1 "uploading $files_total files" false true 0 "$files_total" 0 "$bytes_total" ""

# rsync preferred · progress via --info=progress2 not parseable easily · copy loop with JSON ticks
files_done=0
bytes_done=0
while IFS= read -r f; do
  [[ -z "$f" || ! -f "$f" ]] && continue
  rel="${f#$SRC/}"
  dest="$DEST/$rel"
  mkdir -p "$(dirname "$dest")" 2>/dev/null || true
  if cp -a "$f" "$dest" 2>/dev/null; then
    chmod a+r "$dest" 2>/dev/null || true
  fi
  sz=$(stat -c '%s' "$f" 2>/dev/null || echo 0)
  files_done=$((files_done + 1))
  bytes_done=$((bytes_done + sz))
  if (( files_total > 0 )); then
    pct=$(( files_done * 100 / files_total ))
  else
    pct=100
  fi
  # throttle JSON writes
  if (( files_done % 40 == 0 || files_done == files_total )); then
    write_prog "$pct" "$files_done/$files_total · ${pct}%" false true \
      "$files_done" "$files_total" "$bytes_done" "$bytes_total" "$rel"
  fi
done <"$LIST"
rm -f "$LIST"

echo "Projects/x · share.biggrinrtx.com/x/ · NO xdg-open · HTML5 watch" >"$DEST/README_SHARE.txt"
write_prog 100 "upload complete" true false "$files_done" "$files_total" "$bytes_done" "$bytes_total" ""
say "DONE files=$files_done bytes=$bytes_done → $DEST"
# never xdg-open · print URL only
echo "WATCH https://share.biggrinrtx.com/upload-watch.html"
echo "TREE  https://share.biggrinrtx.com/x/"
echo "WP    https://biggrinrtx.com/share/"
echo "EMAIL https://biggrinrtx.com/email/"
