#!/bin/bash

function clean_mci_sequence() {
	local CSP=$1
	local REGION=$2
	local POSTFIX=$3
	local TestSetFile=$4

	# echo '## 8. MCI: Refine first (remove failed VMs)'
	# OUTPUT=$(../8.mci/refine-mci.sh -c $CSP -r $REGION -n $POSTFIX -f $TestSetFile)

	# echo '## 8. MCI: Terminate'
	# OUTPUT=$(../8.mci/terminate-mci.sh -c $CSP -r $REGION -n $POSTFIX -f $TestSetFile)
	# echo "${OUTPUT}"
	# OUTPUT1=$(echo "${OUTPUT}" | grep -c 'No VM to terminate')
	# OUTPUT2=$(echo "${OUTPUT}" | grep -c 'Terminate is not allowed')
	# OUTPUT3=$(echo "${OUTPUT}" | grep -c 'does not exist')

	# if [ "${OUTPUT1}" != 1 ] && [ "${OUTPUT2}" != 1 ] && [ "${OUTPUT3}" != 1 ]; then
	# 	echo "============== sleep 30 before delete MCI obj"
	# 	dozing 30
	# fi

	../8.mci/delete-mci.sh -c $CSP -r $REGION -n $POSTFIX -f $TestSetFile -x terminate
}

SECONDS=0

echo "####################################################################"
echo "## Remove MCI only"
echo "####################################################################"

source ../init.sh

if [ "${INDEX}" == "0" ]; then
	echo "[Parallel execution for all CSP regions]"
else
	echo "[Single execution for a CSP region]"
fi
clean_mci_sequence $CSP $REGION $POSTFIX $TestSetFile

echo -e "${BOLD}"
echo "[Cleaning related commands in history file executionStatus]"
echo -e ""
echo -e "${NC}${BLUE}- Removing  (MCI) ${CSP} ${REGION} ${POSTFIX} ${TestSetFile}"
echo -e "${NC}"
sed -i "/(MCI) ${CSP} ${REGION} ${POSTFIX} ${TestSetFile//\//\\/}/d" ./executionStatus
echo ""
echo "[Executed Command List]"
cat ./executionStatus
cp ./executionStatus ./executionStatus.back
echo ""

duration=$SECONDS

printElapsed $@
