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

echo
echo "RUNNING with NOCHECKS"
echo 
# make sure tmp file cleaning works
tmp_setup_trap

[[ $# -eq 1 ]] || fatal_msg "Usage: $0 <keyA>,<keyB>,<nthreads>"


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

nok=0
nerr=0

move_files="echo 'move files'"
tmp_make_dir tmpdir "makebrh"        || exit 255

for args in ${inparr[@]}; do
    argsarr=( $(tokenize "," $args) )
    [[ ${#argsarr[@]} -eq 3 ]] || fatal_msg "run_makeinpar.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]}

    info_msg "Starting makeinpar job <$A,$B> with N(threads)=$nthreads"
    ###tmp_make tmpout  makeinpar || exit 255
    # alt output in tmpdir/<keyA>/A_B.align
    #mkdir -p $tmpdir/$A || {
    #    error_msg "failed creating tmp dir at $tmpdir/$A"
    #    df -h /tmp | tail -n 1 1>&2
    #    fatal_msg "aborting"
    #}
    #tmpout="$tmpdir/$A/${A}_${B}.${EXT_INPAR}"
    tmpout="$tmpdir/${A}_${B}.${EXT_INPAR}"
    
    # get input pair file names
    fAB="${DIR_PROJECT_PWC}/$A/${A}_${B}.${EXT_BRH}"
    fAA="${DIR_PROJECT_PWC}/$A/${A}_${A}.${EXT_ALIGN}"
    fBB="${DIR_PROJECT_PWC}/$B/${B}_${B}.${EXT_ALIGN}"

    # get inparalog file name (output)
    output="${DIR_PROJECT_PWC}/$A/${A}_${B}.${EXT_INPAR}"

    # options
    opts="--AB $fAB --AA $fAA --BB $fBB -o $MIN_OVERLAP -c $MIN_COVERAGE"

    # run it
    startdate=$(date)
    tA=$(time_msec)
    ec=0
    {
        print_header "$EXE_GETINPAR $opts --output /dev/stdout"  "$startdate"
        eval "$EXE_GETINPAR" "$opts"
        ec=$?
        [[ $ec -eq 0 ]] || fatal_msg "$0 : failed with error = $ec"
        dT=$(delta_time $tA)
        print_footer "$dT"
    } > $tmpout

    if [[ $? -ne 0 ]]; then
        nerr=$((nerr+1))
        continue
    fi
    nok=$((nok+1))
    chmod 644 $tmpout
    move_files="$move_files && mv -f $tmpout $output"
    
    info_msg "Done makeinpar job <$A,$B>, exit code = $ec"
done
# move all files
eval $move_files

#info_msg "rsynching results..."
#cd $tmpdir
#rsync -ah ./ "$DIR_PROJECT_PWC"

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

