#!/bin/zsh
exec > /tmp/heaven_finish.log 2>&1
# wait up to 8 min for all 6 images (or the build to give up)
for i in {1..96}; do
  n=$(ls /Users/jasonlangley/ornith-builds/heaven/images/*.jpg 2>/dev/null | wc -l | tr -d ' ')
  grep -q "^DONE" /tmp/heaven.log 2>/dev/null && break
  [ "$n" -ge 6 ] && break
  sleep 5
done
CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
"$CHROME" --headless --disable-gpu --no-sandbox --hide-scrollbars \
  --window-size=1200,1700 --virtual-time-budget=9000 \
  --screenshot=/tmp/heaven.png "file:///Users/jasonlangley/ornith-builds/heaven/index.html"
set -a; . /Users/jasonlangley/.hermes/.env 2>/dev/null; set +a
CHAT="${TELEGRAM_HOME_CHANNEL:-${TELEGRAM_ALLOWED_USERS%%,*}}"
N=$(ls /Users/jasonlangley/ornith-builds/heaven/images/*.jpg 2>/dev/null | wc -l | tr -d ' ')
curl -s --max-time 60 -F "chat_id=${CHAT}" -F "photo=@/tmp/heaven.png" \
  -F "caption=Heaven — Ornith designed the whole page and chose all its images himself. (${N}/6 images painted.)" \
  "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendPhoto" >/dev/null
echo "sent ($N/6 images)"
