#/bin/sh

topic="data"

rt_0=$(docker port rt-${topic}-0 | grep "5000/tcp" | awk '{ print $3}')
rt_1=$(docker port rt-${topic}-1 | grep "5000/tcp" | awk '{ print $3}')
rt_2=$(docker port rt-${topic}-2 | grep "5000/tcp" | awk '{ print $3}')
ca=$(cat certs/client/ca | sed ':a;N;$!ba;s/\n/\\n/g')
cert=$(cat certs/client/cert | sed ':a;N;$!ba;s/\n/\\n/g')
key=$(cat certs/client/key | sed ':a;N;$!ba;s/\n/\\n/g')

cat << EOS > "client.json"
{
  "name": "client-name",
  "topics": {
    "insert": "$topic",
    "query": "requests"
  },
  "ca": "$ca\n",
  "cert": "$cert\n",
  "key": "$key\n",
  "insert": {
    "insert": [
      ":$rt_0",
      ":$rt_1",
      ":$rt_2"
    ],
    "query": []
  },
  "query": []
}
EOS