#!/bin/sh
#
# Run this script once to prepare your machine to develop Warp. This script is
# a work in progress, and may be incomplete.

set -e

if ! [ -d "/Applications/Xcode.app" ]; then
    echo "Please install Xcode from the App Store before continuing."
    exit 1
fi

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
# Mimic actually launching XCode, which performs some necessary set-up of the
# development environment.
xcodebuild -runFirstLaunch

if ! command -v brew; then
    echo "Installing brew..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    echo "Please make sure brew is set correctly in your PATH"
    exit 1
fi

if ! command -v cargo; then
    echo "Installing rust..."
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
    echo "Please start a new terminal session so that cargo is in your PATH"
    exit 1
fi

# Install various binaries through cargo.
"$PWD"/script/install_cargo_test_deps
"$PWD"/script/install_cargo_release_deps

# Install a version of cargo-bundle that supports the --profile flag
cargo install cargo-bundle --git=https://github.com/burtonageo/cargo-bundle --rev ae4c76e92c08774bf54ff077b1c52e3d1cd6c16d

# Update brew
brew update

brew install jq
brew install getsentry/tools/sentry-cli
brew install clang-format
brew install create-dmg
brew install multitime
brew install powershell
brew install pkgconf
brew install llvm

# Install PSScriptAnalyzer for PowerShell linting
pwsh -Command "Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser"

if ! [ "$(command -v docker)" ]; then
    brew install --cask docker
fi

if ! [ "$(command -v gcloud)" ]; then
    brew install google-cloud-sdk
fi

if [[ -z $(gcloud auth print-identity-token) ]]; then
    echo "gcloud CLI authentication missing.  Press enter to continue..."
    read var
    gcloud auth login
fi

# Needed for building for Mac ARM machines (e.g. M1)
rustup target add aarch64-apple-darwin
