#!/bin/bash

#remove obs-studio package first
if package_installed obs-studio ;then
  echo "Removing obs-studio package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove obs-studio
fi

#remove obs package first
if package_installed obs ;then
  echo "Removing obs package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove obs
fi

#remove libobs0 package first
if package_installed libobs0 ;then
  echo "Removing libobs0 package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs0
fi

#remove libobs-dev package first
if package_installed libobs-dev ;then
  echo "Removing libobs-dev package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs-dev
fi

# note: bump this version to whatever the version of OBS-Studio we have precompiled debs for
# if the distro version is equal or newer, prefer it to our debs
# perform the package_is_new_enough check after purging obs-studio to prevent accidentally checking our own precompiled deb if it happens to still be installed 
if package_is_new_enough obs-studio 27.2.0 ;then
  install_packages obs-studio || exit 1
  exit 0
fi

if [ "$__os_codename" == "bullseye" ]; then
  #install bullseye version
  install_packages http://ftp.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac2_2.0.1-1_arm64.deb http://ftp.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac-dev_2.0.1-1_arm64.deb https://github.com/ryanfortner/ryanfortner/releases/download/1002/obs-studio_27.2.0-296-gc5154cc31-bullseye_arm64.deb libmbedtls-dev libasound2-dev libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libcurl4-openssl-dev libfontconfig1-dev libfreetype6-dev libgl1-mesa-dev libjack-jackd2-dev libjansson-dev libluajit-5.1-dev libpulse-dev libqt5x11extras5-dev libspeexdsp-dev libswresample-dev libswscale-dev libudev-dev libv4l-dev libvlc-dev libx11-dev libx11-xcb1 libx11-xcb-dev libxcb-xinput0  libxcb-randr0 libxcb-shm0 libxcb-xfixes0 libxcb-xinerama0 libxcb1 libxcb-xinput-dev libxcb-damage0 libxcb-ewmh2 libxcb-xtest0 libxdamage1 libx264-dev libxcomposite1 libxext6 libxfixes3 libxkbcommon0 libxrandr2 libxshmfence1 pkg-config python3-dev qtbase5-dev libqt5svg5-dev swig "libxcb-util1 | libxcb-util0" || exit 1
elif [ "$__os_codename" == "bionic" ]; then
  # install bionic version
  apt_lock_wait
  ubuntu_ppa_installer "okirby/qt6-backports"

  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-28.1.2-1-arm64-bionic.deb || exit 1
  exit 0
else 
  #install buster version
  install_packages http://ftp.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac2_2.0.1-1_arm64.deb http://ftp.debian.org/debian/pool/non-free/f/fdk-aac/libfdk-aac-dev_2.0.1-1_arm64.deb https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio_27.2.4_arm64-buster.deb libmbedtls-dev libasound2-dev libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libcurl4-openssl-dev libfontconfig1-dev libfreetype6-dev libgl1-mesa-dev libjack-jackd2-dev libjansson-dev libluajit-5.1-dev libpulse-dev libqt5x11extras5-dev libspeexdsp-dev libswresample-dev libswscale-dev libudev-dev libv4l-dev libvlc-dev libx11-dev libx11-xcb1 libx11-xcb-dev libxcb-xinput0  libxcb-randr0 libxcb-shm0 libxcb-xfixes0 libxcb-xinerama0 libxcb1 libxcb-xinput-dev libxcb-damage0 libxcb-ewmh2 libxcb-xtest0 libxdamage1 libx264-dev libxcomposite1 libxext6 libxfixes3 libxkbcommon0 libxrandr2 libxshmfence1 pkg-config python3-dev qtbase5-dev libqt5svg5-dev swig "libxcb-util1 | libxcb-util0" || exit 1
fi

#Create workaround wrapper script to override MESA_GL_VERSION
file -bL --mime /usr/bin/obs | grep -q "binary" && sudo mv /usr/bin/obs /usr/bin/obs-exec
echo '#!/bin/bash
LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libGL.so MESA_GL_VERSION_OVERRIDE=3.3 /usr/bin/obs-exec "$@"' | sudo tee /usr/bin/obs >/dev/null
sudo chmod +x /usr/bin/obs
