#!/bin/bash

function realpath {
  echo "$(cd "$(dirname "$1")"; pwd)"/"$(basename "$1")";
}

#---------------------------------------------------------------------------------------------------
function show_help {
  echo
  echo "Usage: $0 [OPTIONS]"
  echo
  echo "Supported options:"
  echo "  --help              - Useful information on how to run this script"
  echo "  --diagnostic-report - Generates a compressed, diagnostic log output for troubleshooting purposes."
  echo "  --reset-auth        - For standard authentication source: Resets login and password."
  echo "                        For elasticsearch-native and elasticsearch-saml authentication sources:"
  echo "                        Removes the current role mappings. All users will be granted the owner role."
  echo
}

if [ "$1" == '-h' ] || [ "$1" == '--help' ]; then
  show_help
  exit 0
fi

#---------------------------------------------------------------------------------------------------
set -eu

unset BUNDLE_APP_CONFIG
unset BUNDLE_BIN_PATH
unset BUNDLE_GEMFILE
unset BUNDLE_PATH
unset BUNDLER_VERSION
unset GEM_HOME
unset GEM_PATH
unset RUBYLIB
unset RUBYOPT

BIN_DIR=$(dirname "$0")
BIN_DIR=$(realpath "$BIN_DIR")
export APP_ROOT="$(cd "$BIN_DIR/.."; pwd)"

CONFIG_DIR="$APP_ROOT/config"
LIB_DIR="$APP_ROOT/lib"

# Need this for Filebeat config
export RAILS_ENV=loco_togo_production

# Need this because warbler uses the default one for some reason
export BUNDLE_GEMFILE=Gemfile-LocoTogo

# War file containing all of our code
export WAR_FILE="$APP_ROOT/lib/app-search.war"

# Detect the platform
PLATFORM=$(uname -s | awk '{print tolower($0)}')
ARCH=$(uname -m)
PLATFORM_FULL="$PLATFORM-$ARCH"

# Make sure the platform is supported
if [ "$PLATFORM_FULL" != "linux-x86_64" ] && [ "$PLATFORM_FULL" != "darwin-x86_64" ]; then
  echo
  echo "Elastic App Search currently only supports OSX and Linux operating systems on 64-bit platforms."
  echo
  exit 1
fi

# Filebeat locations
export FILEBEAT_BIN="$BIN_DIR/vendor/filebeat/filebeat-$PLATFORM_FULL"
export FILEBEAT_DIR="$APP_ROOT/filebeat"

#---------------------------------------------------------------------------------------------------
source "$LIB_DIR/require_java_version.sh"

# Load common environment settings
source "$CONFIG_DIR/env.sh"

REQUIRED_JAVA_OPTS="-Djruby.cli.warning.level=NIL -Dwarbler.debug=true -Djava.awt.headless=true -server"

export APP_SERVER_JAVA_OPTS="$APP_SERVER_JAVA_OPTS $REQUIRED_JAVA_OPTS"

#---------------------------------------------------------------------------------------------------
# Prepare config file
#---------------------------------------------------------------------------------------------------
if [[ -z "${APP_SEARCH_CONFIG_PATH:-}" ]]; then
  export APP_SEARCH_CONFIG_PATH="$CONFIG_DIR/app-search.yml"
fi

if [[ "$APP_SEARCH_CONFIG_PATH" != /* ]]; then
  export APP_SEARCH_CONFIG_PATH="$APP_ROOT/$APP_SEARCH_CONFIG_PATH"
fi

if [[ ! -a "$APP_SEARCH_CONFIG_PATH" ]]; then
  echo "Cannot read APP_SEARCH_CONFIG_PATH file because it does not exist, it's currently $APP_SEARCH_CONFIG_PATH"
  echo
  exit 1
fi

if [[ ! -r "$APP_SEARCH_CONFIG_PATH" ]]; then
  echo "Cannot read APP_SEARCH_CONFIG_PATH file, it's currently $APP_SEARCH_CONFIG_PATH"
  echo
  exit 1
fi

# Detect which components we are going to run, default to all
COMPONENT=${1:-all}
echo

#---------------------------------------------------------------------------------------------------
# Handle special commands
if [ "$COMPONENT" == "--reset-install-lock" ] || \
   [ "$COMPONENT" == "--reset-auth" ] || \
   [ "$COMPONENT" == "--diagnostic-report" ]; then
  APP_SEARCH_PROCESS=script exec "$_JAVA" -jar "$WAR_FILE" -S rails runner "LocoTogo.run_script!('$COMPONENT')"
  exit 0
fi

#---------------------------------------------------------------------------------------------------
# Single-process mode
#---------------------------------------------------------------------------------------------------
if [ "$COMPONENT" == "--process" ]; then
  echo "ERROR: single-process mode has been deprecated."
  echo "bin/app-search now runs worker threads and Filebeat in the background."
  show_help
  exit 1
fi

#---------------------------------------------------------------------------------------------------
# Components-based mode
#---------------------------------------------------------------------------------------------------
if [ "$COMPONENT" != 'all' ] && [ "$COMPONENT" != 'app' ]; then
  echo "ERROR: Invalid option or a component name '$COMPONENT'!"
  show_help
  exit 1
fi

echo "App Search is starting..."

echo
APP_SEARCH_PROCESS=app-server exec "$_JAVA" ${APP_SERVER_JAVA_OPTS:-} -jar "$WAR_FILE" -S rails runner 'LocoTogo.start_app_server!'
