#!/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="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
COMMON_SCRIPT="$SCRIPT_DIR/bulk-common"
if [[ ! -f "$COMMON_SCRIPT" ]]; then
    echo "Error: required script not found: $COMMON_SCRIPT" >&2
    exit 1
fi
# shellcheck source=/dev/null
source "$COMMON_SCRIPT"
bulk_init "$@"


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

for organization in "${ORGANIZATIONS[@]}"; do
    if [[ -d "$WORKDIR/$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=(
    "moonsunsoft" \
)
for organization in "${PROTECTED_ORGANIZATIONS[@]}"; do
    if [[ -d "$WORKDIR/$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
