#!/bin/sh
set -e

RES=0

echo '* fixing /etc/siridb/siridb.conf'
sed --in-place 's/http_api_port = 0/http_api_port = 9020/' /etc/siridb/siridb.conf

echo '* restarting siridb-server'
service siridb-server restart

echo '* touch res.txt'
touch res.txt

echo '* sleep to let the server start up properly'
sleep 5

echo '* run queries'
echo '  get-version'
curl --silent --show-error --location --output res.txt \
     --retry 10 \
     --request GET 'http://localhost:9020/get-version' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' || RES=1
cat res.txt
echo

echo '  new-database'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/new-database' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"dbname": "sampledb",
	"time_precision": "s",
	"buffer_size": 8192,
	"duration_num": "1w",
	"duration_log": "3d"
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  new-account'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/new-account' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
        "account": "bob",
	"password": "passwd4bob"
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  change-password'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/change-password' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
        "account": "bob",
	"password": "pass"
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  drop-account'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/drop-account' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
       "account": "bob"
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  drop-database'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/drop-database' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"database": "sampledb",
	"ignore_offline": false
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  drop-database again'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/drop-database' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"database": "sampledb",
	"ignore_offline": false
}' || RES=1
cat res.txt
echo
expect='{"error_msg":"cannot find database '"'"'sampledb'"'"'"}'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  get-accounts'
curl --silent --show-error --location --output res.txt \
     --request GET 'http://localhost:9020/get-accounts' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ=='
cat res.txt
echo
expect='["sa"]'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  new-database for queries'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/new-database' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ==' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"dbname": "sampledb",
	"time_precision": "s",
	"buffer_size": 8192,
	"duration_num": "1w",
	"duration_log": "3d"
}' || RES=1
cat res.txt
echo
expect='"OK"'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  get-databases'
curl --silent --show-error --location --output res.txt \
     --request GET 'http://localhost:9020/get-databases' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic c2E6c2lyaQ=='
cat res.txt
echo
expect='["sampledb"]'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  query data (nothing there)'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/query/sampledb' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic aXJpczpzaXJp' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"q": "select count() from '\''aggr'\''",
	"t": "ms"
}' || RES=1
cat res.txt
echo
expect='{}'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  insert data'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/insert/sampledb' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic aXJpczpzaXJp' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
    "aggr": [
        [1578933215, 42],
        [1578933223, 123]
    ]
}' || RES=1
cat res.txt
echo
expect='{"success_msg":"Successfully inserted 2 point(s)."}'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

echo '  query data'
curl --silent --show-error --location --output res.txt \
     --request POST 'http://localhost:9020/query/sampledb' \
     --header 'Content-Type: application/json' \
     --header 'Authorization: Basic aXJpczpzaXJp' \
     --header 'Content-Type: text/plain' \
     --data-raw '{
	"q": "select count() from '\''aggr'\''",
	"t": "ms"
}' || RES=1
cat res.txt
echo
expect='{"aggr":[[1578933223000,2]]}'
if [ "$(cat res.txt)" != "$expect" ] ; then RES=1 ; echo "FAILED: expected $expect" ; fi

exit $RES
