#!/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
[[ $# -eq 2 ]] || fatal_msg "usage: $0 <fasta file> <output base>"

# check executable
[[ -x "$EXE_FORMATDB" ]] || fatal_msg "missing formatdb executable on <$(uname -a)>"

# input
inpf="$1"
tmpfile=
outbase="$2"

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

# check input file
check_file  "$0" "$inpf" || fatal_msg "formatdb input is not valid [ $inpf ] or [ $0 ] "

# opts for formatdb
optype="-p T"
####[ "$DATA_TYPE" == "NUCL" ] && optype="-p F"

dname=$(dirname  "$outbase")
bname=$(basename "$outbase")

cd $dname

flog=
tmp_make flog "makeblastdb"
# run formatdb
$EXE_FORMATDB -o T $optype -l "$flog" -i "$inpf" -n "$bname"
ec=$?

# check error
[[ $ec -eq 0 ]] || { mv -f "$flog" "${inpf}_formatdb.log"; fatal_msg "failed formatdb (ec=$ec) for input file: $inpf"; }

dT=$(delta_time $tA)
{
    print_header "formatdb -o T $optype -l ${inpf}_formatdb.log -i $inpf -n $bname" "$startdate"
    cat "$flog"
    print_footer "$dT"
} > "${inpf}_formatdb.log"

rm -f "$flog"
cd -
