#!/usr/bin/env bash
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# 	  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -euo pipefail

function print() {
  GREEN='\033[0;32m'
  NC='\033[0m'
  echo
  echo -e "${GREEN}${1}${NC}"
}

function touteSuite() {
  createCluster
}

function quitterLaScene() {
  destroyCluster
}

function createCluster() {
  print "Initializing KIND Kubernetes cluster"
  just kind
}

function destroyCluster() {
  print "Destroying KIND Kubernetes cluster"
  just destroy
  just unkind
}

# fabric CLI binaries + config
FABRIC_VERSION=2.5.8
FABRIC_CA_VERSION=1.5.11

curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary --fabric-version $FABRIC_VERSION --ca-version $FABRIC_CA_VERSION
export PATH=${PWD}/bin:$PATH
export FABRIC_CFG_PATH=${PWD}/config

# Set The Stage: a local KIND cluster
touteSuite
trap "quitterLaScene" EXIT


# Act I: launch Fabric services
#export FABRIC_CFG_PATH=${PWD}/config

just start org0
just start org1
just start org2

just enroll org0
just enroll org1
just enroll org2

just check-network


# Act II: Build a Consortium

just export-msp org0
just export-msp org1
just export-msp org2

just create-genesis-block
just inspect-genesis-block

just join org0
just join org1
just join org2


# Act III: Chaincode and application

just install-cc org1
just install-cc org2

# org1:
export ORG=org1
export MSP_ID=Org1MSP

export $(just show-context $MSP_ID $ORG peer1)

print "env context:"
export

print "querying cc as org1"
peer chaincode query \
  -n asset-transfer \
  -C mychannel \
  -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}'

# org2:
export ORG=org2
export MSP_ID=Org2MSP

export $(just show-context $MSP_ID $ORG peer1)

peer chaincode query \
  -n asset-transfer \
  -C mychannel \
  -c '{"Args":["org.hyperledger.fabric:GetMetadata"]}'


# Client application:  (still org2 context)

export USER_MSP_DIR=$PWD/organizations/$ORG/enrollments/${ORG}user/msp
export PRIVATE_KEY=$USER_MSP_DIR/keystore/key.pem
export CERTIFICATE=$USER_MSP_DIR/signcerts/cert.pem
export TLS_CERT=$CORE_PEER_TLS_ROOTCERT_FILE
export ENDPOINT=${ORG}-peer-gateway.${ORG}.localho.st:443

SAMPLES_DIR=$(mktemp -d)
git clone https://github.com/hyperledger/fabric-samples.git $SAMPLES_DIR

( pushd ${SAMPLES_DIR}/full-stack-asset-transfer-guide/applications/trader-typescript \
  && npm install
  && npm start getAllAssets
  && npm start create banana bananaman yellow
  && npm start getAllAssets )

