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

Open an interactive shell in a customer repository.

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[@]}"
customer_init_working_dir "$CUSTOMER_DIR"
customer_load_env_file "$CUSTOMER_DIR"


# Customer Info
cd "$CUSTOMER_DIR"
customer_print_info

_ot_log_info "Customer shell initialized. Current branch: $BRANCH"
_ot_log_info "Customer directory: $CUSTOMER_DIR"
_ot_log_info "Customer repositories: \n$REMOTE_REPO"

