#!/bin/bash

version=antigravity_1.23.2-1776332230_arm64_cdd44fd493c967bb35c13b3b2c49e726.deb

sudo modprobe kvm || true
if [ ! -e /dev/kvm ] && [ "$GITHUB_ACTIONS" != "true" ];then
  error "Unfortunately, your system is not compatible with this app. Your kernel is missing the KVM module. Please switch to a kernel that has this feature, or ask for help."
fi

status -n "Adding the Google Antigravity repository..."

# Create the keyrings directory
sudo mkdir -p /etc/apt/keyrings || error 'Failed to create keyrings directory.'

# Download and de-armor the GPG key
curl -fsSL https://us-central1-apt.pkg.dev/doc/repo-signing-key.gpg | \
  sudo gpg --dearmor --yes -o /etc/apt/keyrings/antigravity-repo-key.gpg \
  || error 'Error downloading the GPG key.'

# Add the repository source to sources.list.d
echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/antigravity-repo-key.gpg] https://us-central1-apt.pkg.dev/projects/antigravity-auto-updater-dev/ antigravity-debian main" | \
  sudo tee /etc/apt/sources.list.d/antigravity.list > /dev/null \
  || error 'Error adding the repository source.'

status_green Done

install_packages qemu-system-arm ipxe-qemu antigravity git bc bison flex libssl-dev make gcc libelf-dev socat || exit 1

#fixes error when running /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm on any kernel with a 39bit address space: (all pi kernels except for the pi5's 16k pagesize kernel)
#third_party/tcmalloc/internal/system_allocator.h:589] MmapAligned() failed - unable to allocate with tag (hint=0x2e6680000000, size=1073741824, alignment=1073741824) - is something limiting address placement?
#third_party/tcmalloc/internal/system_allocator.h:596] Note: the allocation may have failed because TCMalloc assumes a 48-bit virtual address space size; you may need to rebuild TCMalloc with TCMALLOC_ADDRESS_BITS defined to your system's virtual address space size
#third_party/tcmalloc/arena.cc:60] CHECK in Alloc: FATAL ERROR: Out of memory trying to allocate internal tcmalloc data (bytes=131072, object-size=16384); is something preventing mmap from succeeding (sandbox, VSS limitations)?

#we have to run the binary under a compatible 48bit kernel, using kvm.
#Then plumb its stdin, stdout, network communication, socket files, and a fifo, all back to the host OS.
#this was beyond my human capability to figure out, and only possible because of ai. (pretty ironic, given the app's purpose) 

cd /tmp
git_clone https://github.com/Botspot/antigravity-48bit-workaround || exit 1

#prevent the language server binary from being deleted in the upcoming rm -rf command
if [ -f /usr/share/antigravity/resources/antigravity-48bit-workaround/language_server_linux_arm ] && ([ ! -f /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ] || [ -h /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ]);then
  #temporarily move it back to its original location
  sudo mv -f /usr/share/antigravity/resources/antigravity-48bit-workaround/language_server_linux_arm /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm
elif [ -h /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ] || [ ! -f /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ];then
  #if the file is entirely missing, or already a symlink, reinstall the package
  status "Working around a situation where the language server binary somehow got deleted..."
  sudo apt update
  sudo apt install --reinstall antigravity -y || error "User error: Tried to work around an edge case caused by an earlier script version, and for whatever reason, apt failed to reinstall antigravity. Please uninstall Antigravity and install it again from Pi-Apps manually."
fi

sudo rm -rf "/usr/share/antigravity/resources/antigravity-48bit-workaround"
sudo mkdir -p "/usr/share/antigravity/resources/antigravity-48bit-workaround"

#move problematic language server binary
if [ ! -h /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ];then
  #only do this if it's the actual binary, not the symbolic link created from a previous install
  sudo mv -f /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm /usr/share/antigravity/resources/antigravity-48bit-workaround
elif [ -h /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm ] && [ -h /usr/share/antigravity/resources/antigravity-48bit-workaround/language_server_linux_arm ];then
  #workaround error caused before this if statement was added, whereby the binary was deleted and the symbolic link is now in both places
  status "Working around a bug caused by an earlier version of this script..."
  sudo rm -f /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm /usr/share/antigravity/resources/antigravity-48bit-workaround/language_server_linux_arm
  sudo apt update
  sudo apt install --reinstall antigravity -y || error "User error: Tried to work around an edge case caused by an earlier script version, and for whatever reason, apt failed to reinstall antigravity. Please uninstall Antigravity and install it again from Pi-Apps manually."
  sudo mv -f /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm /usr/share/antigravity/resources/antigravity-48bit-workaround
fi

sudo cp /tmp/antigravity-48bit-workaround/language-server-wrapper.sh /usr/share/antigravity/resources/antigravity-48bit-workaround/language-server-wrapper.sh
sudo cp /tmp/antigravity-48bit-workaround/guest-wrapper.sh /usr/share/antigravity/resources/antigravity-48bit-workaround/guest-wrapper.sh
sudo cp /tmp/antigravity-48bit-workaround/linux-kernel-image /usr/share/antigravity/resources/antigravity-48bit-workaround/linux-kernel-image
rm -rf /tmp/antigravity-48bit-workaround

sudo ln -sf /usr/share/antigravity/resources/antigravity-48bit-workaround/language-server-wrapper.sh /usr/share/antigravity/resources/app/extensions/antigravity/bin/language_server_linux_arm || error "failed to make symbolic link!"

#now that antigravity is installed, prevent it from updating itself with apt, as this would break our language server wrapper.
#pi-apps will handle updates instead
sudo rm -f /etc/apt/sources.list.d/antigravity.list
