#!/bin/bash

wait_for_deployment_status () {
  status=none
  while [ $status != $2 ]
  do
    status=$(curl -u Administrator:asdasd -X GET $1 --silent | jq -r '.settings | .deployment_status')
    echo current deployment status: $status
  done
}

wait_for_response_status () {
  response=0
  while [ $response -ne $2 ]
  do
    response=$(curl -u Administrator:asdasd -X GET $1 --silent --write-out "%{http_code}\n" --output /dev/null )
    echo current status code: $response
  done
}

cluster_host=localhost
http_port=9000
eventing_port=9300

func_1_endpoint="http://$cluster_host:$eventing_port/api/v1/functions/eventing_func?bucket=default&scope=_default"
func_2_endpoint="http://$cluster_host:$eventing_port/api/v1/functions/eventing_func_legacy"

# import functions
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --import --file ~/temp/eventing_function.txt
wait_for_response_status $func_1_endpoint 200
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --import --file ~/temp/eventing_function_legacy.txt
wait_for_response_status $func_2_endpoint 200

# deploy functions
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --deploy --name eventing_func --bucket default --scope _default
wait_for_deployment_status $func_1_endpoint true
couchbase-cli eventing-function-setup -c $cluster_host:$http_port  -u Administrator -p asdasd --deploy --name eventing_func_legacy
wait_for_deployment_status $func_2_endpoint true

sleep $3
echo State of functions before the backup:
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --list

# do a backup
cbbackupmgr config -a $1 -r $2
cbbackupmgr backup -a $1 -r $2 -c $cluster_host:$http_port -u Administrator -p asdasd --no-progress-bar

# undeploy and delete eventing_func
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --undeploy --name eventing_func --bucket default --scope _default
wait_for_deployment_status $func_1_endpoint false
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --delete --name eventing_func --bucket default --scope _default
wait_for_response_status $func_1_endpoint 404

# undeploy and delete eventing_func_legacy
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --undeploy --name eventing_func_legacy
wait_for_deployment_status $func_2_endpoint false
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --delete --name eventing_func_legacy
wait_for_response_status $func_2_endpoint 404

# sleep for a given amount of time
sleep $4

cbbackupmgr restore -a $1 -r $2 -c $cluster_host:$http_port -u Administrator -p asdasd --no-progress-bar --disable-data --disable-gsi-indexes --disable-ft-indexes --disable-ft-alias --disable-analytics
sleep 5

echo State of functions after the restore:
couchbase-cli eventing-function-setup -c $cluster_host:$http_port -u Administrator -p asdasd --list
echo Done