#!/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 result files for a given label."
    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*      mmseqs DB files (or corresponding blast DB files)
#   - *.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
#   - Cluster/*.og_map       maps cluster id to OrthoDB name
#

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
}

{
    for t in ${labels[@]}; do
        check_file $DIR_PROJECT/Rawdata/${t}.fs.maptxt
    done
    nodes=($LABEL)
    if [[ -n $FTREE ]]; then
        # use the given tree
        nodes=($(python3 $DIR_PROJECT/bin/generate_runs.py -t $FTREE -N -k))
    fi
    for n in ${nodes[@]}; do
        check_file $DIR_PROJECT/todo/${n}.todo
        check_file $DIR_PROJECT/Cluster/${n}.og
        check_file $DIR_PROJECT/Cluster/${n}.og_map
        check_file $DIR_PROJECT/Cluster/${n}.evolrate
    done
    
} 1>$ffound 2>$fmiss

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

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

