#!/bin/bash

# Create hooks directory if it doesn't exist
mkdir -p .git/hooks

# Create pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash

# Enable detailed debugging
set -x

# Print initial environment information
echo "Initial environment: GIT_DIR=$GIT_DIR, GIT_INDEX_FILE=$GIT_INDEX_FILE"
echo "Current working directory: $(pwd)"

# Store original Git environment variables
ORIGINAL_GIT_DIR="$GIT_DIR"
ORIGINAL_GIT_INDEX="$GIT_INDEX_FILE"

# Explicitly unset all Git environment variables
# This is critical for nested Git commands to work properly
unset GIT_DIR
unset GIT_INDEX_FILE
unset GIT_WORK_TREE
unset GIT_OBJECT_DIRECTORY
unset GIT_COMMON_DIR

echo "Running pre-commit hook: Updating markdown tables..."

# Get the absolute path to BoxPwnr-Attempts repo
REPO_ROOT=$(pwd)
ATTEMPTS_REPO="$(dirname "$REPO_ROOT")/BoxPwnr-Attempts"
echo "REPO_ROOT: $REPO_ROOT"
echo "ATTEMPTS_REPO: $ATTEMPTS_REPO"

if [ ! -d "$ATTEMPTS_REPO" ]; then
    echo "Error: BoxPwnr-Attempts directory not found at $ATTEMPTS_REPO"
    echo "Please make sure BoxPwnr-Attempts repo is cloned at the same level as BoxPwnr"
    exit 1
fi

# Create a temporary directory for the clean worktree
TEMP_DIR=$(mktemp -d)
echo "Created temp directory: $TEMP_DIR"
trap 'rm -rf "$TEMP_DIR"' EXIT

# Use a completely isolated subshell for git operations
echo "Creating worktree using isolated subshell..."
(
    cd "$ATTEMPTS_REPO"
    git worktree add "$TEMP_DIR" HEAD
) || {
    echo "Error: Failed to create temporary worktree"
    exit 1
}

# Run the table generator script from the clean worktree
echo "Running table generator script..."
python3 "$TEMP_DIR/scripts/generate_markdown_tables.py" --repo-type boxpwnr --repo "$ATTEMPTS_REPO"

# Restore original Git environment if needed
export GIT_DIR="$ORIGINAL_GIT_DIR"
export GIT_INDEX_FILE="$ORIGINAL_GIT_INDEX"

# Add the updated README to the commit
echo "Adding README.md to commit..."
git add README.md

# Exit successfully
echo "Pre-commit hook completed successfully"
exit 0
EOF

# Make the pre-commit hook executable
chmod +x .git/hooks/pre-commit

echo "Git hooks installed successfully!" 