FROM i386/alpine:latest AS glide-fetcher
ARG CACHE_BUST=29
RUN apk add --no-cache git ca-certificates
COPY .docker-cache/glide.ref /tmp/glide.ref
WORKDIR /src
RUN cat /tmp/glide.ref > /dev/null && \
    echo "$CACHE_BUST" > /dev/null && \
    git clone --depth 1 --branch retrodebian --single-branch https://gitea.chacha.ddns.net/chacha/glide.git && \
    rm -Rf glide/.git

FROM i386/alpine:latest AS icons-fetcher
ARG CACHE_BUST=11
RUN apk add --no-cache git ca-certificates
COPY .docker-cache/icons.ref /tmp/icons.ref
WORKDIR /root
RUN cat /tmp/icons.ref > /dev/null && \
    echo "$CACHE_BUST" > /dev/null && \
    git clone --depth 1 --branch main --single-branch https://gitea.chacha.ddns.net/chacha/icons-collection.git && \
    rm -Rf icons-collection/.git

# building a modified Kernel + modules + libs
FROM lpenz/debian-etch-i386-minbase AS packages-builder
ARG DEBIAN_FRONTEND=noninteractive
ARG ARCH=i386
ARG CONCURRENCY_LEVEL=4

# Preparing Environment
RUN (echo "deb http://archive.debian.org/debian etch main non-free" > /etc/apt/sources.list) && \
    (echo "deb-src http://archive.debian.org/debian etch main non-free" >> /etc/apt/sources.list) && \
    mkdir -p /usr/src/modules &&\
    apt-get update && \
    apt-get install -y  aptitude build-essential quilt cvs unzip git-core ncurses-dev\
                        linux-headers-2.6.24-etchnhalf.1-686 \
                        squashfs-source-etchnhalf \
                        linux-source-2.6.24 && \
    apt-get build-dep -y linux-image-2.6.24-etchnhalf.1-686 && \
    apt-get clean 

# Preparing Kernel + patchs + modules
WORKDIR /usr/src
COPY unionfs-2.2.3_for_2.6.24.diff.gz ./    
COPY nvidia.patch ./
COPY --from=glide-fetcher /src/glide glide
RUN tar xjf linux-source-2.6.24.tar.bz2 && \
    tar xjf etchnhalf/squashfs.tar.bz2 && \
    ln -s linux-headers-2.6.24-etchnhalf.1-686/ linux-headers && \
    mv glide/Device3Dfx modules/ &&\
    wget http://archive.debian.org/debian-archive/debian/pool/non-free/n/nvidia-graphics-drivers-legacy-71xx/nvidia-kernel-legacy-71xx-source_71.86.15-3_i386.deb && \
    dpkg-deb -x nvidia-kernel-legacy-71xx-source_71.86.15-3_i386.deb / && \
    tar xjf nvidia-kernel-legacy-71xx.tar.bz2 && \
    rm nvidia-kernel-legacy-71xx-source_71.86.15-3_i386.deb && \
    patch -d modules/nvidia-kernel-legacy-71xx -p1 < nvidia.patch

# Building Kernel + patchs + modules
WORKDIR /usr/src/linux-source-2.6.24
COPY config-kernel ./
RUN (zcat /usr/src/unionfs-2.2.3_for_2.6.24.diff.gz | patch -p1) && \
    (cat config-kernel > .config) && \
    make oldconfig &&\
    make-kpkg --initrd --arch $ARCH kernel_headers kernel_image modules_image

# Modified urxvt terminal
WORKDIR /usr/src
RUN apt-get build-dep -y rxvt-unicode && \
    apt-get source rxvt-unicode &&\
    cd rxvt-unicode-* && \
    cp debian/rules debian/rules.orig && \
    cp debian/changelog debian/changelog.orig && \
    sed -i '/\.\/configure/ s/$/ --disable-iso14755/' debian/rules && \
    perl -0pi -e 's/#if ENABLE_FRILLS \|\| ISO_14755\n\s*\/\/ ISO 14755 support/#if 0\n      \/\/ ISO 14755 support/' src/command.C && \
    sed -i '1s/^\([^)]* (\)\([^)]*\)\(.*\)$/\1\2+retrodebian1\3/' debian/changelog && \
    dpkg-buildpackage -us -uc -b

FROM packages-builder AS voodoo-builder
# Building userspace libs
# 3Dfx 
WORKDIR /usr/src
COPY buildglidepkg.sh ./
RUN apt-get install -y nasm && \
    chmod +x buildglidepkg.sh 
# cvg / voodoo2
WORKDIR /usr/src/glide
RUN find . -type f \( -name 'fxgasm.h' -o -name 'fxinline.h' -o -name 'fxgasm' \) -exec rm -f {} \;
# glide2x
RUN mv swlibs glide2x/
WORKDIR /usr/src/glide/glide2x/
RUN make -f makefile.linux FX_GLIDE_HW=cvg clean
RUN make -f makefile.linux FX_GLIDE_HW=cvg USE_X86=1 USE_MMX=1
RUN mv swlibs ../glide3x/
## glide3x
WORKDIR /usr/src/glide/glide3x/
RUN make -f makefile.linux FX_GLIDE_HW=cvg clean
RUN make -f makefile.linux FX_GLIDE_HW=cvg USE_X86=1 USE_MMX=1
RUN mv swlibs ../
WORKDIR /usr/src/
RUN /usr/src/buildglidepkg.sh cvg

## h3 / voodoo3 & Banshee
#WORKDIR /usr/src/glide
#RUN echo test3 && find . -type f -name 'fxgasm.h' 
#RUN find . -type f \( -name 'fxgasm.h' -o -name 'fxinline.h' -o -name 'fxgasm' \) -exec rm -f {} \;
#RUN apt-get install -y libx11-dev x11proto-xf86dga-dev libxxf86vm-dev libxxf86dga-dev
### glide2x
#RUN mv swlibs glide2x/
#WORKDIR /usr/src/glide/glide2x/
#RUN make -f makefile.linux clean
#RUN make -f makefile.linux FX_GLIDE_HW=h3 USE_X86=1 USE_MMX=1
## glide3x
#RUN mv swlibs ../glide3x/
#WORKDIR /usr/src/glide/glide3x/
#RUN make -f makefile.linux clean
#RUN make -f makefile.linux FX_GLIDE_HW=h3 USE_X86=1 USE_MMX=1
#RUN mv swlibs ../
#WORKDIR /usr/src/
#RUN /usr/src/buildglidepkg.sh h3

## sst1 / voodoo1
#WORKDIR /usr/src/glide
#RUN find . -type f \( -name 'fxgasm.h' -o -name 'fxinline.h' -o -name 'fxgasm' \) -exec rm -f {} \;
## glide2x
#RUN mv swlibs glide2x/
#WORKDIR /usr/src/glide/glide2x/
#RUN make -f makefile.linux clean
#RUN make -f makefile.linux FX_GLIDE_HW=sst1 USE_X86=1 USE_MMX=1
## glide3x
#RUN mv swlibs ../glide3x/
#WORKDIR /usr/src/glide/glide3x/
#RUN make -f makefile.linux clean
#RUN make -f makefile.linux FX_GLIDE_HW=sst1 USE_X86=1 USE_MMX=1
#RUN mv swlibs ../
#WORKDIR /usr/src/
#RUN /usr/src/buildglidepkg.sh sst1

# sst96 / voodoo Rush
# cannot build driver because of missing xf86rush lib...
#WORKDIR /usr/src/
#WORKDIR /usr/src/glide
#RUN find . -type f \( -name 'fxgasm.h' -o -name 'fxinline.h' -o -name 'fxgasm' \) -exec rm -f {} \;
#    # glide2x
#    RUN mv swlibs glide2x/
#    WORKDIR /usr/src/glide/glide2x/
#    RUN make -f Makefile.linux clean
#    RUN make -f Makefile.linux FX_GLIDE_HW=sst96
#    # glide3x
#    RUN mv swlibs ../glide3x/
#    WORKDIR /usr/src/glide/glide3x/
#    RUN make -f Makefile.linux clean
#    RUN make -f Makefile.linux FX_GLIDE_HW=sst96 USE_X86=1 USE_MMX=1
#    RUN mv swlibs ../
#WORKDIR /usr/src/
#RUN /usr/src/buildglidepkg.sh sst96

#RUN rm *.gz *.bz2 && rm -Rf glide && rm -rf /var/lib/apt/lists/*

FROM i386/alpine:latest AS ut99-builder
RUN apk add --no-cache git ca-certificates dos2unix
ARG CACHE_BUST=10
COPY .docker-cache/ut99.ref /tmp/ut99.ref
# Package UT99
WORKDIR /root
RUN cat /tmp/ut99.ref > /dev/null && \
    echo "$CACHE_BUST" && \
    git clone --depth 1 --branch master --single-branch https://gitea.chacha.ddns.net/chacha/UT99-Official.git ut-game && \
    rm -Rf ut-game/.git
COPY games/ut99/ut-install-436.run ./
RUN chmod +x ut-install-436.run && \
    ./ut-install-436.run --target ./ut || true && \
    tar xzf ./ut/data.tar.gz -C ut-game && \
    cp ./ut/bin/x86/ut ut-game/ && \
    rm ut-game/System/*.frt && \
    rm ut-game/System/*.itt && \
    rm ut-game/System/*.est && \
    rm ut-game/System/*.exe && \
    rm ut-game/System/*.dll && \
    rm ut-game/System/*.url && \
    rm ut-game/System/*.bat && \
    rm ut-game/System/de.* && \
    rm ut-game/System/UnrealTournament.ini && \
    rm ut-game/System/Default.ini && \
    rm ut-game/System/libSDL-1.1.so.0 && \
    rm ut-game/Sounds/*est_uax && \
    rm ut-game/Sounds/*frt_uax && \
    rm ut-game/Sounds/*itt_uax && \
    rm ut-game/DoConfig.py && \
    rm ut-game/DoConfig.sh && \
    rm -Rf ut-game/Web && \
    rm -Rf ut-game/Help/* &&\
    tar xzf ./ut/Glide.ini.tar.gz -C ut-game && \
    mv ut-game/System/UnrealTournament.ini ut-game/System/Default-glide.ini && \
    ls -al ./ut/ && \
    tar xzf ./ut/OpenGL.ini.tar.gz -C ut-game && \
    cp ut-game/System/UnrealTournament.ini ut-game/System/Default.ini && \
    mv ut-game/System/UnrealTournament.ini ut-game/System/Default-opengl.ini && \
    dos2unix ut-game/System/*.ini
COPY games/ut99/ut ./ut-game/ut
RUN chmod a+x,o-w ./ut-game/ut

# Note: we need to use Lenny because of live-helper
#       it allows creating Etch live image though !
FROM lpenz/debian-lenny-i386-minbase
WORKDIR /root
COPY --from=packages-builder /usr/src/*.deb .
COPY --from=voodoo-builder /usr/src/*.deb .
COPY --from=ut99-builder /root/ut-game ./ut-game
COPY --from=icons-fetcher /root/icons-collection ./icons-collection
COPY /deps ./deps
COPY update-initramfs.sh .
COPY zzz-config-live.sh .
COPY desktop .
COPY wallpaper.jpg .
COPY neofetch .
COPY editions ./editions
COPY internal ./internal
RUN apt-get update && \
    apt-get -y --force-yes install live-helper genisoimage syslinux squashfs-tools aptitude && \
    apt-get clean
#    rm -rf /var/lib/apt/lists/*
WORKDIR /root/live

#FROM voodoo-builder
#WORKDIR /usr/src
#RUN apt-get build-dep -y debian-edu-artwork-usplash && \
#    apt-get source debian-edu-artwork-usplash &&\
#    ls -al && \
#    cd debian-edu-artwork-*