#!/bin/bash

SERVER=192.168.172.128:42423
DOMAINURL=http://$SERVER/restms/domain/default

##################################################################
# create one feed called testfeed and store it's URL into $FEEDURL
##################################################################

wget -O - --server-response --header="accept:application.restms+xml" --header="content-type:application/restms+xml" --post-data='<?xml version="1.0"?>
<restms xmlns="http://www.imatix.com/schema/restms">
  <feed
    type="topic"
    name="testfeed"
    />
</restms>' $DOMAINURL > 001.createfeed.txt 2>&1

FEEDURL=$(cat 001.createfeed.txt | grep ' \+Location:' | sed 's/.*Location:\s\+//')

echo "Feed URL: $FEEDURL"

##################################################################
# create dynamic pipe and store it's URL into $PIPEURL
##################################################################

wget -O - --server-response --header="accept:application/restms+xml" --header="content-type:application/restms+xml" --post-data='<?xml version="1.0"?>
<restms xmlns="http://www.imatix.com/schema/restms">
  <pipe type="fifo" />
</restms>' $DOMAINURL > 002.createpipe.txt 2>&1

PIPEURL=$(cat 002.createpipe.txt | grep ' \+Location:' | sed 's/.*Location:\s\+//')

echo "Pipe URL: $PIPEURL"

##################################################################
# join the pipe on several topics
##################################################################

for TOPIC in `seq 1 30`; do
  wget -O - --server-response --header="accept:application/restms+xml" --header="content-type:application/restms+xml" --post-data='<?xml version="1.0"?>
    <restms xmlns="http://www.imatix.com/schema/restms">
      <join address="'"$TOPIC"'" feed="'"$FEEDURL"'" />
    </restms>' $PIPEURL > 003.join.$TOPIC.txt 2>&1
done

echo "Topics joined"

##################################################################
# get back the pipe XML
##################################################################

wget -O - --server-response --header="accept:application/restms+xml" $PIPEURL > 004.getpipe.txt 2>&1

echo "The resulting XML is in 004.getpipe.txt"


