#!/usr/bin/env bash
set -Eeuo pipefail
trap 'echo >&2 "${BASH_SOURCE[0]}: line $LINENO: $BASH_COMMAND: exitcode $?"' ERR
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"


###############################################################################
# Functions
###############################################################################
[[ "${VERBOSE:-0}" =~ ^[0-9]+$ ]] && VERBOSE="${VERBOSE:-0}" || VERBOSE=1
trace () {
    [[ "$VERBOSE" -lt 2 ]] || echo >&2 -e "🔬 \033[90m$*\033[0m"
}
debug () {
    [[ "$VERBOSE" -lt 1 ]] || echo >&2 -e "🔍 \033[36m$*\033[0m"
}
info () {
    echo >&2 -e "ℹ️ \033[36m$*\033[0m"
}
warn () {
    echo >&2 -e "⚠️ \033[33m$*\033[0m"
}
error () {
    echo >&2 -e "❌ \033[31m$*\033[0m"
}
abort () {
    error "$*"
    exit 1
}


###############################################################################
# Create keychain to avoid error dialog
# error message: "a keychain cannot be found to store ..."
#
# This occurs when connecting using "sudo --user=admin ..."
# but not when using "ssh admin@..."
#
# TODO: I guess the sv script could call this configure script directly
# with the password the first time instead of it being called from .zshrc
###############################################################################
# Explicitly specify the path to avoid confusion with the host user;
# - security dump-keychain                 => host user
# - security dump-keychain $LOGIN_KEYCHAIN => admin user
LOGIN_KEYCHAIN="$HOME/Library/Keychains/login.keychain-db"
if [[ ! -f "$LOGIN_KEYCHAIN" ]]; then
    debug "Creating keychain without password..."
    security create-keychain -p '' "$LOGIN_KEYCHAIN"
fi

# Unlock the keychain to avoid password dialogs
debug "Unlocking keychain..."
security unlock-keychain -p '' "$LOGIN_KEYCHAIN" 2>/dev/null || true


###############################################################################
# Xcode
###############################################################################
# It's really useful to be able to run XCode inside the VM, especially with
# worktrees to allow multiple agents to work on the code at the same time.
#
# However, the default configuration for Xcode stores build files (e.g. object
# files) in ~/Library/Developer/Xcode/DerivedData, which is inconvenient because
# running multiple agents on separate branches causes file-sharing conflicts;
# who knows what kind of frankenstein-app will get built when two branches
# are compiled into the same folder.
#
# The settings below build the Xcode application into the current directory,
# e.g. $REPO/.DerivedData
#
# You'll need to account for this in your .gitignore file. Ta!
#
# See https://github.com/webcoyote/clodpod/blob/main/scripts/worktree for a
# simple implementation of worktrees.
debug "Configuring xcode..."
defaults write com.apple.dt.Xcode DerivedDataLocationStyle Custom
defaults write com.apple.dt.Xcode IDECustomDerivedDataLocation ".DerivedData"


###############################################################################
# Copy user directory contents to $HOME
###############################################################################
# Copy contents of guest/home/user to $HOME, excluding zsh config files
USER_DIR="$SCRIPT_DIR/user"
if [[ -d "$USER_DIR" ]]; then
    debug "Copying user directory contents to $HOME"
    # Use rsync to copy while excluding specific files
    rsync \
        --quiet \
        --archive \
        --exclude='.git' \
        --exclude='.gitattributes' \
        --exclude='.gitignore' \
        --exclude='README.md' \
        --exclude='configure' \
        --exclude='.zshenv' \
        --exclude='.zprofile' \
        --exclude='.zshrc' \
        --exclude='.zlogin' \
        --exclude='.zlogout' \
        --exclude='bin/claude' \
        --exclude='bin/codex' \
        --exclude='bin/gemini' \
        "$USER_DIR/" "$HOME/" 2>/dev/null
fi
