#!/bin/bash
project=meidi
channel=mychannel
docker_peer_images="hyperledger/fabric-peer:x86_64-1.0.0-alpha"
docker_couchdb_images="hyperledger/fabric-couchdb:x86_64-1.0.0-alpha"
docker_orderer_images="docker.io/hyperledger/fabric-orderer:x86_64-1.0.0-alpha"
docker_ccenv_images="docker.io/hyperledger/fabric-ccenv:x86_64-1.0.0-alpha"
docker_baseos_images="docker.io/hyperledger/fabric-baseos:x86_64-0.3.0"

rm -rf "$project"_*

function create_cli() {
    rm -rf "$projec"_cli
    mkdir -p "$project"_cli

    echo "docker pull $docker_peer_images" >> "$project"_cli/download-dockerimages.sh
    echo "docker tag $docker_peer_images hyperledger/fabric-peer" >> "$project"_cli/download-dockerimages.sh
    echo "CHANNEL_NAME=$channel ./docker-compose -f docker-compose-cli.yaml up -d " >> "$project"_cli/start.sh
    echo "docker logs -f cli" >> "$project"_cli/start.sh
    chmod u+x "$project"_cli/download-dockerimages.sh
    chmod u+x "$project"_cli/start.sh

    mkdir -p "$project"_cli/scripts
    cp -rf examples "$project"_cli
    cp -rf crypto "$project"_cli
    cp -rf scripts "$project"_cli
    cp -rf docker-compose "$project"_cli
    cp -rf docker-compose-cli.yaml "$project"_cli

    tar -cvf "$project"_cli.tar "$project"_cli
    gzip "$project"_cli.tar
    rm -rf "$project"_cli
}

function create_orderer() {
    while read -s -n1 -p "Are you want to generateCfgTrx channel.tx and orderer.block ? [y|n]" input
    do
        case $input in
        Y|y)
            echo "generateCfgTrx"
    	    CURRENT_DIR=$PWD
            source generateCfgTrx.sh $channel
            cd $CURRENT_DIR
	    break;;
        N|n)
            echo "skip generateCfgTrx"
	    break;;
        *)
            echo "input error";;
        esac
    done


    rm -rf "$project"_orderer
    mkdir -p "$project"_orderer
    echo "docker pull $docker_orderer_images" >> "$project"_orderer/download-dockerimages.sh
    echo "docker tag $docker_orderer_images hyperledger/fabric-orderer" >> "$project"_orderer/download-dockerimages.sh
    echo "CHANNEL_NAME=$channel ./docker-compose -f docker-compose-orderer0.yaml up -d " >> "$project"_orderer/start.sh
    chmod u+x "$project"_orderer/download-dockerimages.sh
    chmod u+x "$project"_orderer/start.sh

    mkdir -p "$project"_orderer/crypto
    cp -rf crypto/orderer "$project"_orderer/crypto
    cp -rf docker-compose "$project"_orderer
    cp -rf docker-compose-orderer0.yaml "$project"_orderer
    tar -cvf "$project"_orderer0.tar "$project"_orderer
    gzip "$project"_orderer0.tar
    rm -rf "$project"_orderer
}

function create_peer() {
    rm -rf $project
    mkdir -p $project

    for N in 0 1 2 3 ; do
        rm -rf $project
        mkdir -p $project
        echo "CHANNEL_NAME=$channel ./docker-compose -f docker-compose-peer"$N".yaml up -d " >> $project/start.sh
        echo "docker pull $docker_peer_images" >> $project/download-dockerimages.sh
        echo "docker tag $docker_peer_images hyperledger/fabric-peer" >> $project/download-dockerimages.sh
        echo "docker pull $docker_couchdb_images" >> $project/download-dockerimages.sh
        echo "docker tag $docker_couchdb_images hyperledger/fabric-couchdb" >> $project/download-dockerimages.sh
        echo "docker pull $docker_ccenv_images" >> $project/download-dockerimages.sh
        echo "docker tag $docker_ccenv_images hyperledger/fabric-ccenv" >> $project/download-dockerimages.sh
        echo "docker pull $docker_baseos_images" >> $project/download-dockerimages.sh
        chmod u+x $project/start.sh
        chmod u+x $project/download-dockerimages.sh

        mkdir -p ./$project/peer-base
        mkdir -p ./$project/crypto/peer/peer$N/localMspConfig
        cp -rf docker-compose $project
        cp -rf docker-compose-peer"$N".yaml $project
        cp -rf peer-base/peer-base.yaml $project/peer-base/peer-base.yaml
        cp -rf crypto/peer/peer$N/localMspConfig/* $project/crypto/peer/peer$N/localMspConfig/
        tar -cvf "$project"_peer$N.tar $project
        gzip "$project"_peer$N.tar
        rm -rf $project
    done

    rm -rf $project
}


create_orderer

create_cli

create_peer

