#!/bin/bash
source ../test_common.sh
set_test_name "${BASH_SOURCE[0]}"

# set root work dir
export ODBMAPPER_WORK=$(pwd)
###export URL_ORTHODB_API_VERSION="current"

IGNORE_CHECK_ENV=1
testfun() {
    local odbmapper="$DIR_ORTHOLOGER/bin/ODB-mapper.sh"
    [[ -f "$odbmapper" ]] || { echo "error: missing odbmapper command $odbmapper"; return 255; }
    
    # get info
    echo ">>> Get project directory"
    $odbmapper CONFIG project
    USE_DIR_PROJECT=$($odbmapper CONFIG project 2>/dev/null)
    [[ -d $USE_DIR_PROJECT ]] || { echo "error: failed getting project directory <$USE_DIR_PROJECT>"; return 255; }
    
    append_slash USE_DIR_PROJECT
    echo ">>> Got project directory <$USE_DIR_PROJECT>"

    echo ">>> Get/create project refs dir"
    local dirrefs="${USE_DIR_PROJECT}refs"
    [[ -d "$dirrefs" ]] || mkdir "$dirrefs" || return 255

    echo ">>> Get API URL"
    USE_API_URL=$($odbmapper CONFIG apiurl)
    echo ">>> Got API URL $USE_API_URL"

    echo ">>> Setup project dir"
    $odbmapper SETUP || return 255
    
    # 3. download a sample fasta
    echo ">>> Download example.fs (API URL = $USE_API_URL)"
    download_fasta example.fs || return $EC_FAIL

    local run_label="test_simple"
    local node="1489911"
    
    # 5. map fasta file against $node
    export DIR_PROJECT="$USE_DIR_PROJECT"
    echo ">>> Run mapper for version <$URL_ORTHODB_API_VERSION> at $odbmapper"
    $odbmapper MAP "$run_label" "${dirrefs}/example.fs" "$node" subsample="no" || {
        echo "error: failed running orthologer"
        return $EC_FAIL
    }

    local fclus=$($odbmapper RESULT "$run_label" what=cls)
    
    # results
    echo ">>> Get results for label $run_label"
    check_map_result_files "$run_label" || return $EC_FAIL

    echo ">>> Compare with ref"
    local fref="$DIR_PROJECT/../../../proj_map/Cluster/test_simple.og"
    # compare with reference
    cmp_with_ref "$fclus" "file:$fref"
    return $?
}
run_test
exit $?

