FROM --platform=linux/amd64 debian:bullseye AS amd64-base

############## Prepare for hell ##############

RUN apt-get update

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
    build-essential autoconf automake bison flex gawk \
    libtool libtool-bin libncurses-dev \
    curl file git gperf help2man texinfo unzip wget

# Python dev deps for --with-build-python
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
    make libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
    libsqlite3-dev llvm libncurses5-dev libncursesw5-dev \
    xz-utils tk-dev liblzma-dev tk-dev libffi-dev

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y \
    pkg-config

WORKDIR /root

RUN wget -qO- https://www.python.org/ftp/python/3.11.11/Python-3.11.11.tgz | tar xvz

RUN cp -r Python-3.11.11 Python-3.11.11-armhf

RUN cd Python-3.11.11 && \
    ./configure --prefix=/usr/local/python-native --enable-optimizations --with-lto --with-computed-gotos --with-system-ffi && \
    make -j$(nproc) && \
    make install

RUN wget -qO- https://github.com/KindleModding/koxtoolchain/releases/download/2025.01/kindlehf.tar.gz | tar xvz && \
    wget -qO- https://github.com/KindleModding/koxtoolchain/raw/27f361f5d5e1b85b6d02978a6b45271a2605019c/refs/x-compile.sh > x-compile.sh

RUN mkdir -p /mnt/us/python3





############## Python build hell ##############

COPY exec /root/exec
RUN chmod +x /root/exec

RUN wget http://http.us.debian.org/debian/pool/main/p/python3.11/libpython3.11-dev_3.11.2-6+deb12u5_armhf.deb
RUN dpkg -x libpython3.11-dev_3.11.2-6+deb12u5_armhf.deb libpython3.11-dev

RUN git clone --branch v1.25.0 --recurse-submodules https://github.com/NiLuJe/FBInk.git
RUN cd FBInk && \
    bash -c "source /root/exec && \
    make -j$(nproc) shared KINDLE=1"

RUN wget -qO- https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz | tar xvz
RUN cd zlib-1.3.1 && \
    bash -c "source /root/exec && \
    ./configure --prefix=/usr && \
    make -j$(nproc) && \
    make install DESTDIR=\$SYSROOT"

RUN wget -qO- https://github.com/libffi/libffi/releases/download/v3.4.6/libffi-3.4.6.tar.gz | tar xvz
RUN cd libffi-3.4.6 && \
    bash -c "source /root/exec && \
    ./configure --prefix=/usr --host=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu && \
    make -j$(nproc) && \
    make install DESTDIR=\$SYSROOT"

RUN wget -qO- https://github.com/openssl/openssl/archive/refs/tags/OpenSSL_1_1_1w.tar.gz | tar xvz
RUN cd openssl-OpenSSL_1_1_1w && \
    bash -c "source /root/exec && \
    ./Configure linux-armv4 --prefix=/usr --openssldir=/etc/ssl -DOPENSSL_PIC -DOTHER_LIBS=atomic -lpthread -latomic && \
    make -j$(nproc) && \
    make install DESTDIR=\$SYSROOT"

RUN wget -qO- https://www.sqlite.org/2025/sqlite-autoconf-3490100.tar.gz | tar xvz 
RUN cd sqlite-autoconf-3490100 && \
    bash -c "source /root/exec && \
    ./configure --prefix=/usr --host=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu && \
    make -j$(nproc) && \
    make install DESTDIR=\$SYSROOT"

RUN wget -qO- https://ftp.gnu.org/gnu/ncurses/ncurses-6.4.tar.gz | tar xvz 
RUN cd ncurses-6.4 && \
    bash -c "source /root/exec && \
    ./configure --prefix=/usr --host=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu --with-shared --disable-stripping && \
    make -j$(nproc) && \
    make install DESTDIR=\$SYSROOT"

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y cmake
RUN git clone --branch libjpeg-turbo-3.0.3 --recurse-submodules https://github.com/winlibs/libjpeg.git
RUN cd libjpeg && \
    bash -c "source /root/exec && \
    cmake -G\"Unix Makefiles\" -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_BUILD_TYPE=Release . && \
    make -j$(nproc) && \ 
    make install DESTDIR=\$SYSROOT"

RUN cd Python-3.11.11-armhf && \
    bash -c "source /root/exec && \
    ./configure --prefix=/usr --host=arm-linux-gnueabihf --build=x86_64-pc-linux-gnu --with-build-python=/usr/local/python-native/bin/python3.11 --enable-shared --enable-optimizations --enable-loadable-sqlite-extensions --with-lto --with-computed-gotos --with-system-ffi --disable-ipv6 --disable-test-modules ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no && \
    make -j$(nproc) && \
    make altinstall DESTDIR=\$SYSROOT"





############## Python Package Hell ##############

# Build fbink binding (_fbink.so)
COPY fbink_build.py /root/fbink_build.py
RUN /usr/local/python-native/bin/python3.11 -m pip install cffi
RUN bash -c "source /root/exec && cd /root && \
    /usr/local/python-native/bin/python3.11 fbink_build.py"
RUN cp /root/FBInk/Release/libfbink.so.1.0.0 /mnt/us/python3/usr/lib/libfbink.so.1

# install python packages using crossenv
COPY pyexec /root/pyexec
RUN chmod +x /root/pyexec
COPY create_py_crossenv.sh /root/create_py_crossenv.sh
COPY install_py_packages.sh /root/install_py_packages.sh
COPY pypackages.txt /root/pypackages.txt
COPY meson_cross_file.txt /root/meson_cross_file.txt
RUN chmod +x /root/create_py_crossenv.sh /root/install_py_packages.sh

# install crossenv stuff and python packages for kindle
RUN bash -c "source /root/pyexec && /root/create_py_crossenv.sh"
# install build dependencies
RUN bash -c "source /root/pyexec && build-pip install -v \
    cmake \
    scikit-build \
    ninja \
    patchelf \
    cython \
    numpy \
    wheel \
    setuptools \
    meson-python \
    meson"

# Fuckin send it
RUN bash -c "source /root/pyexec && /root/install_py_packages.sh"

# Pillow is cringe why can't you handle library includes properly
RUN git clone --branch 11.1.0 --recurse-submodules https://github.com/python-pillow/Pillow.git
COPY pillow-setup.patch /root/Pillow/pillow-setup.patch
RUN cd Pillow && \
    bash -c "source /root/pyexec && git apply pillow-setup.patch && \
    cross-python setup.py build_ext --library-dirs \$SYSROOT/usr/lib --include-dirs \$SYSROOT/usr/include --libraries jpeg --build-lib \$SYSROOT/usr/lib/python3.11/site-packages && \
    cross-python setup.py build_py -d \$SYSROOT/usr/lib/python3.11/site-packages && \
    cross-python setup.py install_egg_info --install-dir \$SYSROOT/usr/lib/python3.11/site-packages"



######## Trim build size section ########

# Strip binaries to reduce size
RUN bash -c "source /root/exec && find \$SYSROOT -type f \( -name '*.a' -o -name '*.o' -o -name '*.so' -o -perm /111 \) \
    -exec \$STRIP --strip-unneeded {} + 2>/dev/null || true"

# Dereference all links (i know this inflates the size but stfu for a second and watch)
# we do this because of amazon's genius by making the /mnt/us partition some dogshit filesystem that can't handle links
# so instead we dereference all the links and then look for which libraries are actually being used and keep those
# it's fucking genius ok?
# unless there's a way to prevent make from creating a shit load of symlinks
# in which case this was dumb as fuck but i got AI to generate the analyze_used_libraries.sh code and it worked lmao ¯\_(ツ)_/¯
RUN bash -c "source /root/exec && tar --hard-dereference -hcf - \$SYSROOT | tar -xf - -C /"

# clean up shared objects that are not used
COPY analyze_used_libraries.sh /root/analyze_used_libraries.sh
RUN chmod +x /root/analyze_used_libraries.sh
RUN bash -c "source /root/pyexec && /root/analyze_used_libraries.sh \$SYSROOT/usr/lib \$SYSROOT prune"

RUN bash -c "source /root/exec && \
    rm -rf \$SYSROOT/usr/share \
    rm \$SYSROOT/usr/lib/python3.11/config-3.11-arm-linux-gnueabihf/libpython3.11.a \
    rm -rf \$SYSROOT/etc \
    rm -rf \$SYSROOT/usr/lib/terminfo \
    rm -rf \$SYSROOT/usr/lib/cmake \
    rm -rf \$SYSROOT/usr/lib/pkgconfig \
    rm -rf \$SYSROOT/usr/lib/engines-1.1 \
    rm -rf \$SYSROOT/usr/lib/python3.11/test \
    rm -rf \$SYSROOT/usr/lib/python3.11/lib2to3 \
    rm -rf \$SYSROOT/usr/lib/python3.11/tk*  # Tkinter-related \
    rm -rf \$SYSROOT/usr/lib/python3.11/idlelib \
    rm -rf \$SYSROOT/usr/lib/python3.11/pydoc_data"
