#!/bin/bash

DOWNLOAD_SERVER_PATH="https://download.phytec.de/Software/Linux/"
INSTALLER_PATH="Yocto/VM_Installer/"
SOCS_FILE="SoCs.txt"
QT_PROFILE_FILE="qt-profile.tar.gz"

# download installer-script
echo "Downloading SoCs-file..."
wget "$DOWNLOAD_SERVER_PATH$INSTALLER_PATH$SOCS_FILE"
EXIT_CODE=$?

if [[ $EXIT_CODE -ne 0 ]]; then
  echo "Failed to download SoCs-file! Please check!"
  echo "Exit-Code: $EXIT_CODE"
  exit 1
fi

# read file into an array
echo "Processing file into array..."
mapfile -t SOCS < "$SOCS_FILE"

# check, if file is empty
if [[ ${#SOCS[@]} -eq 0 ]]; then
  echo "Downloaded file does not have entries. Please check!"
  exit 1
fi

echo "Choose your SoC:"
echo "----------------"

# show choices
for i in "${!SOCS[@]}"; do
  printf "%d) %s\n" "$((i + 1))" "${SOCS[$i]}"
done

# get valid input
while true; do
  echo
  read -rp "Choose by enter number: " SELECTED_NUMBER

  # check, if input is number
  if ! [[ "$SELECTED_NUMBER" =~ ^[0-9]+$ ]]; then
    echo "Please select by enter a valid number."
    continue
  fi

  # check, if input number is in range
  if (( SELECTED_NUMBER < 1 || SELECTED_NUMBER > ${#SOCS[@]} )); then
    echo "Pleased select by enter a valid number between 1 and ${#SOCS[@]}."
    continue
  fi

  break
done

#HAS_CLANG="YES"

CHOSEN_SOC="${SOCS[$((SELECTED_NUMBER - 1))]}"
echo "Selected SoC: $CHOSEN_SOC"

case "$CHOSEN_SOC" in
  imx91)
    echo "Setting up environment for latest BSP of imx91..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX91/BSP-Yocto-NXP-i.MX91-PD24.2.2/sdk/ampliphy-vendor/"
    SDK_NAME="phytec-ampliphy-vendor-glibc-x86_64-phytec-qt6demo-image-cortexa55-toolchain-5.0.8.sh"
    SDK_INSTALL_PATH="/opt/ampliphy-vendor/5.0.8/"
    ARM_OR_AARCH="aarch64"
    ;;
  imx93)
    echo "Setting up environment for latest BSP of imx93..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX93/BSP-Yocto-NXP-i.MX93-PD24.2.2/sdk/ampliphy-vendor/"
    SDK_NAME="phytec-ampliphy-vendor-glibc-x86_64-phytec-qt6demo-image-cortexa55-toolchain-5.0.8.sh"
    SDK_INSTALL_PATH="/opt/ampliphy-vendor/5.0.8/"
    ARM_OR_AARCH="aarch64"
    ;;
  imx8mm)
    echo "Setting up environment for latest BSP of imx8mm..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX8MM/BSP-Yocto-NXP-i.MX8MM-PD25.1.0/sdk/ampliphy-vendor/"
    SDK_NAME="phytec-ampliphy-vendor-glibc-x86_64-phytec-qt6demo-image-cortexa53-crypto-toolchain-5.0.8.sh"
    SDK_INSTALL_PATH="/opt/ampliphy-vendor/5.0.8/"
    ARM_OR_AARCH="aarch64"
    ;;
  imx8mp)
    echo "Setting up environment for latest BSP of imx8mp..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX8MP/BSP-Yocto-NXP-i.MX8MP-PD24.1.0/sdk/ampliphy-vendor-xwayland/"
    SDK_NAME="phytec-ampliphy-vendor-xwayland-glibc-x86_64-phytec-qt6demo-image-cortexa53-crypto-toolchain-5.0.4.sh"
    SDK_INSTALL_PATH="/opt/ampliphy-vendor-xwayland/5.0.4/"
    ARM_OR_AARCH="aarch64"
    ;;
  imx6)
    echo "Setting up environment for latest BSP of imx6..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX6/BSP-Yocto-Ampliphy-i.MX6-PD25.1.0/sdk/ampliphy/"
    SDK_NAME="phytec-ampliphy-glibc-x86_64-phytec-qt6demo-image-cortexa9t2hf-neon-toolchain-BSP-Yocto-Ampliphy-i.MX6-PD25.1.0.sh"
    SDK_INSTALL_PATH="/opt/ampliphy/BSP-Yocto-Ampliphy-i.MX6-PD25.1.0/"
    ARM_OR_AARCH="arm"
    #HAS_CLANG="NO"
    ;;
  imx6ul)
    echo "Setting up environment for latest BSP of imx6ul..."
    SDK_DOWNLOAD_PATH="BSP-Yocto-i.MX6UL/BSP-Yocto-Ampliphy-i.MX6UL-PD24.1.0/sdk/ampliphy/"
    SDK_NAME="phytec-ampliphy-glibc-x86_64-phytec-qt5demo-image-cortexa7t2hf-neon-vfpv4-toolchain-BSP-Yocto-Ampliphy-i.MX6UL-PD24.1.0.sh"
    SDK_INSTALL_PATH="/opt/ampliphy/BSP-Yocto-Ampliphy-i.MX6UL-PD24.1.0/"
    ARM_OR_AARCH="arm"
    #HAS_CLANG="NO"
    ;;
  *)
    echo "Unknown choice: $CHOSEN_SOC"
    exit 1
    ;;
esac

# download SDK of choosen SoC
echo "Downloading SDK..."
wget "$DOWNLOAD_SERVER_PATH$SDK_DOWNLOAD_PATH$SDK_NAME"
EXIT_CODE=$?

if [[ $EXIT_CODE -ne 0 ]]; then
  echo "Failed to download SDK! Please check!"
  echo "Exit-Code: $EXIT_CODE"
  exit 1
fi

# make SDK executable and install it to default path
chmod +x "$SDK_NAME"
EXIT_CODE=$?

if [[ $EXIT_CODE -ne 0 ]]; then
  echo "Failed to make SDK excecutable! Please check!"
  echo "Exit-Code: $EXIT_CODE"
  exit 1
fi
echo "Installing SDK..."
./$SDK_NAME -y

# download default QtProfile
echo "Downloading default QtProfile..."
wget "$DOWNLOAD_SERVER_PATH$INSTALLER_PATH$QT_PROFILE_FILE"
EXIT_CODE=$?

if [[ $EXIT_CODE -ne 0 ]]; then
  echo "Failed to download QtProfile! Please check!"
  echo "Exit-Code: $EXIT_CODE"
  exit 1
fi

# unpack default QTtProfile
echo "Unpacking default QtProfile..."
echo "$QT_PROFILE_FILE"
tar -xzf "./$QT_PROFILE_FILE"
EXIT_CODE=$?

if [[ $EXIT_CODE -ne 0 ]]; then
  echo "Failed to unpack default QtProfile! Please check!"
  echo "Exit-Code: $EXIT_CODE"
  exit 1
fi

# copy default QtProfile
echo "Deploy automatic selected QtProfile..."
cp -r "./QtProject" "/home/phyvm/.config/"
echo "Deploy HelloQuick configuration..."
cp "./HelloQuick.pro.user" "/home/phyvm/Schreibtisch/PHYTEC_Tools/HelloQuick/"

SDK_BIN_PATH="$SDK_INSTALL_PATH""sysroots/x86_64-phytecsdk-linux/usr/bin/"

SDK_G_XCOMPILER="$ARM_OR_AARCH""-phytec-linux"
SDK_G_XCOMPILER_PATH="$SDK_BIN_PATH$ARM_OR_AARCH""-phytec-linux/""$SDK_G_XCOMPILER"
SDK_G_MUSL_XCOMPILER_PATH="$SDK_BIN_PATH$ARM_OR_AARCH""-phytec-linux-musl/""$SDK_G_XCOMPILER"

# replace SDK paths within QtProfile
cd "/home/phyvm/.config/QtProject/"
echo "Configure SDK paths in QtProfile..."
sed -i -E "s|/opt/ampliphy(.*)/sysroots/x86_64-phytecsdk-linux/usr/bin/(.*)-phytec-linux/(.*)-phytec-linux|$SDK_G_XCOMPILER_PATH|g" $(find . -type f)
sed -i -E "s|/opt/ampliphy(.*)/sysroots/x86_64-phytecsdk-linux/usr/bin/(.*)-phytec-linux-musl/(.*)-phytec-linux|$SDK_G_MUSL_XCOMPILER_PATH|g" $(find . -type f)
sed -i -E "s|/opt/ampliphy(.*)/sysroots/x86_64-phytecsdk-linux/usr/bin/|$SDK_BIN_PATH|g" $(find . -type f)

# TODO
# i.MX6 and i.MX6UL do not have clang at all
# for a clean solution, all entries for clang in the QtProfile should be removed
# this is a more difficult step and should only be done, if it is needed

echo "Configure SDK paths in QtProfile complete."

echo "VM setum complete."

echo "Cleaning workspace..."
cd -
rm -rf ./$QT_PROFILE_FILE ./QtProject ./HelloQuick.pro.user ./$SDK_NAME ./$SOCS_FILE 




