#!/usr/bin/env bash

set -eux

parse="$<TARGET_FILE:parse-mars-request>"

srcdir=@CMAKE_CURRENT_SOURCE_DIR@
bindir=@CMAKE_CURRENT_BINARY_DIR@
wdir=$bindir/METK-89

export ECCODES_DEFINITION_PATH="@ECCODES_DEFINITION_PATH@"

### cleanup and prepare test

cat > req <<EOF
retrieve,
    class=od,
    date=20050601,
    diagnostic=1,
    expver=0001,
    iteration=0,
    levelist=1,
    levtype=ml,
    param=155.129,
    step=0,
    stream=sens,
    domain=g,
    time=1200,
    type=sg
EOF

cat > req <<EOF
retrieve,
    class=od,
    date=20050601,
    diagnostic=1,
    expver=0001,
    iteration=0,
    levelist=1,
    levtype=ml,
    param=155.129,
    step=0,
    stream=XXXX,
    domain=g,
    time=1200,
    type=sg
EOF

! $parse req

cat > req <<EOF
retrieve,
    class=od,
    date=20050601,
    diagnostic=1,
    expver=0001,
    iteration=0,
    levelist=1,
    levtype=ml,
    param=155.129,
    step=0,
    stream=sens,
    domain=g,
    time=1200,
    type=sg
EOF

$parse req

cat > req.porcelain <<EOF
retrieve,class=od,type=sg,stream=sens,levtype=ml,date=20050601,time=1200,step=0,expver=0001,domain=g,diagnostic=1,iteration=0,levelist=1,param=129155
EOF

cat > req.json <<EOF
{"class":"od","type":"sg","stream":"sens","levtype":"ml","date":"20050601","time":"1200","step":"0","expver":"0001","domain":"g","diagnostic":"1","iteration":"0","levelist":"1","param":"129155"}
EOF

$parse --porcelain req | tee out

cmp out req.porcelain

$parse --porcelain --json req | tee out

cmp out req.json

#rm -rf $bindir/root
#mkdir root

#for f in config.yaml schema sens.data req
#do
#    cp $srcdir/$f $bindir
#done

#export FDB5_CONFIG_FILE=config.yaml

#$fdbwrite sens.data

#$fdblist --porcelain class=od,expver=1

#### test 1

#$fdbread req out.grib

#cat > list <<EOF
#g           ml          1           20050601    1200        0           155.129     od          sg          sens        0001        0           1
#EOF

#$gribls -m out.grib | tee out
#grep sens out | sed 's/ *$//' > content
#cmp list content


