#!/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.                                  #
#############################################################################
set -e

function resolve_script_path() {
    local source="${BASH_SOURCE[0]}"
    while [[ -L "$source" ]]; do
        local dir
        dir="$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
        source="$(readlink "$source")"
        [[ $source != /* ]] && source="$dir/$source"
    done
    echo "$(cd -P "$(dirname "$source")" >/dev/null 2>&1 && pwd)"
}

SCRIPT_PATH=$(resolve_script_path)
source "$SCRIPT_PATH/otoolbox-common"

VERBOSE=false
DEBUG=false

function show_help() {
    cat << EOF
Usage: $(basename "$0") [OPTIONS]

Add all predefined repos for core organizations and initialize customer repos.

OPTIONS:
  --help, -h      Show this help message and exit
  --verbose       Enable verbose output
  --debug         Enable debug mode

EOF
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        --help|-h)
            show_help
            exit 0
            ;;
        --verbose)
            export VERBOSE=true
            shift
            ;;
        --debug)
            export VERBOSE=true
            export DEBUG=true
            shift
            ;;
        *)
            echo "Unknown option: $1" >&2
            show_help >&2
            exit 2
            ;;
    esac
done

_ot_init

# Constants
repos=(
    "auxico" \
    "account" \
    "brand" \
    "cnp" \
    "connector" \
    "docker-postgres" \
    "education" \
    "exchange" \
    "exchange-mp" \
    "gmt" \
    "gpu" \
    "gym" \
    "hr" \
    "iot" \
    "municipality" \
    "nesa" \
    "ocaoa" \
    "odoo-book-development" \
    "online" \
    "partner-contact" \
    "payment" \
    "pep" \
    "pos" \
    "product" \
    "purchase" \
    "sale" \
    "server-tools" \
    "server-ux" \
    "sms" \
    "social" \
    "stock" \
    "tb-gateway" \
    "website" \
)

customers=(
    "Auxico-Admin/Auxico" \
    "ethanmoonsun/odoo18" \
    "discoveryfloats/odoo19" \
    "AssociatedProjects/odoo18" \
    "globalstoneaus/global" \
    "marinesuppliesasialk/odoo19" \
    "nuedgeau-dotcom/odoo17Mildura" \
    "nuedgeau-dotcom/Odoo17Innisfail" \
    "CNPBrands/Odoo17" \
    "troyaau/odoo19" \
    "derucciau/odoo19" \
    "GMTLight/odoo17" \
    "Voltarioau/odoo18" \
    "emmengemal/eythimeethi" \
    "generalpump/odoo17" \
    "futuretechnologiesau/odoo18" \
    "prestigegolfau/odoo19" \
    "KianProActive/kian-Proactive" \
    "KianProActive/odoo" \
)

_ot_log_info "---------------------------"
for project in "${repos[@]}"; do
    _ot_log_info "> add repo: odoonix/$project"
    otoolbox --silent repo add "odoonix/$project"
done

_ot_log_info "---------------------------"
for project in "${repos[@]}"; do
    _ot_log_info "> add repo: moonsunsoft/$project"
    if [[ "$WORKER_USER" == "$CURRENT_USER" ]]; then
        otoolbox --silent repo add "moonsunsoft/$project"
    else
        _ot_run_as_worker otoolbox --silent repo add "moonsunsoft/$project"
    fi
done

_ot_log_info "---------------------------"
for customer in "${customers[@]}"; do
    customer-init "$customer"
done
