#!/usr/bin/env bash
set -eu
this_cluster_name=$1
orig_pwd=$(pwd)

: ${TRANSMUTE_DIR:=$HOME/.transmute}
source $TRANSMUTE_DIR/.env

: ${VOYAGER_PROVIDER:='baremetal'}
: ${TRANSMUTE_DO_JWT_DL:=n}
: ${DO_FAAS:=n}
: ${DEBUG_MESSAGES:='Received problem bailing out!'}

export PATH=$HOME/.transmute/bin:$PATH
export PATH=$HOME/.local/bin:$PATH
export TRANSMUTE_ENV=minikube
export NVM_DIR="$HOME/.nvm"

exiter () {
  echo $DEBUG_MESSAGES
}

# these vars are used by the following functions
# which idempotently addlines to a file
LINE_TO_ADD=''
TARGET_FILE_FOR_ADD=~/.profile

check_if_line_exists()
{
  echo "Checking for '$LINE_TO_ADD'  in $TARGET_FILE_FOR_ADD"
  grep -qsFx "$LINE_TO_ADD" $TARGET_FILE_FOR_ADD
}

add_line_to()
{
  echo "Adding '$LINE_TO_ADD'  to $TARGET_FILE_FOR_ADD"
  TARGET_FILE=$TARGET_FILE_FOR_ADD
    [ -w "$TARGET_FILE" ] || TARGET_FILE=$TARGET_FILE_FOR_ADD
    printf "%s\n" "$LINE_TO_ADD" | sudo tee -a "$TARGET_FILE"
}

# Force NVM to load
# This is needed
# if the user has not reloaded their environment
# after installing nvm for the first time
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# Wait for kubectl and kube-dns to be initialized
$(npm root -g)/transmute-cli/scripts/w8s/ready.w8 kube-dns kube-system

sleep 5

# DISABLED RBAC FOR NOW
# Apply rbac for tiller
# echo 'create tiller service account'

# if [ -e "kubectl get serviceaccounts tiller -n kube-system" ]; then
#   echo 'tiller installed, skipping.'
# else
#   # echo 'no'
#   kubectl create --save-config serviceaccount tiller --namespace kube-system
#   echo 'create tiller rbac config'
#   kubectl apply -f $(npm root -g)/transmute-cli/components/helm/rbac-tiller-config.yaml
# fi

sleep 5

if [[ "$TRANSMUTE_VERBOSITY" -ge "111" ]] ; then
  kubectl get pods --all-namespaces
fi
if [[ "$TRANSMUTE_VERBOSITY" -ge "101" ]] ; then
  kubectl get pods -n kube-system
fi
kubectl get pods

echo "Install helm"
helm init 
# --service-account tiller

# Add transmute charts repo
helm repo add transmute-charts http://charts.transmute.network

USE_STRICT_W8=true $(npm root -g)/transmute-cli/scripts/w8s/ready.w8 tiller-deploy kube-system

sleep 1
kubectl get pods -n kube-system

echo "Ganache-cli"
helm install --name ganache transmute-charts/ganache

$(npm root -g)/transmute-cli/scripts/w8s/ready.w8 ganache default

echo "ipfs"


echo "Helm install ipfs"
helm install \
 stable/ipfs \
 --name decentralized-storage

$(npm root -g)/transmute-cli/scripts/w8s/ready.w8 decentralized-storage-ipfs default

# wait a bit longer for ipfs repo lock
sleep 5
export IPFS_POD_NAME=$(kubectl get pods --namespace default -l "app=ipfs,release=decentralized-storage"  -o jsonpath="{.items[0].metadata.name}")
kubectl exec $IPFS_POD_NAME -- ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
kubectl exec $IPFS_POD_NAME -- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]'
kubectl exec $IPFS_POD_NAME -- ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]'
kubectl delete pod $IPFS_POD_NAME 

if [[ "$DO_FAAS" == 'y' ]]; then
  echo "OpenFaaS"
  echo "Create namespaces"
  kubectl apply -f \
    https://raw.githubusercontent.com/openfaas/faas-netes/master/namespaces.yml
  #echo "OpenFaas RBAC"
  #kubectl apply -f https://raw.githubusercontent.com/openfaas/faas-netes/master/yaml/rbac.yml
  echo "OpenFaas Helmchart"
  cd $(npm root -g)/transmute-cli/components/openfaas/faas-netes/chart
  helm upgrade --install openfaas openfaas/ \
    --namespace openfaas \
    --set functionNamespace=openfaas-fn
  cd $orig_pwd
fi

helm install stable/kong --name gateway --set proxy.useTLS=false

echo "spin up"
$(npm root -g)/transmute-cli/scripts/w8s/ready.w8 gateway-postgresql default
EXCLUDE_POD=gateway-kong-migration $(npm root -g)/transmute-cli/scripts/w8s/ready.w8 gateway-kong default
export MINIKUBE_IP=$(minikube ip)
export KONG_ADMIN_URL=$(PATH=$HOME/.transmute/bin:$PATH minikube service gateway-kong-admin --url | sed 's,http://,https://,g')
export KONG_PROXY_URL=$(PATH=$HOME/.transmute/bin:$PATH minikube service gateway-kong-proxy --url)
export KONG_PROXY_PORT=$(PATH=$HOME/.transmute/bin:$PATH kubectl get service gateway-kong-proxy -o json | jq -r '.spec.ports[0].nodePort')
export KONG_HOST=$(echo $KONG_ADMIN_URL | sed 's!https://!!g' | cut -f1 -d:)
export KONG_PORT=$(echo $KONG_ADMIN_URL | sed 's!https://!!g' | cut -f2 -d:)

echo 'configure hosts'
TARGET_FILE_FOR_ADD=/etc/hosts
LINE_TO_ADD="$(minikube ip)  transmute.minikube"
check_if_line_exists || add_line_to
LINE_TO_ADD="$(minikube ip)  ipfs.transmute.minikube"
check_if_line_exists || add_line_to
LINE_TO_ADD="$(minikube ip)  ganache.transmute.minikube"
check_if_line_exists || add_line_to

echo "configure-kong-ganache"

echo 'SETTING UP GANACHE'

echo "Get the service clusterIp for Kong to use."
export GANACHE_CLUSTER_IP=$(PATH=$HOME/.transmute/bin:$PATH kubectl get service ganache -o json | jq -r '.spec.clusterIP');
echo "GANACHE_CLUSTER_IP $GANACHE_CLUSTER_IP"

countzero=0
echo "Waiting for ganache to launch on $KONG_ADMIN_URL..."
wait_for_it=100
DEBUG_MESSAGES='ganache loop'
while [[ $wait_for_it -gt 0 ]]; do
  ((++countzero))
  if [[ "$countzero" -gt 200 ]]; then
    echo 'timeout waiting for ganache to launch'
    exit 1
  fi
  if [[ "$countzero" -gt 5 ]]; then
    sleep 4
  fi
  DEBUG_MESSAGES="ganache loop $countzero"
  set +e
   # This command will fail when ganache if ganache is not up
   # set +e is required
    curl --connect-timeout 2 -s http://$KONG_HOST:$KONG_PORT &>/dev/null
    wait_for_it=$?
   # we'll test the condition afterward
  set -e
  printf '.'
  sleep 1
done
echo "ganache launched"

DEBUG_MESSAGES='kong'
echo "# Add Ganache API to Kong
curl -s -k -X POST \
  --url $KONG_ADMIN_URL/apis/ \
  --data 'name=ganache' \
  --data 'hosts=ganache.transmute.minikube' \
  --data 'upstream_url=http://'$GANACHE_CLUSTER_IP':8545/'"
echo "Add Ganache API to Kong"
curl -s -k -X POST \
  --url $KONG_ADMIN_URL/apis/ \
  --data 'name=ganache' \
  --data 'hosts=ganache.transmute.minikube' \
  --data 'upstream_url=http://'$GANACHE_CLUSTER_IP':8545/' \
  | jq -r '.'

echo 'GANACHE HEALTHCHECK'
echo "https://ganache.transmute.minikube:$KONG_PROXY_PORT"

# wait for kong to configure
sleep 5

curl -s -k -X POST \
  --url "https://ganache.transmute.minikube:$KONG_PROXY_PORT" \
  --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":68}' \
  | jq -r '.'

echo 'configure ipfs'
echo "configure-kong-ipfs.sh"

echo 'SETTING UP IPFS'

echo "Get the service clusterIp for Kong to use."
export IPFS_CLUSTER_IP=$(PATH=$HOME/.transmute/bin:$PATH kubectl get service decentralized-storage-ipfs -o json | jq -r '.spec.clusterIP');

echo "Add IPFS API to Kong"
curl -s -k -X POST \
  --url $KONG_ADMIN_URL/apis/ \
  --data 'name=ipfs' \
  --data 'hosts=ipfs.transmute.minikube' \
  --data 'upstream_url=http://'$IPFS_CLUSTER_IP':5001/' \
  | jq '.'

echo "Configure CORS for IPFS via Kong"
curl -s -k -X POST \
  --url $KONG_ADMIN_URL/apis/ipfs/plugins \
  --data "name=cors" \
  --data "config.origins=*" \
  --data "config.methods=GET, PUT, POST" \
  | jq '.'

echo 'IPFS HEALTHCHECK'
echo 'http://ipfs.transmute.minikube:'$KONG_PROXY_PORT

curl -s -k -X GET \
  --url 'http://ipfs.transmute.minikube:'$KONG_PROXY_PORT'/api/v0/id' \
  | jq '.'

if [[ "$TRANSMUTE_DO_JWT_DL" == 'y' ]]; then
  echo 'configure okta'
  #$TRANSMUTE_REPO/scripts/configure-kong-okta-ipfs.sh

  echo 'Configure Kong to use Okta to secure IPFS'

  curl -s -k -X POST $KONG_ADMIN_URL/apis/ipfs/plugins \
      --data "name=jwt"

  # How to delete a plugin.
  # Get the plugin id from the api
  # curl -s -k -X GET $KONG_ADMIN_URL/apis/ipfs/plugins
  # Delete it from the api
  # curl -k -X DELETE $KONG_ADMIN_URL/apis/ipfs/plugins/e9522844-ef05-45b1-b3fa-09f380d4c0ec

  echo 'Export CONSUMER_ID'
  export CONSUMER_ID=$(curl -k -X POST $KONG_ADMIN_URL/consumers \
      --data "username=bob@example.com" \
      --data "custom_id=0" \
      | jq -r '.id')

  echo "Download JWT Signing Key"
  echo 'Download JWT Signing Key'
  echo 'Using vox'
  node ./scripts/okta/write-okta-pem.js

  echo "Connect the API Consumer to okta"
  echo 'Connect the API Consumer to okta'
  curl -k -X POST $KONG_ADMIN_URL/consumers/$CONSUMER_ID/jwt \
      -F "algorithm=RS256" \
      -F "rsa_public_key=@./scripts/okta/okta.pem" \
      -F "key=https://"$OKTA_HOSTNAME"/oauth2/default"

  echo "Get an okta jwt"
  echo 'Get an okta jwt'
  export ACCESS_TOKEN=$(node ./scripts/okta/get-okta-token.js)

  echo 'Get api v0 id'
  curl -k -X GET \
      --url 'http://ipfs.transmute.minikube:'$KONG_PROXY_PORT/api/v0/id \
      --header 'Authorization: Bearer '$ACCESS_TOKEN
fi

echo "Configuring the transmute framework to use the new kong proxy port in minikube"
DEBUG_MESSAGES='FATAL ERROR ON TESTING'

export OLD_KONG_PROXY_PORT=TPL_KONG_PORT_TPL
export NEW_KONG_PROXY_PORT=$KONG_PROXY_PORT

export FRAMEWORK_CONFIG=$TRANSMUTE_REPO/packages/transmute-framework/src/transmute-config/env.json.tpl
export DASHBOARD_CONFIG=$TRANSMUTE_REPO/packages/transmute-dashboard/src/transmute-config/env.json.tpl

DEBUG_MESSAGES='FATAL ERROR ON FRAMEWORK CONFIG SED'
cat $FRAMEWORK_CONFIG \
  | sed "s/$OLD_KONG_PROXY_PORT/$NEW_KONG_PROXY_PORT/g" > \
  $TRANSMUTE_REPO/packages/transmute-framework/src/transmute-config/env.json
DEBUG_MESSAGES='FATAL ERROR ON DASHBOARD CONFIG SED'
cat $DASHBOARD_CONFIG \
  | sed "s/$OLD_KONG_PROXY_PORT/$NEW_KONG_PROXY_PORT/g" > \
  $TRANSMUTE_REPO/packages/transmute-dashboard/src/transmute-config/env.json

DEBUG_MESSAGES='The Initializer has completed successfully'
