#!/bin/bash
#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
#
# Bubble client script. Wraps the run.sh script.
#
# Usually not called directly, instead use one of the higher-level wrappers: sync-model, run-script
#
# Usage:   bubble command [args]
#
#   command        : one of the Bubble CLI commands. Find the full list of commands in BubbleMain.java
#   args           : depends on the command, usually -h / --help will show command help
#
# Environment variables
#
#   BUBBLE_API     : which API to use. Default is local (http://127.0.0.1:PORT, where PORT is found in .bubble.env)
#   BUBBLE_USER    : account to use. Default is root
#   BUBBLE_PASS    : password for account. Default is root
#   BUBBLE_ENV     : env file to load. Default is ~/.bubble.env or /home/bubble/api/bubble.env (whichever is found first)
#   DEBUG_PORT     : if set, this is the port number the client will wait for a debugger to attach before starting
#   BUBBLE_INCLUDE : when using the sync-model and run-script commands, this is the directory to find included files
#                    For sync-model and migrate-model, the default is the current directory.
#                    For run-script, the default is a directory named "tests" within the current directory
#   BUBBLE_SCRIPTS : location of run.sh script. Default is to assume it is in the same directory containing this script
#
SCRIPT="${0}"
SCRIPT_DIR=$(cd $(dirname ${SCRIPT}) && pwd)
. ${SCRIPT_DIR}/bubble_common

function is_api_command {
  case "${1}" in
    -h|--help)
      echo "0"
    ;;
    *)
      echo "1"
    ;;
  esac
}

if [[ -z "${DEBUG_PORT}" ]] ; then
  debug=""
else
  debug="debug ${DEBUG_PORT}"
fi

if [[ -z "${1}" ]] ; then
  COMMAND="-h"
else
  COMMAND="${1}"
  shift
fi

# always send help commands through
last_arg="$(echo "${@}" | awk '{print $NF}')"
is_help=0
if [[ ! -z "${last_arg}" && ( "${last_arg}" == "-h" || "${last_arg}" == "--help" ) ]] ; then
  is_help=1
fi

if [[ -z "${BUBBLE_ENV}" ]] ; then
  if [[ -f "${HOME}/.bubble.env" ]] ; then
    BUBBLE_ENV="${HOME}/.bubble.env"
  elif [[ -f "/home/bubble/api/bubble.env" ]] ; then
    BUBBLE_ENV="/home/bubble/api/bubble.env"
  else
    die "bubble environment file not found"
  fi
fi

if [[ -z "${BUBBLE_API}" ]] ; then
  if [[ "$(is_api_command ${COMMAND})" == "1" && is_help -eq 0 ]] ; then
    BUBBLE_API=local
  fi
fi
if [[ "${BUBBLE_API}" == "local" ]] ; then
  if [[ -z "${BUBBLE_PORT}" ]] ; then
    BUBBLE_PORT="$(cat ${BUBBLE_ENV} | egrep -v '\s*#' | grep BUBBLE_SERVER_PORT | awk -F '=' '{print $NF}' | tr -d "'" | tr -d '"')"
    if [[ -z "${BUBBLE_PORT}" ]] ; then
      die "Error reading BUBBLE_SERVER_PORT from ${BUBBLE_ENV}"
    fi
  fi
  BUBBLE_API="http://127.0.0.1:${BUBBLE_PORT}/api"
fi

if [[ -z "${BUBBLE_INCLUDE}" ]] ; then
  if [[ "${COMMAND}" == "sync-model" ]] ; then
    BUBBLE_INCLUDE="$(pwd)"
  elif [[ "${COMMAND}" == "script" ]] ; then
    BUBBLE_INCLUDE="$(pwd)/tests"
  else
    BUBBLE_INCLUDE="$(pwd)"
  fi
fi

BUBBLE_PASS=${BUBBLE_PASS} \
BUBBLE_USER=${BUBBLE_USER} \
BUBBLE_API=${BUBBLE_API} \
BUBBLE_INCLUDE=${BUBBLE_INCLUDE} \
  exec ${BUBBLE_SCRIPTS}/run.sh ${debug} ${COMMAND} "${@}"
