#!/bin/bash

# install box86
"${DIRECTORY}/manage" install-if-not-installed Box86 || error "Box86 failed to install somehow!"
# install box64
"${DIRECTORY}/manage" install-if-not-installed Box64 || error "Box64 failed to install somehow!"

# add additional packages not already installed by box86/box64
install_packages libnss3:armhf libnm0:armhf libdbus-glib-1-2:armhf libnspr4:armhf libgudev-1.0-0:armhf libxtst6:armhf libsm6:armhf libice6:armhf libusb-1.0-0:armhf libnss3 libnm0 libdbus-glib-1-2 libudev1 libnspr4 libgudev-1.0-0 libxtst6 libsm6 libice6 libusb-1.0-0 libibus-1.0-dev || exit 1

# perform an apt upgrade to make sure the latest version of box64 and box86 are installed
echo "Upgrading Box86 and Box64 if necessary:"
apt_lock_wait
sudo apt install --only-upgrade box86-* box64-* -y

if package_installed steam-devices ; then
  echo "Removing steam-devices package because it conflicts with steam-launcher..."
  apt_lock_wait
  sudo apt purge -y steam-devices
fi

if package_installed steamlink ; then
  echo "Removing steamlink package because it conflicts with steam-launcher..."
  apt_lock_wait
  sudo apt purge -y steamlink
fi

#prevent lxterm dependency from becoming default terminal
default_terminal="$(readlink -f /usr/bin/x-terminal-emulator)"

#use latest steam deb and simply patch the control file to use the `all` dpkg architecture definition
wget https://repo.steampowered.com/steam/archive/stable/steam_latest.deb -O /tmp/steam_latest.deb || exit 1
patch_deb_sed /tmp/steam_latest.deb 's/Architecture: amd64/Architecture: all/g' || exit 1
install_packages /tmp/steam_latest.deb || exit 1

#check if terminal choice was changed after steam was installed
if [ ! -z "$default_terminal" ] && [ "$(readlink -f /usr/bin/x-terminal-emulator)" != "$default_terminal" ];then
  #restore previous choice
  sudo update-alternatives --set x-terminal-emulator "$default_terminal"
fi

sudo mkdir -p /usr/local/bin /usr/local/share/applications
# if a matching name binary is found in /usr/local/bin it takes priority over /usr/bin
echo '#!/bin/bash
export STEAMOS=1
export STEAM_RUNTIME=1
export DBUS_FATAL_WARNINGS=0
BOX64_LOG=1 BOX86_LOG=1 BOX64_TRACE_FILE=stderr BOX86_TRACE_FILE=stderr BOX64_EMULATED_LIBS=libmpg123.so.0 /usr/lib/steam/bin_steam.sh -no-cef-sandbox steam://open/minigameslist "$@"

rm -f /home/${USER}/Desktop/steam.desktop' | sudo tee /usr/local/bin/steam >/dev/null || error "Failed to create steam launch script"

# set execution bit
sudo chmod +x /usr/local/bin/steam

#remove crashhandler.so from archive that is extracted to ~/.local/share/Steam/ubuntu12_32/crashhandler.so
#fixes first launch error
status -n "Patching bootstraplinux_ubuntu12_32.tar.xz to fix launch crash... "
rm -rf /tmp/bootstraplinux_ubuntu12_32
mkdir -p /tmp/bootstraplinux_ubuntu12_32 || error "failed to make temp folder /tmp/bootstraplinux_ubuntu12_32"
tar -xf '/usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz' -C /tmp/bootstraplinux_ubuntu12_32 || error "failed to extract bootstraplinux_ubuntu12_32.tar.xz"
rm -f /tmp/bootstraplinux_ubuntu12_32/ubuntu12_32/crashhandler.so || error "failed to remove crashhandler.so"
#overwrite the archive (changes will be overwritten on steam.deb update, but this is only needed once for first setup to not fail)
sudo tar -cJf '/usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz' -C /tmp/bootstraplinux_ubuntu12_32 . || error "failed to compress new bootstraplinux_ubuntu12_32.tar.xz"
rm -rf ~/.local/share/Steam/ubuntu12_32/crashhandler.so /tmp/bootstraplinux_ubuntu12_32 #remove it in case it already exists
status_green "Done"

# move official steam.desktop file to /usr/local and edit it (move it so users ignoring the reboot warning cannot find the wrong launcher)
# we can't edit the official steam.desktop file since this will get overwritten on a steam update (steam adds its own apt repo)
# if a matching name .desktop file is found in /usr/local/share/applications it takes priority over /usr/share/applications
if [ ! -f /usr/local/share/applications/steam.desktop ];then
  #fix issue where running this install script twice would create a self-referencing symlink and overwrite the actual text file
  sudo mv -f /usr/share/applications/steam.desktop /usr/local/share/applications/steam.desktop
fi
sudo sed -i 's:Exec=/usr/bin/steam:Exec=/usr/local/bin/steam:' /usr/local/share/applications/steam.desktop
#symlink to original location to prevent first-run steam exit when file is missing, and to prevent /usr/local/share in PATH from being a hard requirement (Chances are the user will reboot before steam.deb gets an update)
sudo ln -s /usr/local/share/applications/steam.desktop /usr/share/applications/steam.desktop

rm -f $HOME/Desktop/steam.desktop

status "Steam should be able to launch right now from the menu, but if it fails, try rebooting."
