#!/bin/bash

# Check for required Git configuration files
if [ ! -f "$HOME/.gitconfig" ]; then
	>&2 echo "Error: Git configuration file not found at $HOME/.gitconfig"
	exit 1
fi

if [ ! -f "$HOME/.git-credentials" ]; then
	>&2 echo "Error: Git credentials file not found at $HOME/.git-credentials"
	exit 1
fi

# Determine which script to run and filter arguments
script_to_run="/app/git_commit_ai.py"
docker_args=()
for arg in "$@"; do
	if [[ "$arg" == "-r" ]]; then
		script_to_run="/app/git_rebase_ai.py"
	elif [[ "$arg" == "-w" ]]; then
		script_to_run="/app/git_reword_ai.py"
	else
		docker_args+=("$arg")
	fi
done

docker run --rm -it \
	-v "$(pwd):/repo" \
	-v "$HOME/.gitconfig:/home/appuser/.gitconfig:ro" \
	-v "$HOME/.git-credentials:/home/appuser/.git-credentials:ro" \
	-e GEMINI_API_KEY="$GEMINI_API_KEY" \
	-e GEMINI_MODEL="${GEMINI_MODEL:-gemini-2.0-flash}" \
	-u "$(id -u):$(id -g)" \
	${DOCKER_IMAGE_NAME:-docker.senomas.com/commit:1.1} "$script_to_run" ${docker_args[@]}