#!/usr/bin/env bash
# Unlock score TASK
# Description: Unlocks the score of all services of a namespace.

TASK="UNLOCK_SCORE"
source $(dirname $0)/oio-ga-tools-logger

NS=$1
timeout=120
count=9
str="score"

if [ -n "$2" ]; then
   count="$2"
fi

list() {
  /usr/bin/oio-cluster -r $NS
}

echo "test" >&2


while true; do
    sc_count=$(list | grep -c $str)
    if [ "$sc_count" -ge "$count" ]; then
       /usr/bin/oio-cluster -r $NS | xargs -n 1 oio-cluster $NS --unlock-score -S
       exit 0
    fi
    sleep 1
    ((timeout=timeout-1))
    if [ "$timeout" -eq 0 ]; then
       exit 1
    fi
done
