#!/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 libobs0 package first
if package_installed libobs0t64 ;then
  echo "Removing libobs0t64 package first..."
  apt_lock_wait
  sudo apt purge -y --autoremove libobs0t64
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

if [ "$__os_codename" == "bullseye" ]; then
  if ! package_available libqt6core6 ; then
    # backports are automatically only used when required https://backports.debian.org/Instructions/
    echo 'deb http://deb.debian.org/debian bullseye-backports main contrib non-free' | sudo tee /etc/apt/sources.list.d/bullseye-backports.list
    apt_update
  fi
  #install bullseye version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-29.1.3-1-arm64-bullseye.deb || 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
elif [ "$__os_codename" == "focal" ]; then
  # install focal 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-29.1.3-1-arm64-focal.deb || exit 1
elif [ "$__os_codename" == "jammy" ]; then
  # install jammy version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-30.2.2-1-arm64-jammy.deb || exit 1
elif [ "$__os_codename" == "noble" ]; then
  # install noble version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-30.2.2-1-arm64-noble.deb || exit 1
elif [ "$__os_codename" == "bookworm" ]; then
  # install bookworm version
  install_packages qt6-qpa-plugins https://github.com/Pi-Apps-Coders/files/releases/download/large-files/obs-studio-30.2.2-1-arm64-bookworm.deb || exit 1
else
  # 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 30.2.2 ;then
    install_packages obs-studio || exit 1
  else
    error "User error: You are not using a supported Pi-Apps distribution and OBS Studio from your distribution was too old or not available."
  fi
fi

# obtain model
get_model &>/dev/null
if [[ "$model" == *"Raspberry Pi"* ]]; then
  #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
fi
exit 0
