#!/bin/bash

# To run this script with a locally built loom binary set the LOOM_BIN env var to point to your loom
# binary. If you plan on running the tests frequently you'll save a lot of time if you run the
# steps in the setup_weave_blueprint in another directory, and then set LOOM_BLUEPRINT_DIR env var
# to point to it, otherwise the contract will be recloned and rebuilt every time.

set -euxo pipefail

ls $(go env GOPATH)

# Prepare env
DEFAULT_GOPATH=$(go env GOPATH)
GANACHE_PORT=8545
REPO_ROOT=`pwd`
LOOM_DIR=`pwd`/tmp/e2e


# Check available platforms
PLATFORM='unknown'
unamestr=`uname`
if [[ "$unamestr" == 'Linux' ]]; then
  PLATFORM='linux'
elif [[ "$unamestr" == 'Darwin' ]]; then
  PLATFORM='osx'
else
  echo "Platform not supported on this script yet"
  exit 1
fi

download_dappchain() {
  cd $LOOM_DIR
  wget -O loom https://downloads.loomx.io/loom/$PLATFORM/$BUILD_ID/loom-gateway
  chmod +x loom
  LOOM_BIN=`pwd`/loom
}

setup_weave_blueprint() {
  cd $LOOM_DIR
  git clone https://github.com/loomnetwork/weave-blueprint.git
  cd weave-blueprint
  LOOM_BLUEPRINT_DIR=`pwd`
  make deps
  make
}

setup_dappchain() {
  cd $LOOM_DIR
  $LOOM_BIN init -f
  cp -R $REPO_ROOT/e2e_support/* .
  cp -R $REPO_ROOT/e2e_support/tm-config/* chaindata/config/
  # mkdir -p contracts
  # cp $LOOM_BLUEPRINT_DIR/build/contracts/* contracts
}

start_chains() {
  $REPO_ROOT/node_modules/.bin/ganache-cli -d -p $GANACHE_PORT >> ganache.log &
  GANACHE_PID=$!

  cd $LOOM_DIR
  $LOOM_BIN run &
  LOOM_PID=$!
  sleep 5

  $LOOM_BIN deploy -a public_key_acc_1 -k private_key_acc_1 -n SimpleStore -b ./SimpleStore.bin
}

stop_chains() {
  if [[ -n "$GANACHE_PID" ]]; then
    kill -9 $GANACHE_PID
    GANACHE_PID=""
  fi
  if [[ -n "$LOOM_PID" ]]; then
    kill -9 $LOOM_PID
    LOOM_PID=""
  fi
  # pkill -f "${LOOM_DIR}/contracts/blueprint.0.0.1" || true
}

run_tests() {
  yarn e2e:node
}

cleanup() {
  stop_chains
  export GOPATH=$DEFAULT_GOPATH
}

if [ "${CI:-}" ]; then
  mkdir -p $LOOM_DIR

  # setup_weave_blueprint
  download_dappchain
fi

setup_dappchain

trap cleanup EXIT

start_chains
run_tests
cleanup

sleep 1

if [ "${CI:-}" ]; then
  rm -rf $LOOM_DIR
fi
