← back to Dw Vendor Microsites

launch-window.sh

80 lines

#!/usr/bin/env bash
# launch-window.sh <vendor_code> [vendor_code ...]   (max 6)
#
# Opens ONE iTerm2 window with a 6-pane grid; each pane cd's to this repo and runs a
# Claude session instructed to build that vendor's microsite to BUILT (no deploy).
# Reuses the iterm-restore 6-col grid engine pattern.
#
# Mac2-only (iTerm). Uses the Max-plan `claude` (= $0).
set -euo pipefail

REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
[ "$#" -ge 1 ] || { echo "usage: launch-window.sh <vendor_code> [.. up to 6]" >&2; exit 1; }
[ "$#" -le 6 ] || { echo "launch-window: max 6 vendors per window (got $#)" >&2; exit 1; }

VENDORS=("$@")
N=${#VENDORS[@]}

# screen geometry -> grid columns (cap at 6, matching restore.sh)
WIDTH=$(system_profiler SPDisplaysDataType 2>/dev/null | awk -F'[ x]+' '/UI Looks like/{print $5; exit}')
[ -z "${WIDTH:-}" ] && WIDTH=1600
HEIGHT=$(system_profiler SPDisplaysDataType 2>/dev/null | awk -F'[ x]+' '/UI Looks like/{print $6; exit}')
[ -z "${HEIGHT:-}" ] && HEIGHT=1000
COLS=$(( WIDTH / 640 )); [ "$COLS" -lt 1 ] && COLS=1; [ "$COLS" -gt 6 ] && COLS=6; [ "$COLS" -gt "$N" ] && COLS=$N

base=$(( N / COLS )); extra=$(( N % COLS ))
declare -a COLROWS=()
for ((c=0;c<COLS;c++)); do r=$base; [ $c -lt $extra ] && r=$((r+1)); COLROWS+=("$r"); done

claude_cmd() {
  local v="$1"
  local prompt="You are a DW microsite build worker. cd to ${REPO} and run: bash ./build-vendor.sh ${v}  -- this builds the vendor microsite for '${v}' to local-verified BUILT status. LOCAL ONLY: do NOT deploy, do NOT touch DNS, do NOT write to dw_unified, no Shopify, no spend. If image health flags NEEDS_RESCRAPE, that is a flag not a blocker -- record it and continue. Stop when build-vendor.sh reports BUILT (or BUILT_NEEDS_RESCRAPE). Report the final manifest line."
  # escape for AppleScript double-quoted string
  local esc="${prompt//\\/\\\\}"; esc="${esc//\"/\\\"}"
  printf 'cd \\"%s\\" && clear && echo \\"=== BUILD %s ===\\" && claude \\"%s\\"' "$REPO" "$v" "$esc"
}

SCPT="$(mktemp /tmp/launch-window.XXXXXX.applescript)"
{
  echo 'tell application "iTerm2"'
  echo '  activate'
  echo '  set w to (create window with default profile)'
  echo "  set bounds of w to {0, 25, $WIDTH, $HEIGHT}"
  echo '  tell w'
  echo '    set col1 to current session'
  declare -a CV=("col1") ; declare -a CW=("1000")
  for ((k=1;k<COLS;k++)); do
    wi=0; wmax=-1
    for ((j=0;j<${#CW[@]};j++)); do [ "${CW[$j]}" -gt "$wmax" ] && { wmax=${CW[$j]}; wi=$j; }; done
    newv="col$((k+1))"
    echo "    tell ${CV[$wi]} to set ${newv} to (split vertically with default profile)"
    half=$(( wmax / 2 )); CW[$wi]=$half; CV+=("$newv"); CW+=("$half")
  done
  i=0
  for ((c=0;c<COLS;c++)); do
    ctop="${CV[$c]}"; rows="${COLROWS[$c]}"
    declare -a RV=("$ctop") ; declare -a RH=("1000")
    for ((k=1;k<rows;k++)); do
      hi=0; hmax=-1
      for ((j=0;j<${#RH[@]};j++)); do [ "${RH[$j]}" -gt "$hmax" ] && { hmax=${RH[$j]}; hi=$j; }; done
      newv="c${c}r${k}"
      echo "    tell ${RV[$hi]} to set ${newv} to (split horizontally with default profile)"
      half=$(( hmax / 2 )); RH[$hi]=$half; RV+=("$newv"); RH+=("$half")
    done
    for ((j=0;j<rows;j++)); do
      v="${VENDORS[$i]}"; i=$((i+1))
      echo "    tell ${RV[$j]} to write text \"$(claude_cmd "$v")\""
    done
    unset RV RH
  done
  echo '  end tell'
  echo 'end tell'
} > "$SCPT"

if ! osacompile -o /dev/null "$SCPT" 2>/tmp/launch-window.err; then
  echo "AppleScript compile failed:"; cat /tmp/launch-window.err; echo "script: $SCPT"; exit 1
fi
if [ "${DRY:-0}" = "1" ]; then echo "--- DRY RUN ($SCPT) ---"; cat "$SCPT"; exit 0; fi
osascript "$SCPT"
echo "launched ${N} build panes in a ${COLS}-col grid: ${VENDORS[*]}"