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

# get args
#
# Make a full check of fasta file
# It returns (exit code):
#   0 : file is OK
#   1 : check_fasta.py script failed
#   2 : wrong type
#
if [ $# -ne 2 ]; then
    echoerr "usage: $0 <inpfile> <output>"
    echoerr "Got arguments <$*>"
    exit 255
fi

fasta="$1"
output="$2"

# check existence of fasta file
check_file  "$0" "$fasta" || fatal_msg "fasta file not found $fasta"

tmpfile=""
tmp_make tmpfile checkfasta

startdate=$(date)
tA=$(time_msec)

# check fasta file format
$PYTHON3 "${DIR_ORTHOLOGER_BIN}"/check_fasta.py --spsep ":" --expected "PROT" --check --clean -f "$fasta" -o "$tmpfile"
ec=$?
[[ $ec -eq 0 ]] || { error_msg "failed check_fasta for fasta $fasta"; exit $ec; }

dT=$(delta_time $tA)

add_header_footer $0 $tmpfile "$output" "$startdate" "$dT" "/check_fasta.py --spsep \":\" --expected \"PROT\" --check --clean -f \"$fasta\" -o \"$tmpfile\""
if [[ $? -ne 0 ]]; then
    tmp_mark_keep "$tmpfile"
    error_msg "failed add_header_footer(), output at $(uname -n):$tmpfile"
    exit 255
fi
chmod 644 "$output"

exit 0
