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

# Created by argbash-init v2.10.0
# DEFINE_SCRIPT_DIR([])
# INCLUDE_PARSING_CODE([polap-parsing.sh])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info

# set -e
# set -u
# set -euo pipefail
# _POLAP_DEBUG=0
# _POLAP_RELEASE=0
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
: "${_POLAP_DEBUG:=0}"
export _POLAP_DEBUG
: "${_POLAP_RELEASE:=0}"
export _POLAP_RELEASE

# OTHER STUFF GENERATED BY Argbash
_polap_script_bin_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || {
	echo "Couldn't determine the script's running directory, which probably matters, bailing out" >&2
	exit 2
}
_POLAPLIB_DIR="${_polap_script_bin_dir}/polaplib"
source "${_POLAPLIB_DIR}/polap-git-hash-version.sh"
_polap_version=v0.4.3.7-"${_polap_git_hash_version}"
. "${_POLAPLIB_DIR}/polap-parsing.sh" # '.' means 'source'
_polap_output_dest="/dev/null"

### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash

# vvv  PLACE YOUR CODE HERE  vvv

################################################################################
# include bash shell files
################################################################################

# for var in $(compgen -v _arg_); do
# 	echo "$var=${!var}"
# done

source "${_POLAPLIB_DIR}/polap-constants.sh"
source "${_POLAPLIB_DIR}/polap-function-debug.sh"
source "${_POLAPLIB_DIR}/polap-function-steps.sh"
source "${_POLAPLIB_DIR}/run-polap-function-errors.sh"
source "${_POLAPLIB_DIR}/run-polap-function-utilities.sh"
source "${_POLAPLIB_DIR}/run-polap-function-log.sh"
source "${_POLAPLIB_DIR}/polap-lib-conda.sh"
source "${_POLAPLIB_DIR}/polap-lib-file.sh"
source "${_POLAPLIB_DIR}/polap-lib-filepath.sh"
source "${_POLAPLIB_DIR}/polap-lib-fastq.sh"
source "${_POLAPLIB_DIR}/polap-lib-array.sh"
source "${_POLAPLIB_DIR}/polap-lib-table.sh"
source "${_POLAPLIB_DIR}/polap-lib-timing.sh"
source "${_POLAPLIB_DIR}/polap-lib-number.sh"
source "${_POLAPLIB_DIR}/polap-lib-ncbi.sh"
source "${_POLAPLIB_DIR}/polap-lib-random.sh"
source "${_POLAPLIB_DIR}/polap-lib-process.sh"
source "${_POLAPLIB_DIR}/run-polap-function-menus.sh"
source "${_POLAPLIB_DIR}/polap-function-set-variables.sh"
source "${_POLAPLIB_DIR}/run-polap-function-fastq.sh"
source "${_POLAPLIB_DIR}/run-polap-function-fasta.sh"
source "${_POLAPLIB_DIR}/run-polap-function-random.sh"
source "${_POLAPLIB_DIR}/run-polap-function-bandage.sh"
source "${_POLAPLIB_DIR}/run-polap-function-wga.sh"
source "${_POLAPLIB_DIR}/run-polap-function-nextdenovo.sh"

source "${_POLAPLIB_DIR}/polap-function-disassemble-seeds.sh"

# organelle-genome annotation
source "${_POLAPLIB_DIR}/run-polap-function-annotate.sh"
source "${_POLAPLIB_DIR}/run-polap-function-annotate-contig.sh"

# read selection and organelle-genome assembly
source "${_POLAPLIB_DIR}/run-polap-function-oga.sh"
source "${_POLAPLIB_DIR}/run-polap-function-dga.sh"
source "${_POLAPLIB_DIR}/run-polap-function-polishing.sh"

# Template
source "${_POLAPLIB_DIR}/run-polap-function-template.sh"

# NCBI: BioProject
source "${_POLAPLIB_DIR}/run-polap-function-bioproject.sh"

# contig selection
source "${_POLAPLIB_DIR}/run-polap-function-seeds.sh"

# mtDNA or ptDNA selection
# mtDNA annotation and plotting
# NCBI: mtDNA
source "${_POLAPLIB_DIR}/run-polap-function-mtdna.sh"

# report, archive, and cleanup
source "${_POLAPLIB_DIR}/run-polap-function-archive.sh"

# assemble
source "${_POLAPLIB_DIR}/run-polap-function-assemble.sh"
source "${_POLAPLIB_DIR}/run-polap-function-disassemble.sh"
source "${_POLAPLIB_DIR}/run-polap-function-directional.sh"

# miscellaneous
source "${_POLAPLIB_DIR}/run-polap-function-miscellaneous.sh"

source "${_POLAPLIB_DIR}/run-polap-function-test.sh"
source "${_POLAPLIB_DIR}/run-polap-function-taxonomy.sh"

source "${_POLAPLIB_DIR}/run-polap-function-install.sh"
source "${_POLAPLIB_DIR}/run-polap-function-demo.sh"
source "${_POLAPLIB_DIR}/run-polap-function-ncbixml.sh"
source "${_POLAPLIB_DIR}/run-polap-function-config.sh"

source "${_POLAPLIB_DIR}/polap-cmd-ptgaul.sh"

################################################################################
# All of the variables at our disposal
################################################################################

################################################################
# MAIN
#
if [ $# -eq 0 ]; then
	print_help
	touch make-menus
	exit $EXIT_SUCCESS
fi

# the default output o is created.
# o/polap.log, o/tmp, o/log are created as well.
source "${_POLAPLIB_DIR}/polap-variables-main.sh"

# see polap-variables-main.sh for the logit function.
# all message to a log file
# https://stackoverflow.com/questions/49851882/how-to-log-echo-statement-with-timestamp-in-shell-script
exec 3>&1 1>> >(logit)
# exec 3>&1 1>> >(logit) 2>&1

[[ "${_arg_clock}" == "on" ]] && date +"%Y-%m-%d %H:%M:%S" >&3
CMD="$0 $*"
echo "POLAP: ${_polap_version}"
echo "CMD: $CMD"

# Print all the global variables from polap-parsing.sh.
for var in $(compgen -v _arg_); do
	echo "$var=${!var}"
done

# Call a subcommand function
if declare -f "_run_polap_${_arg_menu[0]}" >/dev/null 2>&1; then
	# invoke that function, passing arguments through
	# _run_polap_"$@" # same as "$1" "$2" "$3" ... for full argument list
	if _run_polap_${_arg_menu[0]}; then
		_polap_log3 "Menu ${_arg_menu[0]} has been finished."
	else
		_polap_error_message $?
	fi
else
	_polap_log0 "Menu: assemble, prepare-polishing, polish"
	_polap_log0 "  assemble1, total-length-long, find-genome-size, reduce-data, flye1, annotate"
	_polap_log0 "  assemble2, select-seeds, map-reads, test-reads, select-reads, flye2"
	_polap_log0 "ERROR: no such menu of $1"
fi

[[ "${_arg_clock}" == "on" ]] && date +"%Y-%m-%d %H:%M:%S" >&3
ELAPSED="Time at $(hostname): $((SECONDS / 3600))hrs $(((SECONDS / 60) % 60))min $((SECONDS % 60))sec - $CMD"
echo "$ELAPSED"

# Close FD 3 when no longer needed
exec 3>&-

# ^^^  TERMINATE YOUR CODE BEFORE THE BOTTOM ARGBASH MARKER  ^^^

# ] <-- needed because of Argbash
