#!/usr/bin/env bash

# URL for OrthoDB API - user settable
URL_ORTHODB_API_BASE=${URL_ORTHODB_API_BASE:="https://data.orthodb.org"}
URL_ORTHODB_API_VERSION=${URL_ORTHODB_API_VERSION:="current"}

#-----------------------------------------------
# Deduced by the above, see set_orthodb_api()
# Note that they may have been already set before!

# Full APU url
URL_ORTHODB_API=${URL_ORTHODB_API:=}

# OrthoDB version string as retrieved from API
#API_ORTHODB_VERSION=${API_ORTHODB_VERSION:=}

# same as previous but with '.' replaced by '-'
#API_ORTHODB_VERSION_NODOT=${API_ORTHODB_VERSION_NODOT:=}

# OrthoDB version string used with mapping (e.g odb11v0 for v11)
MAP_ORTHODB_VERSION=${MAP_ORTHODB_VERSION:=}

_CLEANED_VER=""
CLEANED_VER_API=""
clean_ver_api() {
    local ALLOWED_ORTHODB_API_VERSIONS="current v11 v12"
    local ver=$1
    if [[ -z $ver ]]; then
        #echo "TST: empty input - set to current" 1>&2
        CLEANED_VER_API="current"
        return 0
    fi
    #echo "TST: input == <$ver>" 1>&2
    ver=${ver,,}
    # change v11.x to v11-x
    ver="${ver//./-}"

    # change v11-0 to v11
    ver="${ver//-0/}"

    # for v12, there will be no separate API for v12.0, v12.1, ...
    [[ ${ver:0:3} == "v12" ]] && ver="v12"
    
    _CLEANED_VER="$ver"
    CLEANED_VER_API="$ver"
    #ver="${ver%%.*}"
    local v=
    local vfound=
    local IFSsave="$IFS"
    IFS=" "
    #echo "TST: loop over <${ALLOWED_ORTHODB_API_VERSIONS}>" 1>&2
    for v in $ALLOWED_ORTHODB_API_VERSIONS; do
        #echo "TST: <$v> == <$ver>" 1>&2
        if [[ "$v" == "$ver" ]]; then
            vfound="$ver"
            #echo "TST: MATCH <$v> == <$ver>" 1>&2
            break
        fi
    done
    IFS="$IFSsave"
    if [[ -z $vfound ]]; then
        # unknown version - fallback to current
        CLEANED_VER_API="current"
        return 1
    fi
    
    return 0
}

# Set OrthoDB API URL's.
# In addition retrieve API version string.
set_orthodb_api() {
    #echo "DBG:set_orthodb_api is called" 1>&2
    # mapping odb version string - part of filenames to be downloaded
    MAP_ORTHODB_VERSION=""

    # make sure API is non-empty
    URL_ORTHODB_API_VERSION=${URL_ORTHODB_API_VERSION:='current'}
    [[ -n $URL_ORTHODB_API_VERSION ]] || URL_ORTHODB_API_VERSION='current'
    
    # clean API
    local inpurl="$URL_ORTHODB_API_VERSION"
    clean_ver_api "$URL_ORTHODB_API_VERSION"
    if [[ $? -ne 0 ]]; then
        echo "WARNING: unknown API version given ($inpurl) - fall back to '$CLEANED_VER_API' - '$_CLEANED_VER'" 1>&2
    fi
    URL_ORTHODB_API_VERSION=$CLEANED_VER_API
    
    # set full API url
    URL_ORTHODB_API=$URL_ORTHODB_API_BASE/$URL_ORTHODB_API_VERSION

    # get API orthodb version string
    local url="$URL_ORTHODB_API/orthodb_release_id"
    local odbver=$(curl -H "Accept: text/plain" --silent $url 2>/dev/null)
    # it returns plain text OrthoDB version
    #API_ORTHODB_VERSION="$odbver"
    #API_ORTHODB_VERSION_NODOT="${odbver//./-}"
    URL_ORTHODB_API_VERSION_RAW="$odbver"
    URL_ORTHODB_API_VERSION_RAW_NODOT="${odbver//./-}"
    if [[ "$odbver" == "v11.0" ]]; then
        MAP_ORTHODB_VERSION="odb11v0"
        URL_ORTHODB_API_VERSION="v11"
    elif [[ "$odbver" == "v12.0" ]]; then
        MAP_ORTHODB_VERSION="odb12v0"
        URL_ORTHODB_API_VERSION="v12"
    elif [[ "$odbver" == "v12.1" ]]; then
        MAP_ORTHODB_VERSION="odb12v1"
        URL_ORTHODB_API_VERSION="v12"
    else
        if [[ -n $odbver ]]; then
            {
                echo "URL for OrthoDB API returns an invalid version id"
                echo "URL: $url"
                echo "Version id -> <$odbver>"
            } 1>&2
            return 255
        else
            {
                echo "Failed accessing OrthoDB API."
                echo "URL: $url"
                echo "Mapping against OrthoDB data may fail if data is not already downloaded."
            } 1>&2
        fi
    fi
    # reset API url
    URL_ORTHODB_API=$URL_ORTHODB_API_BASE/$URL_ORTHODB_API_VERSION
    #echo "API version     : $API_ORTHODB_VERSION"
    #echo "API url         : $URL_ORTHODB_API"
    return 0
}

export -f clean_ver_api
export -f set_orthodb_api
