#! /usr/bin/env bash

# setup.sh - Checks that all the required tools are present and that they are
#            appropriately configured for deploying to Triton.
#
# Adapted from https://github.com/autopilotpattern/mysql/blob/master/setup.sh
#

#
# Prelude
#
set -euo pipefail
IFS=$'\n\t'

#
# Utilities
#
die() {
  local msg="$*"
  [[ -z "${msg}" ]] || {
    echo
    tput setaf 1  # red
    tput bold
    echo "${msg}"
    tput sgr0     # reset
  }
  exit 1
}

#
# Check functions
#
ensure_command() {
  local cmd="$1"

  command -v "${cmd}" > /dev/null 2>&1 || {
    die "Couldn't find required command: ${cmd}"
  }
}

get_triton_details() {
  TRITON_USER=$(triton profile get | awk -F": " '/account:/{print $2}')
  TRITON_DC=$(triton profile get | awk -F"/" '/url:/{print $3}' | awk -F'.' '{print $1}')
  TRITON_URL=$(triton profile get | awk -F' ' '/url:/{print $2}')
  TRITON_ACCOUNT=$(triton account get | awk -F": " '/id:/{print $2}')
  TRITON_KEY=$(triton profile get | awk -F' ' '/keyId:/{print $2}')
}

check_docker_config() {
  [[ "${DOCKER_HOST:=unset}" == "unset" ]] && {
    echo "Run \"docker-compose -f local-compose.yml up\" to run locally"
    return 0
  }

  return 0
}

ensure_docker_config_matches_triton_config_and_capture_triton_details() {
  local docker_user
  docker_user=$(docker info 2>&1 | awk -F": " '/SDCAccount:/{print $2}')
  local docker_dc
  docker_dc="$(echo "${DOCKER_HOST}" | awk -F"/" '{print $3}' | awk -F'.' '{print $1}')"
  get_triton_details
  [[ ! "$docker_user" = "$TRITON_USER" ]] || [[ ! "$docker_dc" = "$TRITON_DC" ]] && {
    echo "Docker user: ${docker_user}"
    echo "Triton user: ${TRITON_USER}"
    echo "Docker data center: ${docker_dc}"
    echo "Triton data center: ${TRITON_DC}"
    die "Your Triton config does not match your Docker configuration." 
  }

  return 0;
}

ensure_triton_cns_is_enabled() {
  local triton_cns_enabled
  triton_cns_enabled=$(triton account get | awk -F": " '/cns/{print $2}')
  [[ "$triton_cns_enabled" == "true" ]] || {
    die "Triton CNS is required and not enabled."
  }
}

write_env_file() {
  if [[ -f .env ]] ; then
    echo "Env file already exists, not overwriting"
  else
    echo '# Consul discovery via Triton CNS' >> .env
    [[ "${DOCKER_HOST:=unset}" == "*docker.joyent.com*" ]] || {
      echo CONSUL="consul.svc.${TRITON_ACCOUNT}.${TRITON_DC}.cns.joyent.com" \
      >> .env
    }
    echo SDC_KEY_ID=${TRITON_KEY} >> .env
    echo SDC_ACCOUNT=${TRITON_ACCOUNT} >> .env
    echo SDC_URL=${TRITON_URL} >> .env
    echo >> .env
  fi
}

ensure_prerequisites() {
  ensure_command docker
  ensure_command docker-compose
  ensure_command triton
}

# vim: syntax=sh et ts=2 sts=2 sw=2
