#!/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]

Initialize or update public repositories from OCA template.

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

if [[ -z "$PUBLIC_ORGANIZATION" ]]; then
    _ot_log_error "PUBLIC_ORGANIZATION is not set."
    exit 1
fi

WORKDIR="$WORKSPACE_ROOT"
TEMPLATE_FILE="$WORKDIR/gen_addon_readme.rst.jinja"
COPIER_ANSWERS_FILE="$WORKDIR/copier-answers.yml"

if [[ ! -f "$TEMPLATE_FILE" ]]; then
    _ot_log_error "Template file not found: $TEMPLATE_FILE"
    exit 1
fi

if [[ ! -f "$COPIER_ANSWERS_FILE" ]]; then
    _ot_log_error "Copier answers file not found: $COPIER_ANSWERS_FILE"
    exit 1
fi

for dir in "$WORKDIR/$PUBLIC_ORGANIZATION"/*/; do
    if [[ -d "$dir" ]]; then
        project="$(basename "$dir")"
        _ot_log_info "Processing project: $project"
        cd "$WORKDIR/$PUBLIC_ORGANIZATION/$project"

        cp -f "$TEMPLATE_FILE" "$WORKDIR/$PUBLIC_ORGANIZATION/$project/gen_addon_readme.rst.jinja"

        if [[ ! -f ".copier-answers.yml" ]]; then
            _ot_log_info "Project $project is not initialized, initializing..."
            cp "$COPIER_ANSWERS_FILE" "$WORKDIR/$PUBLIC_ORGANIZATION/$project/.copier-answers.yml"
            copier copy \
                --UNSAFE \
                --overwrite \
                --answers-file ".copier-answers.yml" \
                https://github.com/OCA/oca-addons-repo-template.git .
        else
            _ot_log_info "Project $project is already initialized."
            copier update \
                --UNSAFE \
                --skip-answered \
                --answers-file ".copier-answers.yml"
        fi
    fi
done
