#!/usr/bin/env bash
# DIY Platform: workstation setup for Windows, from Git Bash (or any MSYS2 / Cygwin bash on Windows).
#
#   curl -fsSL https://install.vibedout.app/diy-setup-windows.sh | bash
#   curl -fsSL https://install.vibedout.app/diy-setup-windows.sh | bash -s -- -Check
#   bash diy-setup-windows.sh -Skip agents,graft
#   bash diy-setup-windows.sh -Only tools
#
# What it installs is the same as diy-setup.ps1: DIY Platform, its agent tools (the MCP server registered with Claude Code, the diy
# command, diy-agentd, diy-graphd), and Git, Node.js, GitHub CLI, Python, Claude Code and Codex. Windows' own installers, its registry,
# the Startup folder and junctions are PowerShell's ground, so this downloads diy-setup.ps1 and runs it in Windows PowerShell, passing
# your options on (-Check, -Only, -Skip, -With, -BuildMachine, -Help). No administrator needed; -BuildMachine (a build machine for DIY's
# lanes, through WSL2) opens its own window as Administrator, which Windows asks about.
#
# On Linux use https://install.vibedout.app/diy-setup.sh instead.
set -euo pipefail
SITE="${DIY_SITE:-https://install.vibedout.app}"

case "$(uname -s 2>/dev/null)" in
  MINGW*|MSYS*|CYGWIN*) ;;
  Linux) if grep -qi microsoft /proc/version 2>/dev/null; then
           echo "This is WSL: DIY Platform for Windows is set up from Windows itself. Run it in Git Bash or PowerShell, or use diy-setup.sh for this Linux." >&2
         else echo "This is Linux: use  curl -fsSL $SITE/diy-setup.sh | bash" >&2; fi
         exit 2 ;;
  *) echo "This script is for Windows (Git Bash). Linux: $SITE/diy-setup.sh" >&2; exit 2 ;;
esac

PS=$(command -v powershell.exe || command -v powershell || command -v pwsh.exe || true)
[ -n "$PS" ] || { echo "Windows PowerShell was not found on the PATH (it is part of Windows: C:\\Windows\\System32\\WindowsPowerShell\\v1.0)" >&2; exit 1; }

dir=$(mktemp -d)
trap 'rm -rf "$dir"' EXIT
curl -fsSL --retry 3 --proto '=https' --tlsv1.2 -o "$dir/diy-setup.ps1" "$SITE/diy-setup.ps1" \
  || { echo "could not download $SITE/diy-setup.ps1" >&2; exit 1; }
win=$(cygpath -w "$dir/diy-setup.ps1" 2>/dev/null || echo "$dir/diy-setup.ps1")
# MSYS would rewrite arguments that look like paths; the options never are, but the script path must reach PowerShell as it is.
MSYS2_ARG_CONV_EXCL='*' "$PS" -NoProfile -ExecutionPolicy Bypass -File "$win" "$@"
