#!/usr/bin/env bash

set -e -o pipefail -u

root_dir=$(realpath "${1-$HOME}")

# First, set up the regular sandbox
script_dir=$(cd "$(dirname "$0")" && pwd)
"$script_dir/setup-sandbox" "$root_dir"

# Clean up any existing worktree directories
if [ -d "$root_dir/machete-sandbox-worktrees" ]; then
  echo "Removing existing worktree directories..."
  rm -rf "$root_dir/machete-sandbox-worktrees"
fi

# Now create worktrees
cd "$root_dir/machete-sandbox"

echo
echo "Creating worktrees..."
echo

# Create worktrees for some branches
git worktree add "$root_dir/machete-sandbox-worktrees/allow-ownership-link" allow-ownership-link
echo "Created worktree for allow-ownership-link at $root_dir/machete-sandbox-worktrees/allow-ownership-link"

git worktree add "$root_dir/machete-sandbox-worktrees/build-chain" build-chain
echo "Created worktree for build-chain at $root_dir/machete-sandbox-worktrees/build-chain"

git worktree add "$root_dir/machete-sandbox-worktrees/master" master
echo "Created worktree for master at $root_dir/machete-sandbox-worktrees/master"

# Create a worktree with detached HEAD at the tip of call-ws
call_ws_sha=$(git rev-parse call-ws)
git worktree add --detach "$root_dir/machete-sandbox-worktrees/detached-call-ws" "$call_ws_sha"
echo "Created worktree with detached HEAD at $root_dir/machete-sandbox-worktrees/detached-call-ws (commit $call_ws_sha from call-ws)"

# Create another worktree with detached HEAD at the tip of hotfix/add-trigger
hotfix_sha=$(git rev-parse hotfix/add-trigger)
git worktree add --detach "$root_dir/machete-sandbox-worktrees/detached-hotfix" "$hotfix_sha"
echo "Created worktree with detached HEAD at $root_dir/machete-sandbox-worktrees/detached-hotfix (commit $hotfix_sha from hotfix/add-trigger)"

echo
echo "Worktree list:"
git worktree list
echo
echo
git machete status
echo
echo "Note: Branches 'develop' and 'call-ws' are NOT checked out in any worktree."
echo "This demonstrates the behavior controlled by:"
echo "  machete.traverse.whenBranchNotCheckedOutInAnyWorktree"
echo
echo "To stay in the current worktree when checking out such branches:"
echo "  git config machete.traverse.whenBranchNotCheckedOutInAnyWorktree stay-in-the-current-worktree"
echo
echo "To use the default behavior (cd to main worktree):"
echo "  git config machete.traverse.whenBranchNotCheckedOutInAnyWorktree cd-into-main-worktree"
echo
