#!/usr/bin/bash

# Resolves symlinks and gives absolute path to the script
get_script_path_bash_only() {
	local source="${BASH_SOURCE[0]}"
	while [ -h "$source" ]; do
		local dir
		dir="$(cd -P "$(dirname "$source")" && pwd)"
		source="$(readlink "$source")"
		[[ $source != /* ]] && source="$dir/$source"
	done
	cd -P "$(dirname "$source")" && pwd
}

_polap_script_bin_dir="$(get_script_path_bash_only)" || {
	echo "Couldn't determine the script's running directory, which probably matters, bailing out" >&2
	exit 2
}

echo "Script directory: $_polap_script_bin_dir"

# 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-lib-timing.sh"

_log_echo() {
	if [[ -s "${_brg_outdir}/polap-data-v2.txt" ]]; then
		echo "$(date '+%Y-%m-%d %H:%M:%S') [$subcmd1] - $1" >>"${_brg_outdir}/polap-data-v2.txt"
	fi
	# echo "$1"
}

# Input parameter
subcmd1="${1:-help}"

_polap_cmd="${_polap_script_bin_dir}/polap.sh"
if [[ -d "src" ]]; then
	_brg_default_target_dir="$HOME/all/manuscript/polap-v0.4/"
else
	_brg_default_target_dir="man/"
	if [[ ! -d "man" ]]; then
		mkdir man
	fi
fi

# _polap_version="$(${_polap_cmd} --version | awk '{print $2}')"
if [ -z "${_polap_version+x}" ]; then
	_polap_version="0.4.3.7.5"
fi
_media1_dir="/media/h1/sra"
_media2_dir="/media/h2/sra"
_media_dir="/media/h2/sra"

help_message=$(
	cat <<HEREDOC
Polap version: ${_polap_version}
Usage: $0 <subcommand> [species_folder] [[option] ...]

Polap data analysis of subsampling-based plastid genome assembly

List of subcommands:
  test

More help for subcommands:
$(basename $0) help <subcommand>
HEREDOC
)

help_message_test=$(
	cat <<HEREDOC

  Test.
HEREDOC
)

_arg1=${1:-arg1}
# Check if the species folder is provided
if [[ "${_arg1}" == "arg1" ]]; then
	echo "${help_message}"
	exit 1
fi

_arg2=${2:-arg2}
if [[ "${_arg2}" != "arg2" ]]; then
	_arg2="${2%/}"
fi

_arg3=${3:-arg3}
_arg4=${4:-arg4}
_arg5=${5:-arg5}
_arg6=${6:-arg6}
_arg7=${7:-arg7}
_arg8=${8:-arg8}
_arg9=${9:-arg9}
_arg10=${10:-arg10}

################################################################################
# Part of genus_species
#
test_genus_species() {
	local _brg_outdir="${1:-all}"
	local _brg_inum="${2:-2}"
	local _brg_d_index="${3:-infer-1}"
	local _brg_stage="${4:-1}"
	local _brg_table="${5:-1}"
	local _brg_t_dir="${6:-"${_brg_default_target_dir}"}"

	echo "outdir: ${_brg_outdir}"
}

if [[ "${subcmd1}" == "help" ]]; then
	if [[ "${_arg2}" == "arg2" ]]; then
		subcmd1="help"
	else
		subcmd1="${_arg2}"
		_arg2="arg2"
	fi
fi

# Main case statement
case "$subcmd1" in
'help')
	echo "${help_message}"
	;;
test)
	if [[ "${_arg2}" == arg2 ]]; then
		echo "Help: ${subcmd1} test"
		echo "  polap-data-v2.sh ${subcmd1} test"
		echo "${help_message_test}"
		exit 0
	fi
	[[ "${_arg3}" == arg3 ]] && _arg3=""
	[[ "${_arg4}" == arg4 ]] && _arg4=""
	[[ "${_arg5}" == arg5 ]] && _arg5=""
	[[ "${_arg6}" == arg6 ]] && _arg6=""
	${subcmd1}_genus_species "${_arg2}" "${_arg3}" "${_arg4}" "${_arg5}" "${_arg6}"
	;;
*)
	echo "Usage: $0 <subcommand> [species_folder]"
	echo "${help_message}"
	echo "subcommand '$subcmd1' is not recognized."
	exit 1
	;;
esac
