#!/usr/bin/env bash

set -e

CURRENT_DIR=$PWD

# Magic sauce
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null && pwd )"

if [[ ! -d ${SCRIPT_DIR}/../logs ]]; then
  mkdir ${SCRIPT_DIR}/../logs
fi

JAVA_DIR=${SCRIPT_DIR}/../java
if [[ ! -d ${JAVA_DIR} ]]; then
  mkdir -p ${JAVA_DIR}
fi

# Declare the version and the platform and arch dependant URLs
JAVA_VERSION=17.0.1+12
BASE_URL_PATH=https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.1%2B12/
Linux_aarch64_Filename=OpenJDK17U-jdk_aarch64_linux_hotspot_17.0.1_12.tar.gz
Linux_x86_64_Filename=OpenJDK17U-jdk_x64_linux_hotspot_17.0.1_12.tar.gz
Darwin_x86_64_Filename=OpenJDK17U-jdk_x64_mac_hotspot_17.0.1_12.tar.gz
Darwin_arm64_Filename=OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.1_12.tar.gz

# Build a the URL
OS=`uname -s`
ARCH=`uname -m`
FILENAME="${OS}_${ARCH}_Filename"
DOWNLOAD_URL="${BASE_URL_PATH}${!FILENAME}"

# Ensure both the 'current' and 'jdk-${JAVA_VERSION}' exist, this tells us Java is setup and at the correct version
if [[ ! -e "${JAVA_DIR}/current" || ! -d "${JAVA_DIR}/jdk-${JAVA_VERSION}" ]]; then
  if [[ "${OS}" = "Darwin" ]]; then
    if [[ -e ~/dev/java/current17 ]]; then
      # Development, just sym link to our current version of Java, because only 'current' will exist in dev, we'll always rebuild the symlink.
      cd "${JAVA_DIR}"
      rm -f current
      ln -s ~/dev/java/current17 current
    else
      curl ${CURL_OPTS} "${DOWNLOAD_URL}" -o "${JAVA_DIR}/openjdk-macos-${JAVA_VERSION}.tar.gz"
      tar xfz "${JAVA_DIR}/openjdk-macos-${JAVA_VERSION}.tar.gz" -C "${JAVA_DIR}"
      cd "${JAVA_DIR}"
      rm -f current
      ln -s jdk-${JAVA_VERSION}/Contents/Home current
      rm openjdk-macos-${JAVA_VERSION}.tar.gz
    fi
  elif [[ "${OS}" = "Linux" ]]; then
    curl ${CURL_OPTS} "${DOWNLOAD_URL}" -o "${JAVA_DIR}/openjdk-linux-${JAVA_VERSION}.tar.gz"
    tar xfz "${JAVA_DIR}/openjdk-linux-${JAVA_VERSION}.tar.gz" -C "${JAVA_DIR}"
    cd "${JAVA_DIR}"
    rm -f current
    ln -s jdk-${JAVA_VERSION} current
    rm openjdk-linux-${JAVA_VERSION}.tar.gz
  fi
fi

# Set JAVA_HOME
JAVA_HOME=${JAVA_DIR}/current

# Search
echo -e "Starting fusionauth-search ... \c"
if [[ -f ${SCRIPT_DIR}/../fusionauth-search/elasticsearch/bin/elasticsearch ]]; then
  if pgrep -f fusionAuthSearchEngine87AFBG16 > /dev/null; then
    echo "skipped, already running."
  else
    cd ${SCRIPT_DIR}/../fusionauth-search/elasticsearch/bin
    nohup ./elasticsearch < /dev/null >> ${SCRIPT_DIR}/../logs/fusionauth-search.log 2>&1 &
    disown
    echo "done."
    echo "  --> Logging to ${SCRIPT_DIR}/../logs/fusionauth-search.log"
  fi
else
  echo " skipped, not installed"
fi

# App
echo -e "Starting fusionauth-app ... \c"
if [[ -f ${SCRIPT_DIR}/../fusionauth-app/apache-tomcat/bin/catalina.sh ]]; then
  cd ${SCRIPT_DIR}/../fusionauth-app/apache-tomcat/bin
  nohup ./catalina.sh start < /dev/null >> ${SCRIPT_DIR}/../logs/fusionauth-app.log 2>&1 &
  disown
  echo "done."
  echo "  --> Logging to ${SCRIPT_DIR}/../logs/fusionauth-app.log"
else
  echo " skipped, not installed"
fi

cd ${CURRENT_DIR}
