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

main() {
  readonly TASK="UNLOCK_SCORE"

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

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

  list() {
    oio-cluster -r $NS
  }

  log "INFO" "Unlocking scores for the namespace $NS"

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

. $(dirname $0)/oio-ga-tools-logger main "$@"
