#!/bin/sh

APP_ID=obm
APP_NAME=$SYNOPKG_PKGNAME
APP_BASE=$SYNOPKG_PKGDEST
APP_HOME="$SYNOPKG_PKGDEST/$APP_ID"
MACHINE_TYPE=`uname -m`
APP_VERSION=10.3.0.0

BASE_URL=`echo $CBS_URL | cut -f1 -d "?"`
URL_INPUT_ARGU=`echo $CBS_URL | cut -f2 -d "?"`
if [ "$URL_INPUT_ARGU" == "$BASE_URL" ]; then
  URL_INPUT_ARGU=""
fi
URL_VERSION_ARGU_TAG="version"
URL_ARGU=""
if [ "$URL_VERSION_ARGU_TAG" != "" ] && [ "$URL_INPUT_ARGU" != "" ]; then
  URL_ARGU="$URL_VERSION_ARGU_TAG=$APP_VERSION&$URL_INPUT_ARGU"
elif [ "$URL_INPUT_ARGU" != "" ]; then
  URL_ARGU="$URL_INPUT_ARGU"
elif [ "$URL_VERSION_ARGU_TAG" != "" ]; then
  URL_ARGU="$URL_VERSION_ARGU_TAG=$APP_VERSION"
fi

TEMP_LOC="$SYNOPKG_PKGDEST_VOL/@tmp/_$APP_NAME"
mkdir -p $TEMP_LOC

# exit the script with error code returned
# shall do cleanup before exit
exit_now()
{
  rm -Rf $TEMP_LOC
  exit $1
}

# Download and setup components
# 1 - URL
# 2 - Component name
download_component()
{
  C_URL=$1
  C_NAME=$2

  if [ ! -z "$URL_ARGU" ]; then
    C_DOWNLOAD_URL="$C_URL?$URL_ARGU"
  else
    C_DOWNLOAD_URL="$C_URL"
  fi

  C_TEMP_FILE="$TEMP_LOC/$C_NAME.tar.gz"
  C_TEMP_LOC="$TEMP_LOC/$C_NAME"
  echo "Download '$C_NAME' begin"
  wget --no-check-certificate --tries=5 $C_DOWNLOAD_URL -O $C_TEMP_FILE
  if [ $? -ne 0 ]; then
    echo "Download file from URL '$C_DOWNLOAD_URL' is failed"
    echo "Download file from URL '$C_DOWNLOAD_URL' is failed" > $SYNOPKG_TEMP_LOGFILE
    exit_now 1
  fi
  mkdir -p $C_TEMP_LOC
  tar -zxf $C_TEMP_FILE -C $C_TEMP_LOC/
  if [ $? -ne 0 ]; then
    echo "Error in reading data '$C_TEMP_FILE'"
    echo "Error in reading data '$C_TEMP_FILE'" > $SYNOPKG_TEMP_LOGFILE
    exit_now 2
  fi
  chmod -R 755 $C_TEMP_LOC
  rm -f $C_TEMP_FILE

  echo " Download completed '$C_NAME'"
}

# Download and setup prepackaged components
prepare_component()
{
  PRODUCT_NAME="$APP_NAME"

  # Define URLs of client versions
  OLD_JVM_URL="$BASE_URL/obs/download/9/jvm"
  OLD_COMPONENT_URL="$BASE_URL/obs/download/9/component"
  # [Start] 42960: Centralize getting of legacy version
  FALLBACK_VERSION="9.15.0.0"
  FALLBACK_MAJOR_VERSION=$(echo "$FALLBACK_VERSION" | cut -d'.' -f1)
  # [End] 42960

  NEW_JVM_URL="$BASE_URL/obs/download/10/jvm"
  NEW_COMPONENT_URL="$BASE_URL/obs/download/10/component"
  NEW_CLIENT_MAJOR_VERSION=$(echo "$APP_VERSION" | cut -d'.' -f1)

  # 42960 issue#4: function as legacy installer if legacy version marked in INFO file
  SYNOPKG_PKGVER_MAJOR_VERSION=$(echo "$SYNOPKG_PKGVER" | cut -d'.' -f1)

  # The JVM supported version before v10
  JAVA_VERSION="1.8"

  # [Start] 42960 issue#4: function as legacy installer if legacy version marked in INFO file
  # Decide which client version to use
  # CLIENT_MAJOR_VERSION="$FALLBACK_MAJOR_VERSION"
  # if [ "${MACHINE_TYPE}" == "x86_64" ] || [ "${MACHINE_TYPE}" == "aarch64" ]; then
  #   CLIENT_MAJOR_VERSION="$NEW_CLIENT_MAJOR_VERSION"
  #   # The JVM supported version starting v10
  #   JAVA_VERSION="21.0"
  # fi

  # if [ "$CLIENT_MAJOR_VERSION" = "$FALLBACK_MAJOR_VERSION" ]; then
  #   JVM_URL="$OLD_JVM_URL"
  #   COMPONENT_URL="$OLD_COMPONENT_URL"
  #   # [Start] 42960: Display unsupported OS message in Log Center too
  #   # 42960: Period is removed at the end because Log Center automatically adds a period at the end. Puts the period in the terminal display (echo) and Package Center popup.
  #   UNSUPPORTED_OS_MSG="$PRODUCT_NAME v$NEW_CLIENT_MAJOR_VERSION does not support this operating system version of your machine. Please go to $BASE_URL/cbs/system/ShowDownload.do to download $PRODUCT_NAME v$CLIENT_MAJOR_VERSION installer and install it instead"
  #   # Log message to terminal
  #   echo "$UNSUPPORTED_OS_MSG."
  #   # Display message as a popup in Package Center
  #   echo "$UNSUPPORTED_OS_MSG." > $SYNOPKG_TEMP_LOGFILE
  #   exit_now 8
  #   # [End] 42960
  # else
  #   JVM_URL="$NEW_JVM_URL"
  #   COMPONENT_URL="$NEW_COMPONENT_URL"
  # fi
  if [ "$SYNOPKG_PKGVER_MAJOR_VERSION" == "$FALLBACK_MAJOR_VERSION" ]; then
    JVM_URL="$OLD_JVM_URL"
    COMPONENT_URL="$OLD_COMPONENT_URL"
  else
    # The JVM supported version starting v10
    JAVA_VERSION="21.0"
    JVM_URL="$NEW_JVM_URL"
    COMPONENT_URL="$NEW_COMPONENT_URL"
    if [ "${MACHINE_TYPE}" != "x86_64" ] && [ "${MACHINE_TYPE}" != "aarch64" ]; then
      # [Start] 42960: Display unsupported OS message in Log Center too
      # 42960: Period is removed at the end because Log Center automatically adds a period at the end. Puts the period in the terminal display (echo) and Package Center popup.
      UNSUPPORTED_OS_MSG="$PRODUCT_NAME v$NEW_CLIENT_MAJOR_VERSION does not support this operating system version of your machine. Please go to $BASE_URL/cbs/system/ShowDownload.do to download $PRODUCT_NAME v$FALLBACK_MAJOR_VERSION installer and install it instead"
      # Log message to terminal
      echo "$UNSUPPORTED_OS_MSG."
      # Display message as a popup in Package Center
      echo "$UNSUPPORTED_OS_MSG." > $SYNOPKG_TEMP_LOGFILE
      exit_now 8
      # [End] 42960
    fi
  fi
  # [End] 42960 issue#4
  
  # Download $APP_ID util first for supporting scripts
  download_component "$COMPONENT_URL/util-nix-$APP_ID.tar.gz" "util-nix"
  UTIL_HOME="${TEMP_LOC}/util-nix/util"
  # Obtain JRE
  if [ "${MACHINE_TYPE}" == "x86_64" ]; then
    # Download JRE from server
    download_component "$JVM_URL/jre-std-linux-amd64.tar.gz" "jvm"
  elif [ "${MACHINE_TYPE}" == "i686" ]; then
    download_component "$JVM_URL/jre-std-linux-x86-586.tar.gz" "jvm"
  elif [ "${MACHINE_TYPE}" == "aarch64" ]; then
    download_component "$JVM_URL/jre-std-linux-arm64.tar.gz" "jvm"
  elif [ "${MACHINE_TYPE}" == "armv7l" ]; then
    download_component "$JVM_URL/jre-std-linux-arm32.tar.gz" "jvm"
  else
    # Copy JRE from the system
    source /etc/profile
    if [ ! -d "$JAVA_HOME" ]; then
      source /etc/VERSION
      echo "Java Runtime Environment not found! Please install Java $JAVA_VERSION in Package Center" > $SYNOPKG_TEMP_LOGFILE
      exit_now 3
    else
      # Just need a symlink instead of copy
      ln -s $JAVA_HOME $TEMP_LOC/jvm
      if [ $? -ne 0 ]; then
        echo "Error in reading data from '$JAVA_HOME'" > $SYNOPKG_TEMP_LOGFILE
        exit_now 4
      fi
    fi
  fi
  JAVA_TEMP="${TEMP_LOC}/jvm"
  # Verify the JAVA_EXE whether it is a valid JAVA Executable or not.
  STRING_JAVA_VERSION="java version,openjdk version"
  OUTPUT_JAVA_VERSION=`"${JAVA_TEMP}/bin/java" -version 2>&1`
  OUTPUT_JVM_SUPPORT=0
  for word in $STRING_JAVA_VERSION; do
    if [ `echo "${OUTPUT_JAVA_VERSION}" | grep "${word}" | grep -cv "grep ${word}"` -le 0 ]
    then
      continue;
    else
      OUTPUT_JVM_SUPPORT=1
      break;
    fi
  done
  if [ $OUTPUT_JVM_SUPPORT -eq 0 ]; then
    echo "The Java Executable \"${JAVA_TEMP}/bin/java\" is not a valid Java Executable." > $SYNOPKG_TEMP_LOGFILE
    exit_now 5
  fi

  # Verify if the JVM version in the JVM Home are supported
  MINIMUM_SUPPORTED_JVM_VERSION="$JAVA_VERSION"
  MAXIMUM_SUPPORTED_JVM_VERSION="$JAVA_VERSION"
  echo "Minimum supported JVM version: $MINIMUM_SUPPORTED_JVM_VERSION"
  echo "Maximum supported JVM version: $MAXIMUM_SUPPORTED_JVM_VERSION"
  [ ! -f "$UTIL_HOME/bin/verify-jvm-version.sh" ] && echo "The shell script \"$UTIL_HOME/bin/verify-jvm-version.sh\" is missing." > $SYNOPKG_TEMP_LOGFILE && exit_now 6
  ${UTIL_HOME}/bin/verify-jvm-version.sh $JAVA_TEMP $MINIMUM_SUPPORTED_JVM_VERSION $MAXIMUM_SUPPORTED_JVM_VERSION 1>/dev/null 2>&1
  JVM_VERIFIED=$?
  if [ $JVM_VERIFIED -ne 0 ]; then
    if [ $JVM_VERIFIED -eq 1 ]; then
      echo "The JVM version is lower than \"$MINIMUM_SUPPORTED_JVM_VERSION\" which is not supported by the APP." > $SYNOPKG_TEMP_LOGFILE
    elif [ $JVM_VERIFIED -eq 2 ]; then
      echo "The JVM version is greater than or equal to \"$MAXIMUM_SUPPORTED_JVM_VERSION\" which is not supported by the APP." > $SYNOPKG_TEMP_LOGFILE
    else
      echo "The JVM provided is invalid, please verify." > $SYNOPKG_TEMP_LOGFILE
    fi
    exit_now 6
  else
    echo "The current JVM version is supported by the APP"
  fi

  # # 22730: Check if Python is installed with version >= 2.0 && < 3.0
  # if [ -n "$(type python2)" ]; then
  #   echo "The current Python version is supported by the APP"
  # else
  #   echo "Supported Python not found! Please install Python in Package Centre. Supported Python version: 2.x " > $SYNOPKG_TEMP_LOGFILE
  #   exit_now 7
  # fi

  # Download other components
  # 50623: Use *-native-nix-x64.tar.gz because arm64 libraries were already moved here from *-native-nix-others.tar.gz (Task #42960)
  # if [ "${MACHINE_TYPE}" == "x86_64" ]; then
  if [ "${MACHINE_TYPE}" = "x86_64" ] || ( [ "${MACHINE_TYPE}" = "aarch64" ] && [ "${SYNOPKG_PKGVER_MAJOR_VERSION}" != "${FALLBACK_MAJOR_VERSION}" ] ); then
    download_component "$COMPONENT_URL/app-native-nix-x64.tar.gz" "nix-native"
    download_component "$COMPONENT_URL/aua-native-nix-x64.tar.gz" "aua-native"
  elif [ "${MACHINE_TYPE}" == "i686" ]; then
    download_component "$COMPONENT_URL/app-native-nix-x86.tar.gz" "nix-native"
    download_component "$COMPONENT_URL/aua-native-nix-x86.tar.gz" "aua-native"
  else
    download_component "$COMPONENT_URL/app-native-nix-others.tar.gz" "nix-native"
    download_component "$COMPONENT_URL/aua-native-nix-others.tar.gz" "aua-native"    
  fi
  download_component "$COMPONENT_URL/properties-common.tar.gz" "properties-common"
  download_component "$COMPONENT_URL/app-common.tar.gz" "app-common"
  download_component "$COMPONENT_URL/app-nas.tar.gz" "app-nas"
  # Get app-inst-nix-$APP_ID for specially branded images
  download_component "$COMPONENT_URL/app-inst-nix-$APP_ID.tar.gz" "app-inst-nix-$APP_ID"
  # 35603: create new package for custom.xml to avoid unnecessary rebuild
  download_component "$COMPONENT_URL/app-custom-common-$APP_ID.tar.gz" "app-custom-common-$APP_ID"
  # 38560: Supported AUA on NAS
  download_component "$COMPONENT_URL/aua-common.tar.gz" "aua-common"
  download_component "$COMPONENT_URL/aua-nix-obm.tar.gz" "aua-nix-obm"
  download_component "$COMPONENT_URL/aua-inst-nix-obm.tar.gz" "aua-inst-nix-obm"
}

# Prepare prepackaged components
prepare_component

exit 0
