#!/usr/bin/env bash
#
# Download JetBrains JDK including JCEF for development & deployment.
#
# -------------------------------------------------------------------
#
# Copyright (c) 2024-2025 Andreas Rudolph <andy@openindex.de>.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

#
# The application uses the Jetbrains JDK bundled with JCEF.
#
# see https://github.com/JetBrains/JetBrainsRuntime
# see https://github.com/JetBrains/JetBrainsRuntime/releases
#

JBR_VERSION="21.0.6b825.69"

set -e
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

JDK_DIR="${BASE_DIR}/jdk"
mkdir -p "${JDK_DIR}"
cd "${JDK_DIR}"

JDK_VERSION="$(echo "${JBR_VERSION}" | cut -d "b" -f 1)"
#echo "${JDK_VERSION}"
JDK_BUILD="$(echo "${JBR_VERSION}" | cut -d "b" -f 2)"
#echo "${JDK_BUILD}"

case "$(uname -s)" in
  Linux*)     SYSTEM="linux";;
  Darwin*)    SYSTEM="osx";;
  CYGWIN*)    SYSTEM="windows";;
  MINGW*)     SYSTEM="windows";;
  *)          SYSTEM=""
esac
if [ -z "${SYSTEM}" ]; then
  echo "Unsupported operating system: $(uname -s)"
  exit 1
fi

case "$(uname -m)" in
  aarch64*)   ARCH="aarch64";;
  arm64*)     ARCH="aarch64";;
  x86_64*)    ARCH="x64";;
  *)          ARCH=""
esac
if [ -z "${ARCH}" ]; then
  echo "Unsupported system architecture: $(uname -m)"
  exit 1
fi

echo ""
echo "==================================================================="
echo ""
echo "Init JetBrains JDK for \"${SYSTEM}-${ARCH}\"..."

# Download links at https://github.com/JetBrains/JetBrainsRuntime/releases
BASE_NAME="jbrsdk_jcef-${JDK_VERSION}-${SYSTEM}-${ARCH}-b${JDK_BUILD}"
DOWNLOAD_URL="https://cache-redirector.jetbrains.com/intellij-jbr/${BASE_NAME}.tar.gz"
DOWNLOAD_DIR="${JDK_DIR}/${BASE_NAME}"
DOWNLOAD_FILE="${JDK_DIR}/${BASE_NAME}.tar.gz"

if [ ! -d "${DOWNLOAD_DIR}" ]; then
  if [ ! -f "${DOWNLOAD_FILE}" ]; then
    echo "Downloading from \"${DOWNLOAD_URL}\"..."
    wget -q "${DOWNLOAD_URL}"
  fi

  echo "Extracting archive..."
  tar xfz "${DOWNLOAD_FILE}"
  if [ ! -d "${DOWNLOAD_DIR}" ]; then
    echo "Expected JDK directory does not exist at \"${DOWNLOAD_DIR}\"!"
    exit 1
  fi

  # Remove Apple quarantine attributes. Not sure if necessary, but it doesn't hurt.
  if [ "${SYSTEM}" = "osx" ]; then
    echo "Remove Apple quarantine attributes. This might require authentication as admin user..."
    sudo xattr -r -d com.apple.quarantine "${DOWNLOAD_DIR}"
  fi
fi

echo "Writing environment variables..."
JAVA_HOME="${DOWNLOAD_DIR}"
if [ "${SYSTEM}" = "osx" ]; then
  JAVA_HOME="${JAVA_HOME}/Contents/Home"
fi

echo "#" > "${BASE_DIR}/env.sh"
# shellcheck disable=SC2129
echo "# This file was automatically generated by \"init-jdk.sh\"." >> "${BASE_DIR}/env.sh"
echo "# It is automatically updated, if \"init-jdk.sh\" is executed." >> "${BASE_DIR}/env.sh"
echo "#" >> "${BASE_DIR}/env.sh"
echo "" >> "${BASE_DIR}/env.sh"
echo "export JAVA_HOME=\"${JAVA_HOME}\"" >> "${BASE_DIR}/env.sh"
echo "export PATH=\"${JAVA_HOME}/bin:${PATH}\"" >> "${BASE_DIR}/env.sh"
chmod ugo+x "${BASE_DIR}/env.sh"

echo ""
echo "==================================================================="
echo ""
echo "JetBrains JDK was downloaded into:"
echo ""
echo "${DOWNLOAD_DIR}"
echo ""
echo "Please update your JAVA_HOME and IDE settings accordingly or use:"
echo ""
echo "${BASE_DIR}/env.sh"
echo ""
echo "==================================================================="
echo ""
