#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
source $BASEDIR/../lib/util/util.sh
source ../lib/util/util.sh 2>/dev/null # for IDE code completion

usageMsg="ORG=<org name> ORDERER_NAME=<orderer instance name> $0"
exampleMsg="ORG=org1 ORDERER_NAME=raft0 $0"

: ${DOMAIN:=example.com}
: ${ORDERER_NAME_PREFIX:=raft}
: ${ORDERER_DOMAIN:=$DOMAIN}
: ${ORDERER_NAME:=orderer}
: ${RAFT_NODES_COUNT:=1}
: ${RAFT_NODES_NUMBERING_START:=0}
: ${DOCKER_COMPOSE_ORDERER_ARGS:="-f docker-compose-orderer.yaml -f docker-compose-orderer-domain.yaml"}
#: ${WWW_PORT:=81}

export DOMAIN ORDERER_NAME ORDERER_DOMAIN WWW_PORT RAFT_NODES_COUNT RAFT_NODES_NUMBERING_START

COMPOSE_PROJECT_NAME=${ORDERER_NAME}.${ORDERER_DOMAIN} ORDERER_NAME=${ORDERER_NAME} ORDERER_NAME_PREFIX=${ORDERER_NAME_PREFIX} ORDERER_GENESIS_PROFILE=Raft3OrdererGenesis ORDERER_GENERAL_LISTENPORT=${RAFT0_CONSENTER_PORT:-7050} docker-compose ${DOCKER_COMPOSE_ORDERER_ARGS} up -d www.orderer cli.orderer

RAFT_NUMBER=$(($RAFT_NODES_NUMBERING_START+1))
ORDERER_NAME=${ORDERER_NAME_PREFIX}$RAFT_NUMBER
COMPOSE_PROJECT_NAME=${ORDERER_NAME}.${ORDERER_DOMAIN} ORDERER_NAME=${ORDERER_NAME} ORDERER_NAME_PREFIX=${ORDERER_NAME_PREFIX} ORDERER_GENERAL_LISTENPORT=${RAFT1_CONSENTER_PORT:-7150} docker-compose ${DOCKER_COMPOSE_ORDERER_ARGS} up -d cli.orderer

RAFT_NUMBER=$(($RAFT_NUMBER+1))
ORDERER_NAME=${ORDERER_NAME_PREFIX}$RAFT_NUMBER
COMPOSE_PROJECT_NAME=${ORDERER_NAME}.${ORDERER_DOMAIN} ORDERER_NAME=${ORDERER_NAME} ORDERER_NAME_PREFIX=${ORDERER_NAME_PREFIX} ORDERER_GENERAL_LISTENPORT=${RAFT2_CONSENTER_PORT:-7250} docker-compose ${DOCKER_COMPOSE_ORDERER_ARGS} up -d cli.orderer

