#!/usr/bin/env bash
#
# orthologer - Pipeline for deducing orthology from fasta files.
# Copyright (C) 2005-2023 E. Zdobnov lab: F. Tegenfeldt
# <fredrik.tegenfeldt@unige.ch> based on code by E. Kriventseva
#
# This file is part of orthologer.
#
# orthologer 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.
#
# orthologer 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 orthologer.  If not, see <http://www.gnu.org/licenses/>.
#
#--- check if environment is ok -----------------
[[ "${LOADED_COMMON_PIPELINE_SH+1}" == "" ]] && echo "ERROR $0 : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

# check args
[[ $# -eq 1 ]] || fatal_msg "usage: $0 <label>"

# script
cmd="${DIR_ORTHOLOGER_BIN}/find_inparsel.py"
check_file_exe "$0" "$cmd" || exit 255

# get args
input="$1"
inparr=( $(tokenize ";" $input) )


#-----------------------------------
# fix for run using splits
# check splits file
tmpbase=""
tmp_base tmpbase
splitfile=""   ####${tmpbase}/${PL_TODO_NAME}.splits"
splitopt=""

if [[ -f $splitfile ]]; then
    splitopt="$splitfile"
    info_msg "split file found at $splitfile"
else
    info_msg "no split file ($splitfile) found - do not split genes"
    splitfile=""
fi
#-----------------------------------


nok=0
nerr=0
ec=0


# loop over set of arguments separated by ';'
for args in ${inparr[@]}; do
    echo
    print_separator
    info_msg "Starting MAKEINPARSEL job <$args>"
    print_separator
    echo

    # input
    fasta="$DIR_PROJECT_SEQUENCES/${args}.$EXT_FASTA_SELECTED"
    fcls="$DIR_PROJECT_SEQUENCES/${args}.$EXT_FASTA_SELCLSTR"
    [[ -f $fcls ]] || { error_msg "find_inparsel.sh: missing file $(trim_name $fcls)"; continue; }

    # check for end tag
    check_footer "$fcls" || fatal_msg "run_makeinparsel : missing end tag (#END) in <$fcls>"
    
    # output
    output=$(set_extension "$fasta" $EXT_INPAR_SELECTED)

    if [[ "$SELECT_LABEL" == "NONE" ]]; then
        rm -f "$output$cmpext" "$output"
        echo "# No inparlogs from select step present since NONE was used" > $output
        nok=$((nok+1))
    else
        # run cmd
        tmp_make tmpfile "inparsel"
        startdate=$(date)
        tA=$(time_msec)
        $PYTHON3 "$cmd" "$fcls" "$splitfile" > $tmpfile
        ec=$?
#
        dT=$(delta_time $tA)

        if [[ $ec -ne 0 ]]; then
            nerr=$((nerr+1))
        else
            nok=$((nok+1))
            # add header and footer
            add_header_footer "$0" "$tmpfile" "$output" "$startdate" $dT "$cmd $(basename $fcls)"
            [[ $? -eq 0 ]] || fatal_msg "$cmdline : unable to add header to file $tmpfile"
        fi
    fi
    
    echo
    print_separator
    info_msg "Done MAKEINPARSEL job <$args>"
    print_separator

done
echo
[[ $nerr -eq 0 ]] || { info_msg "Completed $nok succesfully and $nerr with errors"; exit 255; }

