#!/usr/bin/env bash
################################################################################
# This file is part of polap.
#
# polap is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# polap 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. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# polap. If not, see <https://www.gnu.org/licenses/>.
################################################################################

set -Eeuo pipefail
set -o errtrace
set -o functrace

if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
	echo "[ERROR] This script must be executed, not sourced: use 'bash $BASH_SOURCE'" >&2
	return 1 2>/dev/null || exit 1
fi

# We want to set the environment variable, _POLAP_DEBUG.
# _POLAP_DEBUG=1 bash polap.sh
# If _POLAP_DEBUG is not already set, set it to 0. Then make sure it is visible to all subprocesses.
# The leading colon (:) is simply a no-op command to wrap that parameter expansion safely:
# The following two are equivalent.
# 1)
# : "${_POLAP_DEBUG:=0}"
# 2)
# if [[ -z "${_POLAP_DEBUG}" ]]; then
#     _POLAP_DEBUG=0
# fi
: "${_POLAP_DEBUG:=0}"
export _POLAP_DEBUG
: "${_POLAP_RELEASE:=0}"
export _POLAP_RELEASE

_local_host="thorne"
_media_dir="/media/h2/sra"
_media1_dir="/media/h1/sra"
_media2_dir="/media/h2/sra"

# Local site defaults (override via env if needed)
: "${_LOCAL_HOST:=thorne}"
: "${_MEDIA_DIR:=/media/h2/sra}"

# Determine the base directory of the bolap command.
# We use _POLAPLIB_DIR in everywhere in the code.
_polap_script_bin_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || {
	echo "Couldn't determine the script's running directory, bailing out" >&2
	exit 2
}
_POLAPLIB_DIR="${_polap_script_bin_dir}/polaplib"

# Create $HOME/.polap/profiles if it is absent.
if [[ -r "${_POLAPLIB_DIR}/polap-lib-profiles.sh" ]]; then
	source "${_POLAPLIB_DIR}/polap-lib-profiles.sh"
	_polap_ensure_profiles_dir
fi

# Display what bash libraries are loaded.
for arg in "$@"; do
	case "$arg" in
	--trace-load)
		export POLAP_AUTOLOAD_TRACE=1
		shift
		;;
	esac
done

# Load bash libraries.
source "${_POLAPLIB_DIR}/polap-lib-version.sh"
source "${_POLAPLIB_DIR}/bolap-parsing.sh"

# Set if the bolap type is not set in the command-line options.
: "${_bolap_type:=read}"
_polap_output_dest="/dev/null"
source "${_POLAPLIB_DIR}/polap-bash-bolap-autoload-min.sh"
bolap_autoload_min "${_POLAPLIB_DIR}" "${_bolap_type}"

# Load more if not loaded by the autoloader.
source "${_POLAPLIB_DIR}/polap-lib-command.sh"
source "${_POLAPLIB_DIR}/polap-lib-bolap-compat.sh"
source "${_POLAPLIB_DIR}/polap-lib-seqkit.sh"
source "${_POLAPLIB_DIR}/bolap-lib-dataset.sh"
source "${_POLAPLIB_DIR}/polap-lib-dialog.sh"
source "${_POLAPLIB_DIR}/polap-lib-help.sh"

# Main
_run_bolap
