pipeline {
    agent { label 'pyDABFActory-CIAgent' }
    stages {
        stage('Prepare Docker cache markers') {
            steps {
                sh '''
                mkdir -p .docker-cache

                git ls-remote https://gitea.chacha.ddns.net/chacha/glide.git refs/heads/retrodebian \
                    | awk '{print $1}' > .docker-cache/glide.ref
                test -s .docker-cache/glide.ref
                echo "glide ref: $(cat .docker-cache/glide.ref)"
                
                git ls-remote https://gitea.chacha.ddns.net/chacha/UT99-Official.git refs/heads/master \
                    | awk '{print $1}' > .docker-cache/ut99.ref
                test -s .docker-cache/ut99.ref
                echo "ut99 ref: $(cat .docker-cache/ut99.ref)"
                
                git ls-remote https://gitea.chacha.ddns.net/chacha/icons-collection.git refs/heads/main \
                    | awk '{print $1}' > .docker-cache/icons.ref
                test -s .docker-cache/icons.ref
                echo "icons ref: $(cat .docker-cache/icons.ref)"
                '''
            }
        }
        stage('Convert splash images')
        {
            agent {
                docker {
                    image 'alpine:3.21'
                    reuseNode true
                }
            }
            steps {
                sh '''
                    set -eu
                    apk add --no-cache imagemagick
                    found_edition=0
                    for edition_dir in editions/*; do
                        [ -d "$edition_dir" ] || continue
                        found_edition=1
                        set -- "$edition_dir"/splash-source.*
                        if [ ! -e "$1" ]; then
                            echo "ERROR: missing splash-source.* in $edition_dir" >&2
                            exit 1
                        fi
                        if [ $# -ne 1 ]; then
                            echo "ERROR: multiple splash-source.* files in $edition_dir" >&2
                            exit 1
                        fi
                        src="$1"
                        out="$edition_dir/splash.jpg"
                        echo "Converting $src -> $out"
                        magick "$src" \
                            -resize 640x480! \
                            -background black \
                            -alpha remove -alpha off \
                            -colorspace sRGB \
                            -strip \
                            -quality 95 \
                            "$out"
                    done

                    if [ "$found_edition" -eq 0 ]; then
                        echo "ERROR: no edition directory found under editions/" >&2
                        exit 1
                    fi
                '''
            }
        }
        stage('Create live')
        {
            agent {
                dockerfile {
                    filename 'Dockerfile'
                    dir '.'
                    label 'pyDABFActory-CIAgent'
                    args '--privileged --security-opt seccomp=unconfined --security-opt apparmor=unconfined -u root:root'
                    reuseNode true
                }
            }
            stages {
                stage('Configure ISO') {
                    steps {
                        dir('root')
                        {
                            sh 'rm -Rf live artifacts'
                        }
                        dir('root/live')
                        {
                            sh '''
                                set -e
                                deb="$(ls -1 /root/linux-image-*.deb | head -n1)"
                                kernpkg="$(dpkg-deb -f "$deb" Package)"
                                echo "$kernpkg"

                                deb="$(ls -1 /root/squashfs-modules-*.deb | head -n1)"
                                squashfspkg="$(dpkg-deb -f "$deb" Package)"
                                echo "$squashfspkg"

                                lh_config \
                                    -a i386 \
                                    --distribution etch \
                                    --apt apt \
                                    --apt-options "--yes --force-yes" \
                                    --apt-secure false \
                                    --apt-recommends false \
                                    --linux-packages "squashfs-modules-2.6.24 linux-image-2.6.24 device3dfx-modules-2.6.24" \
                                    --packages-lists "desktop" \
                                    --mirror-bootstrap http://archive.debian.org/debian/ \
                                    --mirror-binary http://archive.debian.org/debian/ \
                                    --mirror-binary-security http://archive.debian.org/debian-security/ \
                                    --mirror-chroot http://archive.debian.org/debian/ \
                                    --mirror-chroot-security http://archive.debian.org/debian-security/ \
                                    --binary-images iso \
                                    --bootloader syslinux \
                                    --syslinux-timeout 5 \
                                    --syslinux-menu true \
                                    --bootstrap debootstrap \
                                    --debconf-frontend noninteractive \
                                    --debian-installer live \
                                    --bootappend-live "elevator=deadline vga=785 splash quiet" \
                                    --hostname RetroDebian \
                                    --iso-application RetroDebian \
                                    --syslinux-splash config/binary_syslinux/splash.jpg \
                                    --union-filesystem unionfs \
                                    --username bob \
                                    --templates config/templates \
                                    -k retrodebian1
                                
                                cp -R /usr/share/live-helper/templates/* config/templates/
                                sed -i  \
                                    -e 's|^menu vshift.*|menu vshift    4|' \
                                    -e 's|^menu hshift.*|menu hshift    9|' \
                                    -e 's|^menu width.*|menu width    60|' \
                                    -e 's|^menu color sel.*|menu color sel       * #ff82c059 #c01f1f1f none|' \
                                    -e 's|^menu color unsel.*|menu color unsel     * #8082c059 #c01f1f1f none|' \
                                    -e 's|^menu color hotkey.*|menu color hotkey    * #fff01e1e #c01f1f1f none|' \
                                    -e 's|^menu color tabmsg.*|menu color tabmsg    * #ffB0B0B0 #00000000 none|' \
                                    -e 's|^menu color timeout_msg.*|menu color timeout_msg    * #ffB0B0B0 #00000000 none|' \
                                    -e 's|^menu color timeout.*|menu color timeout    * #ffff0000 #00000000 none|' \
                                    config/templates/syslinux/menu/header.cfg
                                
                                cp -R /root/internal/rootfs/* config/chroot_local-includes

                                chmod +x /root/update-initramfs.sh
                                chmod +x /root/zzz-config-live.sh
                                cp /root/update-initramfs.sh config/chroot_local-hooks
                                cp /root/zzz-config-live.sh config/chroot_local-hooks
                                cp /root/linux-image-*.deb config/chroot_local-packages/
                                cp /root/rxvt-unicode_*.deb config/chroot_local-packages/
                                cp /root/device3dfx-*.deb config/chroot_local-packages/
                                cp /root/squashfs-modules-*.deb config/chroot_local-packages/
                                cp /root/libglide*-cvg*_i386.deb config/chroot_local-packages/
                                cp /root/desktop config/chroot_local-packageslists/
                                mkdir -p config/chroot_local-includes/usr/share/wallpapers
                                cp /root/wallpaper.jpg config/chroot_local-includes/usr/share/wallpapers/retrodebian.jpg
                                mkdir -p config/chroot_local-includes/usr/local/bin
                                cp /root/neofetch config/chroot_local-includes/usr/local/bin/neofetch
                                chmod +x config/chroot_local-includes/usr/local/bin/neofetch
                                cp /root/editions/voodoo2/splash.jpg config/binary_syslinux/
                            '''
                        }
                    }
                }
                stage('Configure games') {
                    steps {
                        dir('root/live') {
                            sh '''
                                mkdir -p config/chroot_local-includes/usr/local/games/ut99/
                                cp -R /root/ut-game/* config/chroot_local-includes/usr/local/games/ut99/
                                cp /root/icons-collection/small/ut99.png /usr/share/icons/
                            '''
                        }
                    }
                }
                stage('Build ISO') {
                    steps {
                        dir('root/live')
                        {
                            sh 'lh_build'
                        }
                    }
                }
                stage('Save artifacts') {
                    steps {
                        dir('root')
                        {
                            sh '''
                                mkdir artifacts
                                mv /root/*.deb artifacts/
                            '''
                        }
                    }
                }
            }
        }
    }
    post {
        success  {
            archiveArtifacts artifacts: 'root/live/*.iso', fingerprint: true
            archiveArtifacts artifacts: 'root/artifacts/*.deb', fingerprint: true
            deleteDir()
        }
    }
}