#!/bin/sh
# G.Landais (CDS) : 25-oct-2019
# Query ReadMe index 
# 
# switch query to bibcat (qcat.old) or to vsearch (elasticsearch)
# vsearch query can use remote search

if [ "$VSEARCH_DISABLE" = "yes" ]; then
  return
fi

HOST=127.0.0.1
DOCKER_HOSTS="172.19.0.5 172.17.0.1 172.18.0.1"
PORT=9200
#PYTHON_ROOT=~cds/pythonlib
PYTHON_ROOT=~cds/httpd/Pages/vizier.cat/vsearch/
PYTHONPATH=$PYTHONPATH:$PYTHON_ROOT
export PYTHONPATH

TIMEOUT=5

###########################################################
# test if docker environment                              #
###########################################################
is_docker=0
if [ "$DOCKER" = "yes" ]; then
    is_docker=1
    for host in $DOCKER_HOSTS; do
        nc -w $TIMEOUT -z $host $PORT >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            HOST=$host
            break
        fi
    done
    #PYTHON_ROOT=/srv/pythonlib
    PYTHON_ROOT=/srv/httpd/Pages/vizier.cat/vsearch
fi

###########################################################
# TEST if elasticsearch is runing                         #
# if not runing: useremote qcat if VSEARCH_HOST is set    #
#                else use bibcat                          #
###########################################################
nc -z $HOST $PORT >/dev/null 2>&1
if [ $? -ne 0 ]; then
    if [ -n "$VSEARCH_HOST" ]; then
        if [ "$VSEARCH_HOST" != $HOST ] ; then
            arg=`echo $*|sed 's/[()]/ /g'|sed 's/&/\\\\&/g'`
            exec aclient $VSEARCH_HOST 1660 qcat  $arg
        fi
    fi 
    echo "#execute bibcat"
    exec qcat.old $*
fi

# get options
PARAM=$*

#test if parameter are correct!
TEST=`echo $PARAM|awk '/.*;.*/{print "ERROR"}/.*[()]$/{print "ERROR"}'`
if [ "$TEST" = "ERROR" ]; then
    echo "#error qcat: char not authorized"
    exit 0
fi

###########################################################
# if option -s is NOT SET then execute python qcat        #
###########################################################
listonly=`echo $PARAM|sed "s/^.*-s .*/XXX/g"`
if [ "$listonly" != "XXX" ]; then
    #exec python3 $PYTHON_ROOT/vizier.ingestion/vsearch/qcat/qcat.py --server=$HOST $* #2>/dev/null
    exec python3 $PYTHON_ROOT/bin/qcat.py --server=$HOST $* #2>/dev/null
fi

###########################################################
#OPTION -s IS SET => direct access to vsearch             #
#(used by vizier)                                         #
# - remove all other options                              #
# - direct http access for efficiency                     #
###########################################################

# remove all options
PARAM=`echo $PARAM|sed "s/ *-[-a-z][^ 0-9]*//g"`
if [ "$PARAM" = "" ]; then
    PARAM="*"
fi

# special case for catalogues and '/' (ex DOI)
CATPARAM=`echo $PARAM|gawk '/.*[IBVJX]/{s="";for (i=1;i<=NF;i++){s=s" "gensub(/^([IBXVJ][IVX]*)$/,"\\\\1/","g",$i)} print s; }'`
if [ "$CATPARAM" != "" ]; then
    #exec python3 $PYTHON_ROOT/vizier.ingestion/vsearch/qcat/qcat.py --server=$HOST -s "$CATPARAM" #2>/dev/null
    exec python3 $PYTHON_ROOT/bin/qcat.py --server=$HOST -s "$CATPARAM" #2>/dev/null
fi
TEST=`echo $PARAM|gawk '/.*[/&><].*/{print "XXX";}'`
if [ "$TEST" != "" ]; then
    #exec python3 $PYTHON_ROOT/vizier.ingestion/vsearch/qcat/qcat.py --server=$HOST -s "$PARAM" #2>/dev/null
    exec python3 $PYTHON_ROOT/bin/qcat.py --server=$HOST -s "$PARAM" #2>/dev/null
fi
#echo "optimized way"

BIBPARAM=`echo $PARAM|awk '/^.*[0-9]{4}[a-zA-Z&][a-zA-Z&+]*[.].*$/{ s="";for (i=1;i<=NF;i++){s=s""gensub(/^([0-9]{4}[a-zA-Z&][a-zA-Z&+]*[.].*)$/,"bibcode:\\\\1 ","g",$i)} print s;}'`
if [ "$BIBPARAM" != "" ]; then
    PARAM=$BIBPARAM
fi


DEFAULT_SIZE=50000
INDEX_NAME="readme"
DEFAULT_FIELDS='"id","title_cds^2","title_pub","first_author","author","alias^8","keyword_cds^4","object","doi","orcid_pub","bibcode"'
FIELDS_TABLE="$DEFAULT_FIELDS,\"tables.name\",\"tables.desc\""
FIELDS_COL="$FIELDS_TABLE,\"tables.columns.name\",\"tables.columns.desc\""
SOURCE="id"

searchcol=`echo $PARAM|sed "s/^.*-c.*/XXX/g"`
searchtab=`echo $PARAM|sed "s/^.*-t.*/XXX/g"`
searchall=`echo $PARAM|sed "s/^.*--match_all.*/XXX/g"`

PARAM=\"$PARAM\"

if [ "$searchall" = "XXX" ]; then
    JSON="{\"query\":{\"query_string\": {\"lenient\":true,\"query\": "${PARAM}",\"default_operator\":\"AND\",\"fields\":[\"*\"]}},\"_source\": [\"$SOURCE\"],\"size\": $DEFAULT_SIZE}"
else
    if [ "$searchcol" = "XXX" ]; then
        FIELDS="[$FIELDS_COL]"
    else
        if [ "$searchtab" = "XXX" ]; then
            FIELDS="[$FIELDS_TABLE]"
        else
            FIELDS="[$DEFAULT_FIELDS]"
        fi
    fi
    JSON="{\"query\":{\"query_string\": {\"query\": "${PARAM}",\"default_operator\":\"AND\",\"fields\":$FIELDS}},\"_source\": [\"$SOURCE\"],\"size\": $DEFAULT_SIZE}"
fi

#echo "#...qcat (vsearch) executed on `hostname`: $PARAM"  >&2
#echo "exec curl -s -XGET \"http://$HOST:$PORT/$INDEX_NAME/_search\" -H 'Content-Type: application/json' -d '$JSON' ">/tmp/tmp.tmp1
exec curl -s -XGET "http://$HOST:$PORT/$INDEX_NAME/_search" -H 'Content-Type: application/json' -d "$JSON" |sed 's/"id":"\([^"]*\)"/\n====\1\n/g'|sed -n 's/^====\(.*\)/\1/p'
#echo "#...query: http://$HOST/$INDEX_NAME/_search ($searchall): $JSON "
#if [ "$displayall" = "__ALL" ]; then
#    exec curl -s -XGET "http://$HOST:$PORT/$INDEX_NAME/_search" -H 'Content-Type: application/json' -d "$JSON" 
