#!/usr/bin/env bash
# echo ${BASH_SOURCE[0]}
# HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

req="$1"

set -au

bdir=""

#[[ -x ~/build/bin/mars.bin ]] && bdir=~/build/
#[[ -x ~/build/mars-client/bin/mars.bin ]] && bdir=~/build/mars-client/
#[[ -x ~/build/mars-client-bundle/bin/mars.bin ]] && bdir=~/build/mars-client-bundle/

#[[ -z "$bdir" ]] && echo "couldn't find mars.bin executable" && exit 1

#export MARS_LANGUAGE_FILE=~/git/mars-client-configs/etc/mars.def
#export MARS_TEST_FILE=~/git/mars-client-configs/chk/mars.chk
#export MARS_EMS_FILE=/tmp/mars.ems
#export MARS_AUTH_FILE=/dev/null
#export MARS_DISSEMINATION_SCHEDULE_FILE=/tmp/mars.schedule
export MARS_CONFIG=/tmp/mars.config
export FDB_HOME=/tmp/fdblocal/

export MARS_ROOT=~/build/mars-client
export MARS_HOME=~/git/mars-client-configs

export MARS_LANGUAGE_FILE=$MARS_HOME/etc/mars.def
export MARS_TEST_FILE=$MARS_HOME/chk/mars.chk
#export MARS_CONFIG=/root/mars.cfg

# mkdir -p $FDB_HOME/etc/fdb
# cp $bdir/etc/fdb/schema $FDB_HOME/etc/fdb/

# cat <<@ > $FDB_HOME/etc/fdb/config.yaml
# ---
# type: dist
# lanes:
# - type: remote
#   host: localhost
#   port: 7655
# - type: remote
#   host: localhost
#   port: 7656
# @

cat <<@ > $MARS_CONFIG
database,
    class   = dhsbase,
    name    = localhost/dhslocal/local/marsod,
    retry   = 32767,
    timeout = 0,
    # rules   = '~/chk/marsod.chk',
    host    = 127.0.0.1,
    port    = 9000,
    obs     = 1

database,
    name    = file,
    class   = flatfilebase,
    visit   = false

database,
    class = fdb5base,
    name  = fdblocal/fdblocalhost/fdb,
    visit = false,
    timeout = 0,
    retry   = 32767,
    home = "$FDB_HOME",
    # rules = '~/chk/fdbmain.chk',
    host  = fdblocalhost # dummy

database,
    name=webmars,
    class=apibase,
    url='https://api.ecmwf.int/v1',
    visit=false

database,
    name=webmars2,
    class=apibase,
    visit=false

database,
    class   = dhsbase,
    name    = passive,
    retry   = 32767,
    timeout = 0,
    # rules   = '~/chk/marsod.chk',
    host    = localhost,
    port    = 9000,
    obs     = 1,
    passive_mode = 1,
    passive_host = localhost,
    passive_port = 9707,
    callback_proxy_host = localhost,
    visit   = false

database,
    class   = dhsbase,
    name    = passive-mvr0,
    retry   = 32767,
    timeout = 0,
    # rules   = '~/chk/marsod.chk',
    host    = marscore,
    port    = 9000,
    obs     = 1,
    passive_mode = 1,
    passive_host = marsmvr-0000/marsmvr-0001/marsmvr-0002,
    passive_port = 9707,
    # callback_proxy_host = marsmvr-0000,
    use_proxy=socks,
    visit   = false

@

export MARS_PROXIES=$MARS_HOME/etc/proxies.def

cat<<@ > $MARS_PROXIES

proxy,
    name = socks,
    type = socks,
    host = localhost,
    port = 1080,
    username = user,
    password = password
    # authorization = Basic foobar
@

# cat $MARS_PROXIES


# cat<<@ > $MARS_EMS_FILE
# $LOGNAME all
# @

# cat<<@ > $MARS_DISSEMINATION_SCHEDULE_FILE
# allow, request=(empty)
# @

echo "MARS client is $MARS_ROOT/bin/mars.bin"

# export ECKIT_DEBUG=1
# export METKIT_DEBUG=1
# export FDB_DEBUG=1
# export MARS_DEBUG=1

MARS_DEBUGGER=${MARS_DEBUGGER:=}

if [[ $req != "" ]]
then
  export MARS_REQUEST="$req"
  $MARS_DEBUGGER $MARS_ROOT/bin/mars.bin
else
  $MARS_DEBUGGER $MARS_ROOT/bin/mars.bin || exit 1
fi
