#!/bin/bash
#############################################################################
#                                                                           #
# This file is part of the "ubuntu" module of the otoolbox project.         #
#                                                                           #
# This script is open-source and intended for automation purposes.          #
# It is distributed in the hope that it will be useful, but WITHOUT         #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        #
# or FITNESS FOR A PARTICULAR PURPOSE.                                      #
#                                                                           #
# Use of this script is entirely at your own risk.                          #
#                                                                           #
# Copyright (c) The otoolbox contributors.                                  #
#############################################################################
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "$SCRIPT_DIR/bulk-common"
cd "$WORKSPACE_ROOT" || { echo "Error: failed to change directory to $WORKSPACE_ROOT" >&2; exit 1; }


########################################################################################
# Parse CLI arguments
########################################################################################
COMMIT_MESSAGE="bulk: automated commit"
POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
    case "$1" in
        -m|--message)
            shift
            if [[ -z "${1:-}" ]]; then
                echo "Error: missing value for --message" >&2
                exit 1
            fi
            COMMIT_MESSAGE="$1"
            ;;
        -h|--help)
            cat <<'EOF'
Usage: bulk-commit [OPTIONS] [MESSAGE]

Commit all pending changes in configured organizations.

Options:
  -m, --message TEXT   Commit message to use
  -h, --help           Show this help and exit

Examples:
  bulk-commit -m "chore: sync updates"
  bulk-commit "chore: sync updates"
EOF
            exit 0
            ;;
        --)
            shift
            while [[ $# -gt 0 ]]; do
                POSITIONAL_ARGS+=("$1")
                shift
            done
            break
            ;;
        -* )
            echo "Error: unknown option '$1'" >&2
            exit 1
            ;;
        *)
            POSITIONAL_ARGS+=("$1")
            ;;
    esac
    shift
done

if [[ ${#POSITIONAL_ARGS[@]} -gt 0 ]]; then
    COMMIT_MESSAGE="${POSITIONAL_ARGS[*]}"
fi


########################################################################################
# Pull latest changes from the repositories
########################################################################################
ORGANIZATIONS=(
    "$PUBLIC_ORGANIZATION" \
    "odoo" \
    "oca" \
)

for organization in "${ORGANIZATIONS[@]}"; do
    if [[ -d "$WORKSPACE_ROOT/$organization" ]]; then
        echo "Committing changes in $organization organization"
        find "$WORKSPACE_ROOT/$organization" -maxdepth 1 -type d -name ".git" -prune -o -type d -print | while read -r repo; do
            if [[ -d "$repo/.git" ]]; then
                cd "$repo" || continue
                if [[ -n $(git status -s) ]]; then
                    git add -A
                    git commit -m "$COMMIT_MESSAGE" >> "$LOG_FILE" 2>&1
                fi
            fi
        done
    else
        echo "Repository $organization not found, skipping commit for $organization organization"
    fi
done

