#!/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
DEBUG=0
# echo ${_xyz}

# OTHER STUFF GENERATED BY Argbash
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" || {
	echo "Couldn't determine the script's running directory, which probably matters, bailing out" >&2
	exit 2
}
. "$script_dir/polap-parsing.sh" # '.' means 'source'

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

# vvv  PLACE YOUR CODE HERE  vvv

_polap_var_function_verbose=4

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

source "$script_dir/polap-constants.sh"
source "$script_dir/run-polap-function-errors.sh"
source "$script_dir/run-polap-function-utilities.sh"
source "$script_dir/run-polap-function-log.sh"
source "$script_dir/run-polap-function-menus.sh"
source "$script_dir/polap-function-set-variables.sh"
source "$script_dir/run-polap-function-wga.sh"

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

# read selection and organelle-genome assembly
source "$script_dir/run-polap-function-oga.sh"
source "$script_dir/run-polap-function-polishing.sh"

# Template
source "$script_dir/run-polap-function-template.sh"

# NCBI: BioProject
source "$script_dir/run-polap-function-bioproject.sh"

# contig selection
source "$script_dir/run-polap-function-seeds.sh"

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

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

# assemble
source "$script_dir/run-polap-function-assemble.sh"

# miscellaneous
source "$script_dir/run-polap-function-miscellaneous.sh"

source "$script_dir/run-polap-function-test.sh"
source "$script_dir/run-polap-function-taxonomy.sh"

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

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

source "$script_dir/polap-variables-main.sh"

# 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"

# subcommand function call
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
