#!/bin/bash

set -e

trap 'echo "got an error"' ERR

add_resource() {
    local num="$1"
    local add_constraints=${2:-false}
    local use_crm=${3:-false}
    
    cat <<EOF > /tmp/resource-$num.xml
<primitive class="ocf" provider="foo" type="Target" id="resource-$num">
  <meta_attributes id="resource-$num-meta_attributes">
    <nvpair name="target-role" id="resource-$num-meta_attributes-target-role" value="Stopped"/>
  </meta_attributes>
  <operations id="resource-$num-operations">
    <op id="resource-$num-monitor-120" interval="120" name="monitor" timeout="60"/>
    <op id="resource-$num-start-0" interval="0" name="start" timeout="300"/>
    <op id="resource-$num-stop-0" interval="0" name="stop" timeout="300"/>
  </operations>
  <instance_attributes id="resource-$num-instance_attributes">
    <nvpair id="resource-$num-instance_attributes-target" name="target" value="resource-$num"/>
  </instance_attributes>
</primitive>
EOF

    if ! $use_crm; then
        cibadmin -o resources -C -x /tmp/resource-$num.xml
        if $add_constraints; then
            cibadmin -o constraints -C -X "<rsc_location id=\"resource-$num-primary\" node=\"$(uname -n)\" rsc=\"resource-$num\" score=\"10\"/>"
        fi
    else
        crm configure primitive resource-$num ocf:foo:Target meta \
            target-role="stopped" operations \$id="resource-$num-operations" \
            op monitor interval="120" timeout="60" op start interval="0" \
            timeout="300" op stop interval="0" timeout="300" params \
            target="resource-$num"
    fi

}

remove_resource() {
    local num="$1"

    cibadmin -D -X "<rsc_location id=\"resource-$num-primary\">"
    cibadmin -D -X "<primitive id=\"resource-$num\">"

}

CLEAN=${CLEAN:-false}
USE_CRM=${USE_CRM:-false}
ADD_CONSTRAINTS=${ADD_CONSTRAINTS:-false}
CONCURRENT=${CONCURRENT:-false}

# not interested in conncurrent cleaning at this time
if $CLEAN; then
    CONCURRENT=false
fi

for x in $(seq 1 50); do
    if $CLEAN; then
        remove_resource $x $AMP
    else
        if $CONCURRENT; then
            add_resource $x $USE_CRM $ADD_CONSTRAINTS &
        else
            add_resource $x $USE_CRM $ADD_CONSTRAINTS 
        fi
    fi
done
if $CONCURRENT; then
    echo "waiting: $(date)"
    wait
    echo "done: $(date)"
fi
