#!/bin/bash

set -x

export LC_ALL=C

agents=()
supported_agents=()
unsupported_agents=()

for file in tests/data/metadata/*.xml; do
  agent=$(sed -ne 's/<resource-agent name="\(.*\)" shortdesc="\(.*\)".*/\1: \2/p' "$file" | \
          sed -e 's/\(I.O \)\?\(Fence\|Fencing\) agent for //')
  agents+=("$agent")
done

for agent in "${agents[@]}"; do
  # This agent was already skipped previously, not related to the split of the
  # supported and unsupported fence-agents.
  if grep -qFx "fence_amt_ws" <<< "$agent"; then
    continue
  fi

  if grep -qFx $(cut -d: -f1 <<< "$agent") debian/supported-agents; then
    supported_agents+=("$agent\${Newline}")
  else
    unsupported_agents+=("$agent\${Newline}")
  fi
done

echo "agents=${unsupported_agents[*]}" >> debian/fence-agents.substvars
echo "supportedAgents=${supported_agents[*]}" >> debian/fence-agents-supported.substvars
