#!/bin/sh -eu

# determine directory
DIR=$(cd $(dirname "$0"); pwd)
debFileEndingsFilter=".*\.de[b]$"

# Check if current folder is clean, i.e. check for more than on package of same type
valid_packages="cvb-camerasuite-1 cvb-camerasuite-dev cvb-foundation-1 cvb-foundation-dev cvb-tools-1 cvb-tools-dev"
for current_package in $valid_packages; do
  if [ $(ls $DIR | sort | grep -i $current_package | grep -E "$debFileEndingsFilter" | wc -l) -ge "2" ] ;then
    echo WARNING!!! Multiple $current_package packages found. Please cleanup folder $DIR.
    return -1
  fi
done

# Update the package information
echo Updating the package information
apt-get update

if test $? != 0
then
    echo "Error: Please run this install.sh script with root privileges!"
    exit 1
fi

set +e
apt-get install net-tools -y
set -e


UBUNTU_VERSION=$(lsb_release -rs)
# determine architecture
if echo $(uname -m) | grep -ciq x86_64; then

  # Install codemeter package
  echo Installing Codemeter package
  apt-get install libxcb-xinerama0 -y

  # Install Codemeter Lic package
  echo Installing Codemeter package
  dpkg -i $(find $DIR -maxdepth 1 | grep -i "codemeter$debFileEndingsFilter")
else
  # Install font packages
  echo Detected Arm architecture, skipping Codemeter package.
  if [ "$(echo "$UBUNTU_VERSION >= 22" | bc -l)" -eq 1 ]; then  
    # Install renamed packages for Ubuntu 22+
    apt-get install -y curl fonts-dejavu-core fonts-dejavu-extra
  else
    apt-get install curl ttf-dejavu ttf-dejavu-core ttf-dejavu-extra fonts-dejavu -y
  fi
fi

# Install .NET Core Runtime
echo Installing .NET Core Runtime
if echo $(uname -m) | grep -ciq x86_64; then
  # install via official package source for x86_64
  package=prod.deb
  version=$(lsb_release -sr)
  url=https://packages.microsoft.com/config/ubuntu/${version}/packages-microsoft-prod.deb
  set +e
  curl --silent --show-error -L ${url} --output ${package}
  ret=$?
  set -e
  if  [ "x${ret}" != "x0" ]; then
    echo "Warning: failed to install .NET Core Runtime; check your network connection; ${url}"
  else
    dpkg -i ${package}
    rm ${package}
    apt-get update && apt-get install -y apt-transport-https dotnet-runtime-3.1
  fi
else
  # download and install for ARM platforms
  if echo $(uname -m) | grep -ciq aarch64; then
    url=https://download.visualstudio.microsoft.com/download/pr/cb0e8b4b-7b2b-40cc-b7a6-30f0d4fabe6c/f5cb06cbb1b1b5d198792333b3db235a/dotnet-runtime-3.1.26-linux-arm64.tar.gz
  else
    url=https://download.visualstudio.microsoft.com/download/pr/e6e33223-7d66-4527-a136-4cf975353bfa/1b50d103dd24165c4bb87011c4bb41d6/dotnet-runtime-3.1.26-linux-arm.tar.gz
  fi
  echo ${url}
  curl -SL -o dotnet.tar.gz ${url}
  mkdir -p /usr/share/dotnet
  tar -zxf dotnet.tar.gz -C /usr/share/dotnet
  ln -f -s /usr/share/dotnet/dotnet /usr/bin/dotnet
fi

# Install all available packages from cvb
# (NOTE: sort brings cvb-packages in correct order ... by accident, but no issue if single dpkg statement)
dpkg -i $(find $DIR -maxdepth 1 | grep -i "$debFileEndingsFilter" | sort)

echo
echo Reboot required!
