#!/bin/bash

set +xe

rbdimagefeatures="--image-feature layering --image-feature exclusive-lock --image-feature object-map --image-feature fast-diff"

function test {
echo "===   Creating image PRIMARY  ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd create replicapool/test$i --size 4G $rbdimagefeatures --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Enabling mirror PRIMARY  ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image enable replicapool/test$i snapshot --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Enabling snapshot schedule 1m PRIMARY  ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror snapshot schedule add --pool replicapool --image test$i 1m --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Listing snapshot schedule PRIMARY  ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror snapshot schedule list -p replicapool --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Mirror image status PRIMARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image status replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Mirror snapshot list PRIMARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd -p replicapool snap ls test$i --all --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Perform IO on PRIMARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd bench --io-type write --io-size 4K --io-threads 2 --io-total 30M --io-pattern seq replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
sleep 85
echo "===   demote PRIMARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image demote replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Mirror image status PRIMARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image status replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo

echo "===   Mirror image status SECONDARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cephwest rbd mirror image status replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Mirror snapshot list SECONDARY   ==="
for i in {1..68}; do echo "$i";../src/mrun cephwest rbd -p replicapool snap ls test$i --all --debug-rbd 0 --debug-ms 0;done
echo
echo "===   Mirror promote secondary ==="
for i in {1..68}; do echo "$i";../src/mrun cephwest rbd mirror image promote replicapool/test$i --force --debug-rbd 0 --debug-ms 0;done
echo
}

function cleanup {
echo "=== disable mirroring and remove images cephwest==="
for i in {1..68}; do echo "$i";../src/mrun cephwest rbd mirror image disable replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
for i in {1..68}; do echo "$i";../src/mrun cephwest rbd rm replicapool/test$i --debug-rbd 0 --debug-ms 0;done

echo "=== disable mirroring and remove images cepheast==="
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image promote replicapool/test$i --force --debug-rbd 0 --debug-ms 0;done
echo
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd mirror image disable replicapool/test$i --debug-rbd 0 --debug-ms 0;done
echo
for i in {1..68}; do echo "$i";../src/mrun cepheast rbd rm replicapool/test$i --debug-rbd 0 --debug-ms 0;done

echo " cleanuplogs "
rm -rf $(pwd)/run/cepheast/rbd_mirror_cepheast.log
rm -rf $(pwd)/run/cephwest/rbd_mirror_cephwest.log
}

echo "test"
echo "cleanup"

read varname

case $varname in
  test)
  test
  ;;
  cleanup)
  cleanup
  ;;
  *)
  echo "retry, select setup or cleanup"
esac
