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

# Resolve the script's directory, handling symlinks and aliases
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)"
}

# Load common functions and variables
SCRIPT_PATH=$(resolve_script_path)
source "$SCRIPT_PATH/otoolbox-common"
source "$SCRIPT_PATH/customer-common"

VERBOSE=false
DEBUG=false
POSITIONAL_ARGS=()

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

Initialize customer repository and clone it if missing.

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
            ;;
        --)
            shift
            while [[ $# -gt 0 ]]; do
                POSITIONAL_ARGS+=("$1")
                shift
            done
            break
            ;;
        -*)
            echo "Unknown option: $1" >&2
            show_help >&2
            exit 2
            ;;
        *)
            POSITIONAL_ARGS+=("$1")
            shift
            ;;
    esac
done

_ot_init


customer_load_working_dir "${POSITIONAL_ARGS[@]}"


_ot_run_as_worker mkdir -p "$CUSTOMER_DIR"

if [[ ! -d "$CUSTOMER_DIR/.git" ]]; then
    (cd "$CUSTOMER_DIR" && _ot_run_as_worker git clone --recurse-submodules "$CLONE_URL")
fi

if [[ ! -d "$CUSTOMER_DIR/.git" ]]; then
    _ot_log_error "Repository not found or clone failed: $ORG/$REPO"
    exit 1
else
    _ot_log_info "Repository cloned successfully: $ORG/$REPO"
fi

customer_init_working_dir "$CUSTOMER_DIR"
customer_load_env_file "$CUSTOMER_DIR"
customer_print_info
