#!/usr/bin/env bash
#
# Localhost SonarQube main executable shell script.

set -e

#######################################
# Prevent Sourcing
#
# Firstly, we need to ensure Localhost
# SonarQube is executed in a new shell
# process.
#######################################

if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
  echo >&2 "ERROR: \`Localhost SonarQube\` cannot be sourced."

  exit 1
fi

#######################################
# Load DotEnv support
#
# Secondly, we need to be able to load
# environment variables.
#######################################

BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
readonly BIN_DIR

# shellcheck source=/dev/null
source "${BIN_DIR}/env/dotenv.sh"

DOTENV="$( dirname "${BIN_DIR}" )/.env"
readonly DOTENV

dotenv::load "${DOTENV}"

#######################################
# Define Global Constant
#
# Secondly, we determine the Localhost
# SonarQube directory and load config.
#######################################

# shellcheck source=/dev/null
source "${BIN_DIR}/config/_config.sh"

#######################################
# Load Dependencies
#
# Next, we "glue" all the components of
# Localhost SonarQube and source them.
#######################################

if [[ ! "$( ls -A "${BIN_DIR}/packages" )" ]]; then
  echo >&2 "ERROR: missing dependencies for \`Localhost SonarQube\`."

  exit 1
fi

# shellcheck source=/dev/null
source "${BIN_DIR}/lib/_lib.sh"

# shellcheck source=/dev/null
source "${BIN_DIR}/bootstrap/app.sh"

#######################################
# Run The Application
#
# Finally, we execute the Localhost
# SonarQube application main function.
#######################################

main "$@"
