#!/usr/bin/env bash

set -eo pipefail

IFS=$'\n'
ENTRIES=$(yq -o=json '.command' ${IACD_DOCUMENT} | jq -c '.[]')

for entry in $ENTRIES; do
    entry_create=$(echo "${entry}" | jq -r '.createCommand')
    entry_delete=$(echo "${entry}" | jq -r '.deleteCommand')
    entry_compare=$(echo "${entry}" | jq -r '.compareCommand')
    if [ "${entry_delete}" = "null" ]; then
        entry_delete="/bin/true"
    fi
    if [ "${entry_compare}" = "null" ]; then
        entry_compare="/bin/true"
    fi

    case ${1} in
        "create" | "update")
            bash -c "${entry_create}"
            ;;
        "delete")
            bash -c "${entry_delete}"
            ;;
        "compare")
            bash -c "${entry_compare}"
            ;;
    esac
done
