#!/usr/bin/env bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/functions

MONGO_VERSION=${MONGODB_VERSION:-"5.0"}

desc 'RUN RESTORE ON NEW CLUSTER TEST'

desc 'Start cluster'
start_cluster "$MONGO_VERSION"

desc 'Create test user and role'
mongo_run 'db.getSiblingDB("admin").createRole({ "role": "testRole1","privileges": [{ "resource": { "anyResource": true },"actions": [ "anyAction" ]}],"roles": []});' "rs1"
mongo_run 'db.getSiblingDB("admin").createUser({user: "testUser1",pwd: "test1234","roles" : [{ "db" : "admin", "role" : "testRole1" }]});' "rs1"

desc 'Add test data'
mongo_run 'for (var i = 1; i <= 50000; i++) db.getSiblingDB("test").testts.insert( { x : Date.now(), y: i } )' "rs1"

desc 'Configure PBM'
pbm_run config --file=/etc/pbm/minio.yaml
sleep 6
wait_noop
pbm_run status

date
desc 'Backup'
pbm_run backup
wait_backup
pbm_run config --set pitr.enabled=true
sleep 6
mongo_run 'db.getSiblingDB("admin").createRole({ "role": "testRolePITR","privileges": [{ "resource": { "anyResource": true },"actions": [ "anyAction" ]}],"roles": []});' "rs1"
mongo_run 'db.getSiblingDB("admin").createUser({user: "testUserPITR",pwd: "test1234","roles" : [{ "db" : "admin", "role" : "testRolePITR" }]});' "rs1"
date
sleep 30
pbm_run config --set pitr.enabled=false
wait_noop
pbm_run logs -t 10
PITR_Time=$(pbm_run status -s backups | grep -o " \- [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}T[0-9]\{2\}\:[0-9]\{2\}\:[0-9]\{2\}" | sed "s/ - //" | head -n 1)

testdb_hash_og=$(mongo_run 'db.getSiblingDB("test").runCommand( { dbHash: 1 } ).md5' "rs1" | tail -n 1 | tr -d '\r')
roles_og=$(mongo_run 'db.getSiblingDB("admin").system.roles.find({}, {_id: 1}).sort({_id: -1})' rs1 | grep '{ "_id" : "' | awk -F\" '{print$4}')
users_og=$(mongo_run 'db.getSiblingDB("admin").system.users.find({}, {_id: 1}).sort({_id: -1})' rs1 | grep '{ "_id" : "' | awk -F\" '{print$4}')

date
desc 'Destroy cluster'
docker compose -f ${test_dir}/docker/docker-compose.yaml down
desc 'Removing PSMDB volumes'
docker volume rm $(docker volume ls --filter name=data -q)

date
desc 'Start a new cluster'
start_cluster "$MONGO_VERSION"
date

desc 'Create test user and role'
mongo_run 'db.getSiblingDB("admin").createRole({ "role": "testRole2","privileges": [{ "resource": { "anyResource": true },"actions": [ "anyAction" ]}],"roles": []});' "rs1"
mongo_run 'db.getSiblingDB("admin").createUser({user: "testUser2",pwd: "test1234","roles" : [{ "db" : "admin", "role" : "testRole2" }]});' "rs1"

desc 'Configure PBM'
pbm_run config --file=/etc/pbm/minio.yaml
pbm_run status
sleep 6
wait_noop
pbm_run list

desc 'Restore'
pbm_run restore --time $PITR_Time
wait_restore
pbm_run logs -t 35

testdb_hash_re=$(mongo_run 'db.getSiblingDB("test").runCommand( { dbHash: 1 } ).md5' "rs1" | tail -n 1 | tr -d '\r')
roles_re=$(mongo_run 'db.getSiblingDB("admin").system.roles.find({}, {_id: 1}).sort({_id: -1})' rs1 | grep '{ "_id" : "' | awk -F\" '{print$4}')
users_re=$(mongo_run 'db.getSiblingDB("admin").system.users.find({}, {_id: 1}).sort({_id: -1})' rs1 | grep '{ "_id" : "' | awk -F\" '{print$4}')

desc 'Check the data'
compare "$testdb_hash_og" "$testdb_hash_re"
compare_arrays "$roles_og" "$roles_re"
compare_arrays "$users_og" "$users_re"

desc 'Destroy cluster'
destroy_cluster $COMPOSE_PATH
