#!/bin/bash

version=1.2.4

#make sure armcord is uninstalled
if package_installed armcord ;then
  killall armcord &>/dev/null
  sudo apt purge -fy armcord
fi

#move config over from armcord to legcord
if [ -d ~/.config/armcord ] && [ ! -d ~/.config/legcord ];then
  mv ~/.config/armcord ~/.config/legcord
elif [ -d ~/.config/ArmCord ] && [ ! -d ~/.config/legcord ];then
  mv ~/.config/ArmCord ~/.config/legcord
fi

install_packages "https://github.com/Legcord/Legcord/releases/download/v${version}/Legcord-${version%-*}-linux-arm64.deb" || exit 1

#add script to run with wayland, if enabled
echo '#!/bin/bash

if [ -z "$WAYLAND_DISPLAY" ];then
  "/opt/Legcord/Legcord" "$@"
else
  "/opt/Legcord/Legcord" --enable-features=UseOzonePlatform --ozone-platform=wayland "$@"
fi
' | sudo tee '/opt/Legcord/legcord-wayland.sh' >/dev/null
sudo chmod +x '/opt/Legcord/legcord-wayland.sh'

#patch menu launcher to use wayland script
sudo sed -i 's+/opt/Legcord/Legcord +/opt/Legcord/legcord-wayland.sh +g' /usr/share/applications/Legcord.desktop
