#!/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 postproc_cmpref.sh : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

# define nr of targets - set in postproc_conf
export POSTPROC_NTARGETS=0
export POSTPROC_TRGS=("")

postproc_help() {
    echo -e "" \
	     "${str_help_input}\n" \
         "Defined by two variables to be supplied in the project common.sh\n" \
	     " * POSTPROC_CMPREF_CLUSTER (default Cluster/<todo>.${EXT_CLUSTER_MAP})\n" \
	     " * POSTPROC_CMPREF_REFERENCE (required)\n" \
	     "${str_help_output}\n" \
	     " * Cluster/<todo>.cmpref\n" \
	     "${str_help_desc}\n" \
	     "This post process compares a reference to the result using cmpclus.\n" \
         "By default it requires POSTPROC:ogmap to have been run before.\n" \
	     ""
}

postproc_conf() {
    reset_joblists
    JOBSUBLBL=( $JOBLOGNOSPEC )

    # get nr of targets - used by step_start in step_postproc() function
    POSTPROC_NTARGETS=1
    POSTPROC_TRGS=("$DIR_PROJECT_CLUSTER/${PL_TODO_NAME}.cmpref")
}

postproc_cleanup() {
    local f=""
    for f in ${POSTPROC_TRGS[@]}; do
        rmcmd "$f"
    done
}

postproc() {
    # check if dry run
    [[ $DRY_RUN -eq 0 ]] || return 0

    # error code
    local ec=0

    # source cluster file
    POSTPROC_CMPREF_CLUSTER=${POSTPROC_CMPREF_CLUSTER:=$DIR_PROJECT_CLUSTER/${PL_TODO_NAME}.${EXT_CLUSTER_MAP}}
    [[ -f "$POSTPROC_CMPREF_CLUSTER" ]] || {
        step_error_msg "missing or not readable cluster input file POSTPROC_CMPREF_CLUSTER=$POSTPROC_CMPREF_CLUSTER"
        return 255
    }
    # get reference file if it exists
    POSTPROC_CMPREF_REFERENCE=${POSTPROC_CMPREF_REFERENCE:=""}
    if [[ -z "$POSTPROC_CMPREF_REFERENCE" ]]; then
        step_error_msg "requires a reference file to be set in POSTPROC_CMPREF_REFERENCE"
        step_error_msg "add the following line in the project common.sh"
        step_error_msg "  export POSTPROC_CMPREF_REFERENCE=<reference file name>"
        return 255
    fi
       
    [[ -f "$POSTPROC_CMPREF_REFERENCE" ]] || {
        step_error_msg "missing or not readable reference input file POSTPROC_CMPREF_REFERENCE=$POSTPROC_CMPREF_REFERENCE"
        return 255
    }
    
    # target file
    local trgfile=${POSTPROC_TRGS[0]}

    SRCFILES=( "$POSTPROC_CMPREF_CLUSTER" )
    TRGFILES=( "$trgfile" )
    JOBARGS=( "$POSTPROC_CMPREF_CLUSTER,$POSTPROC_CMPREF_REFERENCE","$trgfile" )
    JOBLABELS=( "cmpref-${PL_TODO_NAME}" )

    # check if target should be remade
    remake_target "$trgfile" "${POSTPROC_CMPREF_CLUSTER}:${POSTPROC_CMPREF_REFERENCE}" || return 0

    step_info_msg "compare reference ($(trim_name $POSTPROC_CMPREF_REFERENCE)) with result ($(trim_name $POSTPROC_CMPREF_CLUSTER))"
    # wait for scheduler
    check_scheduler_status 1

    # submit jobs
    run_cmd "${DIR_ORTHOLOGER_BIN}/run_cmpref.sh"
    ec=$?
    if [[ $ec -ne 0 ]]; then
        step_error_msg "failed to submit job with error code $ec"
        force_remake_missing
        return 1
    fi

    if is_runmode_batch; then
        sj_wait || { step_error_msg "aborting step since job id wait failed"; return 255; }
    fi

    # check error logs
#    if [[ ${#JOBLABELS_CHECK[@]} -gt 0 ]]; then
    if [[ ${#JOBLABELS[@]} -gt 0 ]]; then
        check_error $ec "failed cmpref postproc"
        [[ $? -eq 0 ]] || return 1
    fi

    reset_joblists

    return 0
}
