#!groovy

String[] distributions = ['debian:bullseye', 'debian:bookworm', 'debian:trixie', 'ubuntu:focal', 'ubuntu:jammy', 'ubuntu:noble']

String vendor = 'vitexsoftware'
String distribution = ''
//String distroFamily = ''
String distroCodename = ''
String ver = ''

properties([
    copyArtifactPermission('*')
])
node() {
    ansiColor('xterm') {
        stage('SCM Checkout') {
            checkout scm
        }
    }
}

distributions.each {
    distribution = it

    println  "Dist:" + distribution

    def dist = distribution.split(':')
    distroCodename = dist[1]

    def buildImage = ''

    def artifacts = []

    node {
        ansiColor('xterm') {
            stage('Checkout ' + distribution) {
                checkout scm
                buildImage = docker.image(vendor + '/' + distribution)
                sh 'git checkout debian/changelog'
                def version = sh (
                    script: 'dpkg-parsechangelog --show-field Version',
                    returnStdout: true
                ).trim()
                ver = version + '.' + env.BUILD_NUMBER  + '~' + distroCodename
            }
            stage('Build ' + distribution) {
                buildImage.inside {
                    sh 'dch -b -v ' + ver  + ' "' + env.BUILD_TAG  + '"'
                    sh 'sudo apt-get update --allow-releaseinfo-change'
                    sh 'sudo chown jenkins:jenkins ..'
                    sh 'debuild-pbuilder  -i -us -uc -b'
                    sh 'mkdir -p $WORKSPACE/dist/debian/ ; rm -rf $WORKSPACE/dist/debian/* ; for deb in $(cat debian/files | awk \'{print $1}\'); do mv "../$deb" $WORKSPACE/dist/debian/; done'
                    artifacts = sh (
                        script: "cat debian/files | awk '{print \$1}'",
                        returnStdout: true
                    ).trim().split('\n')
                }
            }

            stage('Test ' + distribution) {
                buildImage.inside {
                    def debconf_debug = 0 //Set to "5" or "developer" to debug debconf
                    sh 'cd $WORKSPACE/dist/debian/ ; dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz; cd $WORKSPACE'
                    sh 'echo "deb [trusted=yes] file://///$WORKSPACE/dist/debian/ ./" | sudo tee /etc/apt/sources.list.d/local.list'
                    sh 'sudo apt-get update --allow-releaseinfo-change'
                    sh 'echo "INSTALATION"'
                    artifacts.each { deb_file ->
                        if (deb_file.endsWith('.deb')) {
                            sh 'echo -e "${GREEN} installing ' + deb_file + ' on `lsb_release -sc` ${ENDCOLOR} "'
                            sh 'sudo DEBIAN_FRONTEND=noninteractive DEBCONF_DEBUG=' + debconf_debug + ' apt-get -y install $WORKSPACE/dist/debian/' + deb_file
                        }
                    }
                }
            }
            stage('Copy artifacts ' + distribution ) {
                buildImage.inside {
                    artifacts.each { deb_file ->
                        println "Copying artifact: " + deb_file
                        archiveArtifacts artifacts: 'dist/debian/' + deb_file
                    }
                    sh 'mv $WORKSPACE/dist/debian/*.deb $WORKSPACE'
                }
            }
        }
    }
}

