#!/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; }


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

for organization in "${ORGANIZATIONS[@]}"; do
    if [[ -d "$WORKSPACE_ROOT/$organization" ]]; then
        echo "Pulling latest changes from $organization organization."
        otoolbox run update --tags "$organization" >> "$LOG_FILE" 2>&1
    else
        echo "Repository $organization not found, skipping pull for $organization organization"
    fi
done

PROTECTED_ORGANIZATIONS=(
    "$SHIELDED_ORGANIZATION" \
)
for organization in "${PROTECTED_ORGANIZATIONS[@]}"; do
    if [[ -d "$WORKSPACE_ROOT/$organization" ]]; then
        echo "Pulling latest changes from $organization organization"
        # Run otoolbox command as the worker user to pull changes from the protected repositories.
        # if the wokere user is the same as the current user, we can run the otoolbox command directly without sudo.
        if [[ "$WORKER_USER" == "$(whoami)" ]]; then
            OTOOLBOX_BIN="$(command -v otoolbox)"
            if [[ -z "$OTOOLBOX_BIN" ]]; then
                echo "Error: otoolbox not found in PATH" >&2
                exit 1
            fi
            "$OTOOLBOX_BIN" run update --tags "$organization" >> "$LOG_FILE" 2>&1
        else
            OTOOLBOX_BIN="$(sudo -u "$WORKER_USER" -H bash -lc 'command -v otoolbox')"
            if [[ -z "$OTOOLBOX_BIN" ]]; then
                echo "Error: otoolbox not found in $WORKER_USER PATH" >&2
                exit 1
            fi
            sudo -u "$WORKER_USER" -H "$OTOOLBOX_BIN" run update --tags "$organization" >> "$LOG_FILE" 2>&1
        fi
    else
        echo "Repository $organization not found, skipping pull for $organization organization"
    fi
done
