#!/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)
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"  # preserved for compat
source "$SCRIPT_PATH/otoolbox-common"

VERBOSE=false
DEBUG=false

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

Initialize test directory structure for all addons in public repositories.

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"
_ot_log_debug "Current directory is: $CURRENT_DIR"

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

        for addon_dir in "$WORKDIR/$PUBLIC_ORGANIZATION/$project"/*; do
            if [[ -d "$addon_dir" ]]; then
                if [[ -f "$addon_dir/__manifest__.py" ]]; then
                    addon_name="$(basename "$addon_dir")"
                    _ot_log_info "Processing addon: $addon_name"

                    # Ensure tests/__init__.py exists (remove if it is a directory by mistake)
                    if [[ -d "$addon_dir/tests/__init__.py" ]]; then
                        rm -fR "$addon_dir/tests/__init__.py"
                    fi

                    mkdir -p "$addon_dir/tests"
                    touch "$addon_dir/tests/__init__.py"
                    _ot_log_debug "Initialized tests/__init__.py for $addon_name"
                fi
            fi
        done
    fi
done
