#!/bin/sh
sudo apt update
# I keep having conflicts when trying to install gnupg, so instead of the steps to generate the icecat source tarball I'm including a premade one, from an instance where I was able to install gnupg by other means
#sudo apt install git gnupg
#git clone https://https.git.savannah.gnu.org/git/gnuzilla.git
#cd gnuzilla
#./makeicecat
#cd ..
wget https://ion.nu/hurd_icecat/icecat-140.8.0-2gnu1.tar.bz2
wget https://ion.nu/hurd_icecat/icecat.patch
sudo apt install llvm pkg-config libnspr4-dev rustc cbindgen clang libclang-dev libgtk-3-dev unzip libx11-xcb-dev nasm
# tar -xjf gnuzilla/output/icecat-140.8.0-2gnu1.tar.bz2 
tar -xjf icecat-140.8.0-2gnu1.tar.bz2 

# Patch /usr/lib/python3.13/multiprocessing/connection.py commenting out setsockopt and set _address directly (Better solution to send upstream: https://ion.nu/tmp/hurd_python.patch )
sudo sed -i -e "/os.name == 'posix':/{s@.*@#&@;n;s@.*@#&@;n;s@.*@#&@;}; s@_address = self@_address = address # self@" /usr/lib/python3.13/multiprocessing/connection.py
# Apply patches
patch -i icecat.patch

# This was an attempt at getting around the rustc memory allocation issue ( https://ion.nu/tmp/rustc_mem.txt or sometimes it just says "memory allocation of 11665424 bytes failed") which seems primarily triggered by glean, but this probably creates more problems down the line
#mv icecat-140.8.0/toolkit/components/glean/api/src{,.bak}
#mkdir icecat-140.8.0/toolkit/components/glean/api/src
#touch icecat-140.8.0/toolkit/components/glean/api/src/lib.rs

mkdir icecat-build
cd icecat-build
../icecat-140.8.0/configure --prefix="${HOME}/.local" --enable-audio-backends="oss" --without-wasm-sandboxed-libraries --with-system-nspr --disable-nodejs
make
