#!/usr/bin/env bash
[[ $# -ge 2 ]] || {
    echo -e "usage: $0 <pipeline directory> <todo label> [<tar file basename>]\n" \
         "\n" \
         "<tar file basename> - default   -> /tmp/<todo label>\n" \
         "                    - is dir    -> <dir>/<todo label>\n" \
         "                    - otherwise -> <given argument>\n" \
         "\n" \
         "It will collect all files for a given label required for the mapping procedure."
    exit 0
}

DIR_PROJECT=$1
LABEL=$2
TFARG=""
TPACKAGE=""
[[ $# -eq 3 ]] && TFARG=$3
[[ -d $DIR_PROJECT ]] || { echo "error: pipeline directory is not readable at $DIR_PROJECT"; exit 255; }

# get todo file
FTODO="$DIR_PROJECT/todo/${LABEL}.todo"
[[ -f $FTODO ]] || { echo "error: todo file is not readable at $FTODO"; exit 255; }

# get tree file
FTREE="$DIR_PROJECT/todo/${LABEL}.${EXT_TREE}"
[[ -f $FTREE ]] || FTREE=""

# if FTREE get BRHCLUS directory
export DIR_BRHCLUS=""
if [[ -n $FTREE ]]; then
    eval $(grep "^DIR_BRHCLUS=" $DIR_PROJECT/orthologer_conf.sh)
    [[ -f $DIR_BRHCLUS/brhclus ]] || { echo "error: tree is given but DIR_BRHCLUS is not valid = $DIR_BRHCLUS"; exit 255; }
fi

# construct package file name
if [[ -z $TFARG ]]; then
    TPACKAGE=/tmp/${LABEL}.tgz
elif [[ -d $TFARG ]]; then
    TPACKAGE=${TFARG}/${LABEL}.tgz
else
    TPACKAGE=${TFARG}
fi

echo "Will create tar file at $TPACKAGE"

# start collecting files!
#
# The mapping requires from pipeline data the following:
# > Rawdata
#   - *.fs.maptxt             mapping between of fasta labels
# > Sequences
#   - *.fs_selected           final processed fasta
#   - *.fs_selected.mm*|dmnd  DB files (mmseqs or diamond)
#   - *.fs_selected.seqlen    sequence lengths
#   - *.fs_selected.stats     fasta stats
#   - *.fs_selected.clstr     cdhit clusters
# > PWC
#   - PWC/A/A_A.align         diagonal of alignment matrix
# > Cluster
#   - Cluster/*.og            final cluster results, internal id's
#

ffound=$(mktemp "/tmp/${LABEL}_found_XXXXXX")
fmiss=$(mktemp "/tmp/${LABEL}_missing_XXXXXX")

# get all labels
labels=($(grep -v '^#' "$FTODO"))
check_file() {
    local f=$1
    if [[ -f $f ]]; then
        echo $f
    else
        echo $f 1>&2
    fi
}

# get list of all files
has_mmseqs_db=0
has_diamond_db=0
{
    # all gene id mapping files
    for t in ${labels[@]}; do
        check_file $DIR_PROJECT/Rawdata/${t}.fs.maptxt
    done

    # required files in Sequences
    for t in ${labels[@]}; do
        bname=$DIR_PROJECT/Sequences/${t}.fs_selected
        check_file $bname
        check_file ${bname}.stats
        check_file ${bname}.seqlen
        check_file ${bname}.clstr
        
        # optional mmseqs DB files
        if [[ -f ${bname}.mm ]]; then
            has_mmseqs_db=1
            for x in ${bname}.mm*; do
                check_file $x
            done
        fi
        # optional diamond DB files
        if [[ -f ${bname}.dmnd ]]; then
            has_diamond_db=1
            check_file ${bname}.dmnd
        fi
    done

    # Pairwise alignments A vs A
    for t in ${labels[@]}; do
        check_file $DIR_PROJECT/PWC/$t/${t}_${t}.align
    done

    # todo and cluster files
    nodes=($LABEL)
    if [[ -n $FTREE ]]; then
        # use the given tree
        nodes=($(python3 $DIR_ORTHOLOGER/bin/get_orthodb_tree.py -n -i $FTREE -N)) || {
            echo "error: failed retrieving nodes from $FTREE"
            exit 255
        }
        check_file $FTREE
    fi
    for n in ${nodes[@]}; do
        check_file $DIR_PROJECT/todo/${n}.todo
        check_file $DIR_PROJECT/Cluster/${n}.og
    done
} 1>$ffound 2>$fmiss      

# check for DB files
has_db=$(( has_mmseqs_db + has_diamond_db ))
(( has_db == 0 )) && { echo "error: no DB files found"; exit 255; }

# print missing files
N=$(cat $fmiss | wc -l)
(( N == 0 )) || { echo "error: certain files are missing ->"; cat $fmiss; exit 255; }

# all is good -> create tar package
echo "create package file at $TPACKAGE"
tar -czf $TPACKAGE --files-from=$ffound
[[ $? -eq 0 ]] || { echo "error: tar failed!" ; exit 255; }
rm -vf $ffound $fmiss >& /dev/null
