#!/bin/bash -u
#
# 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
#------------------------------------------------

fatal_msg "run_alignbest.sh : OBSOLETE SCRIPT - dont run!!!"



# get args
[ $# -eq 1 ] || fatal_msg "usage: $0 <A,B,nthreads>"

# check find_best_alignment tool
cmd="${DIR_PIPELINE_BIN}/find_best_alignment.py"
check_file_exe "$0" "$cmd" || exit 255

input="$1"
inparr=( $(tokenize ";" $input) )

nok=0
nerr=0

for args in ${inparr[@]}; do
    argsarr=( $(tokenize "," $args) )
    [ ${#argsarr[@]} -eq 3 ] || fatal_msg "run_alignment.sh: invalid input ($input), expected comma separated list <idA0,idB0,N;idA1,idB2,N;...> but got <$input>";

    # label pair A,B
    A="${argsarr[0]}"
    B="${argsarr[1]}"
    nthreads=${argsarr[2]}

    echo
    print_separator
    info_msg "Starting alignbest job <$A,$B> with N(threads)=$nthreads"
    print_separator
    echo

    # get I/O files
    inpfile="$(get_pwc_file $A $B $EXT_ALIGN)"
    outfile="$(get_pwc_file $A $B $EXT_ALIGNBEST)"

    # check files
    check_inpout_files "$0" "$inpfile" "$outfile" || exit 255

    # run the tool
    tmp_make tmpfile best_alignment
    startdate=$(date)
    tA=$(time_msec)
    "$cmd" -f "$inpfile" -e -1.0 > $tmpfile
    ec=$?
    dT=$(delta_time $tA)
    [ $ec -eq 0 ] && nok=$((nok+1))
    [ $ec -eq 0 ] || nerr=$((nerr+1))

    # add header and footer
    add_header_footer $0 $tmpfile "$outfile" "$startdate" $dT "$cmd -f \"$inpfile\" -e -1.0"
    if [ $? -ne 0 ]; then
        error_msg "failed adding header/footer"
        exit 255
    fi

    chmod 644 "$outfile"

    echo
    print_separator
    info_msg "Done alignbest job <$A,$B>, exit code = $ec"
    print_separator
done
echo

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

