#!/bin/bash
#
# Eocortex Ubuntu Repository

UBUNTU_FILE_VERSION=$(cat /etc/os-release)
readonly UBUNTU_FILE_VERSION

sudo rm -f /etc/apt/sources.list.d/eocortex* >/dev/null

sudo apt-get update && sudo apt-get install gpg -y >/dev/null

sudo apt-get update

if echo "$UBUNTU_FILE_VERSION" | grep -q 'VERSION_ID="24.04"'; then
  sudo add-apt-repository -y ppa:dotnet/backports
fi

wget -O- https://packages.eocortex.com/deb/install/eocortex.gpg.key | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/eocortex.gpg > /dev/null

sudo tee /etc/apt/sources.list.d/eocortex.list << EOF > /dev/null
deb [arch=amd64 signed-by=/usr/share/keyrings/eocortex.gpg] https://packages.eocortex.com/deb/public main main
deb [arch=amd64 signed-by=/usr/share/keyrings/eocortex.gpg] https://packages.eocortex.com/deb/public ubuntu main
deb [arch=amd64 signed-by=/usr/share/keyrings/eocortex.gpg] https://packages.eocortex.com/deb/public ubuntu extensions
EOF

if [[ "$UBUNTU_FILE_VERSION" == *'VERSION_ID="22.04"'* ]] || [[ "$UBUNTU_FILE_VERSION" == *'VERSION_ID="24.04"'* ]]; then
  wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
  gpg --dearmor | \
  sudo tee /usr/share/keyrings/postgresql.gpg > /dev/null

  echo "deb [arch=amd64 signed-by=/usr/share/keyrings/postgresql.gpg] https://apt.postgresql.org/pub/repos/apt $(. /etc/os-release && echo $VERSION_CODENAME)-pgdg main" | \
  sudo tee /etc/apt/sources.list.d/postgresql.list > /dev/null
fi

sudo apt-get update
