#!/bin/bash
set -e

# Docker stop function
function stop()
{
P1=$(docker ps -q)
if [ "${P1}" != "" ]; then
  echo "Killing all running containers"  &2> /dev/null
  docker kill ${P1}
fi

P2=$(docker ps -aq)
if [ "${P2}" != "" ]; then
  echo "Removing all containers"  &2> /dev/null
  docker rm ${P2} -f
fi
}

if [ "$1" == "stop" ]; then
 echo "Stopping all Docker containers" >&2
 stop
 exit 0
fi

# Get the current directory.
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Get the full path to this script.
SOURCE="${DIR}/composer.sh"

# Create a work directory for extracting files into.
WORKDIR="$(pwd)/composer-data"
rm -rf "${WORKDIR}" && mkdir -p "${WORKDIR}"
cd "${WORKDIR}"

# Find the PAYLOAD: marker in this script.
PAYLOAD_LINE=$(grep -a -n '^PAYLOAD:$' "${SOURCE}" | cut -d ':' -f 1)
echo PAYLOAD_LINE=${PAYLOAD_LINE}

# Find and extract the payload in this script.
PAYLOAD_START=$((PAYLOAD_LINE + 1))
echo PAYLOAD_START=${PAYLOAD_START}
tail -n +${PAYLOAD_START} "${SOURCE}" | tar -xzf -

# stop all the docker containers
stop



# run the fabric-dev-scripts to get a running fabric
./fabric-dev-servers/downloadFabric.sh
./fabric-dev-servers/startFabric.sh
./fabric-dev-servers/createComposerProfile.sh

# pull and tage the correct image for the installer
docker pull hyperledger/composer-playground:0.15.0
docker tag hyperledger/composer-playground:0.15.0 hyperledger/composer-playground:latest


# Start all composer
docker-compose -p composer -f docker-compose-playground.yml up -d
# copy over pre-imported admin credentials
cd fabric-dev-servers/fabric-scripts/hlfv1/composer/creds
docker exec composer mkdir /home/composer/.composer-credentials
tar -cv * | docker exec -i composer tar x -C /home/composer/.composer-credentials

# Wait for playground to start
sleep 5

# Kill and remove any running Docker containers.
##docker-compose -p composer kill
##docker-compose -p composer down --remove-orphans

# Kill any other Docker containers.
##docker ps -aq | xargs docker rm -f

# Open the playground in a web browser.
case "$(uname)" in
"Darwin") open http://localhost:8080
          ;;
"Linux")  if [ -n "$BROWSER" ] ; then
	       	        $BROWSER http://localhost:8080
	        elif    which xdg-open > /dev/null ; then
	                xdg-open http://localhost:8080
          elif  	which gnome-open > /dev/null ; then
	                gnome-open http://localhost:8080
          #elif other types blah blah
	        else
    	            echo "Could not detect web browser to use - please launch Composer Playground URL using your chosen browser ie: <browser executable name> http://localhost:8080 or set your BROWSER variable to the browser launcher in your PATH"
	        fi
          ;;
*)        echo "Playground not launched - this OS is currently not supported "
          ;;
esac

echo
echo "--------------------------------------------------------------------------------------"
echo "Hyperledger Fabric and Hyperledger Composer installed, and Composer Playground launched"
echo "Please use 'composer.sh' to re-start, and 'composer.sh stop' to shutdown all the Fabric and Composer docker images"

# Exit; this is required as the payload immediately follows.
exit 0
PAYLOAD:
Z=rzMrST.Xf*,Mdh9:G8UyF!?yMw"	{|[kP_}ǹb;
`=0D""GI ,rG|Xx<N#B{.8
M&lER"R
}l	[c!*a9a/݆s0fmi~5Em$mn!l4wUHHa)U<Oxeq@f;x0lX6(1(%1mhB\k:;MCHJH1M
˕R*9.hfP۰@MHsqgs=6熎wڑiU
`붦VUSU5,T;; h4-|GEں<Cd&56NPp8(P֫as<ì!Oc<GS#7̎
y;"-؉r7{	\ϞsZӊ>AҜRmM2fϕ*-ߴn;~q)gw݌A	v=>8Ȳ+0ԖΛC'm)?ymC-;vzDё3=Wj;=ХLjd:p}
>pw@
u4tu!/f_Xx{{d
|ߔAltGD)*	x(U?BXc*OR5$;&1.Kb"{͂g,йf@,l勵F'Y׿YUz0echhPH`i`Mv_ @A/pa>^a@(xaAL?K#bx-ih9gg Yu	J4H`
ftQ
:I,3^SSM$|686T3x
N(sNBS}ahVg<i6WOS[n&
-{pYU<ಪJKi¡՞!jFu;5Z-Mvkm8ZXAН!QhLB`,80@Ju8Yx|DΠ.kz}2|0#kZzUxO|$qD9i):݄6h^&j]0]W)l1Bmt>c>2FX ["MX.`Y%!=)M>Bjo

y~'U\0^9.̬H96՚m@	@{Mz1=@kKnh$=>;1a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