#!/bin/bash

# ClaudeVM - Wrapper script to make it easier to run from anywhere

# Get the directory where this script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Check if the executable exists, if not build it
if [ ! -f "$SCRIPT_DIR/.build/debug/ClaudeVM" ]; then
    echo "Building ClaudeVM..."
    cd "$SCRIPT_DIR" && swift build
    if [ $? -ne 0 ]; then
        echo "Error: Failed to build ClaudeVM. Please make sure Swift is installed."
        exit 1
    fi
    echo "Build completed successfully."
fi

# Check for sudo if installation is requested
if [[ "$*" == *"--install"* || "$*" == *"-i"* ]]; then
    if [ "$EUID" -ne 0 ]; then
        echo "Warning: Installation mode requires admin privileges."
        echo "Restarting with sudo..."
        exec sudo "$0" "$@"
        exit $?
    fi
fi

# Print header
echo "ClaudeVM - Run directories in a VM with copy-on-write access"
echo "==========================================================="

# Run the Swift executable
"$SCRIPT_DIR/.build/debug/ClaudeVM" "$@"

# Exit with the same code as the Swift process
exit $?