#!/bin/bash
#
# Copyright Greg Haskins All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

set -e

DAEMONS="_COMPOSE_DAEMONS_"
CHANNEL_NAME=_COMPOSE_CHANNEL_NAME_
PEERS="_COMPOSE_PEERS_"
NODES="_COMPOSE_NODES_"
TLS=_COMPOSE_TLS_

SCRIPTDIR=`dirname $0`
OUTPUT=${1:-$SCRIPTDIR/client.config}

getip() {
    HOST=$1

    docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $HOST
}

url() {
    TYPE=$1
    NODE=$2
    PORT=$3

    if [ "$TLS" == "true" ]; then
        TYPE="$1s"
    fi

    echo $TYPE://$(getip $NODE):$PORT
}

http() {
    NODE=$1
    PORT=$2

    echo $(url "http" $NODE $PORT)
}

grpc() {
    NODE=$1
    PORT=$2

    echo $(url "grpc" $NODE $PORT)
}

peerurls() {
    for i in $(seq 1 4); do
        echo "$(url grpc peer$i, 7051)"
    done
}

generate_hosts() {
    for NODE in $DAEMONS; do
        echo "$(getip $NODE) $NODE"
    done
}

includefile() {
    file=$1
    prefix=$2

    echo "|"

    while IFS= read -r line; do
        printf '%s%s\n' "$prefix" "$line"
    done < "$file"
}

echo "========================================================================"
echo "Launching Hyperledger Cluster via docker-compose"
echo "========================================================================"
docker-compose -f $SCRIPTDIR/docker-compose.yaml up -d $DAEMONS

echo "========================================================================"
echo "Configuring cluster"
echo "========================================================================"
(cd $SCRIPTDIR && docker-compose run --rm cli ./configure.sh $CHANNEL_NAME "channel.tx anchor.tx" "$PEERS" $TLS)

echo "========================================================================"
echo "Cluster ready!"
echo "========================================================================"
echo
generate_hosts | sort

cat <<EOF > $OUTPUT
#
# Generated by fabric.git/examples/cluster.  DO NOT EDIT!
#
ca:
        url: $(http "ca" "7054")
        certificate: $(includefile $SCRIPTDIR/nodes/ca/ca.crt "              ")

tlsca:
        url: $(http "tlsca" "7054")
        certificate: $(includefile $SCRIPTDIR/nodes/tlsca/ca.crt "              ")

orderer:
        url:  $(grpc "orderer" "7050")
        hostname: orderer
        ca: $(includefile $SCRIPTDIR/nodes/orderer/tls/ca.crt "              ")

peers:
$(for i in $(seq 1 4); do
      echo "       - api: $(grpc peer$i 7051)"
      echo "         events: $(grpc peer$i 7053)"
      echo "         hostname: peer$i"
done)

identity:
        principal: Admin@org1.net
        mspid: Org1MSP
        privatekey: $(includefile $SCRIPTDIR/nodes/cli/tls/server.key  "              ")
        certificate: $(includefile $SCRIPTDIR/nodes/cli/tls/server.crt  "              ")
EOF

echo "Output written to $OUTPUT"
