#!/bin/bash
#
# Install-Git-For-Claude.command
# --------------------------------
# Double-click this file to install Apple's Command Line Tools (which include
# git). Claude Desktop / Claude Code require git, and this resolves the
# "git needs to be installed" message.
#
# Safe to run more than once: if git is already installed it just confirms and
# exits. It only ADDS git — it never deletes or changes anything else.
#

clear
echo "============================================================"
echo "   Git setup for Claude"
echo "============================================================"
echo ""

# 1) If git already works, there's nothing to do.
if git --version >/dev/null 2>&1; then
    echo "✅  Git is already installed:"
    echo "    $(git --version)"
    echo ""
    echo "You're all set — quit Claude completely, reopen it, and the"
    echo "\"git needs to be installed\" message should be gone."
    echo ""
    read -n 1 -s -r -p "Press any key to close this window..."
    echo ""
    exit 0
fi

echo "Git was not found on this Mac."
echo ""
echo "This will launch Apple's official \"Command Line Tools\" installer,"
echo "which includes git. A separate Apple installer window will pop up."
echo ""
echo "  👉  In that window: click \"Install\", then \"Agree\","
echo "      and (if asked) enter your Mac login password."
echo ""
echo "Starting the installer now..."
echo ""

# 2) Trigger Apple's Command Line Tools installer (GUI, no Terminal admin needed).
xcode-select --install 2>/dev/null

# 3) Wait for the install to finish by watching for the installed package.
#    (We check the package registry instead of calling git, so we don't keep
#     re-popping the install dialog.)
echo "Waiting for the installation to complete..."
echo "You can watch progress in the Apple installer window."
echo "This screen will update on its own once git is ready."
echo ""

tries=0
max_tries=240   # 240 x 5s = 20 minutes
until pkgutil --pkg-info=com.apple.pkg.CLTools_Executables >/dev/null 2>&1 \
      || git --version >/dev/null 2>&1; do
    sleep 5
    tries=$((tries + 1))
    if [ $((tries % 12)) -eq 0 ]; then
        echo "   ...still installing (about $((tries / 12)) min so far)"
    fi
    if [ "$tries" -ge "$max_tries" ]; then
        echo ""
        echo "⚠️  It's taking longer than expected (20+ min)."
        echo "    If the Apple installer window closed, just double-click"
        echo "    this file again, or install git manually from:"
        echo "        https://git-scm.com/download/mac"
        echo ""
        read -n 1 -s -r -p "Press any key to close this window..."
        echo ""
        exit 1
    fi
done

# 4) Confirm.
echo ""
echo "============================================================"
if git --version >/dev/null 2>&1; then
    echo "✅  Success! Git is now installed:"
    echo "    $(git --version)"
    echo "============================================================"
    echo ""
    echo "Final step: quit Claude completely (Cmd+Q) and reopen it."
    echo "The \"git needs to be installed\" message should be gone."
else
    echo "Git tools were installed. Please QUIT and REOPEN Terminal,"
    echo "then double-click this file once more to confirm."
    echo "============================================================"
fi
echo ""
read -n 1 -s -r -p "Press any key to close this window..."
echo ""
exit 0
