#!groovy

// Current version of this Pipeline https://github.com/VitexSoftware/BuildImages/blob/main/Test/Jenkinsfile-parael

// debian:forky disabled: Forky is still unstable/research-only.
// The full Debian package ecosystem is not yet available for Forky.
// Re-enable once the stack builds cleanly for Forky.
String[] distributions = ['debian:bookworm', 'debian:trixie', 'ubuntu:jammy', 'ubuntu:noble']

String vendor = 'vitexsoftware'
//String distroFamily = ''

properties([
    copyArtifactPermission('*'),
    buildBlocker(
        useBuildBlocker: true,
        blockLevel: 'GLOBAL',
        scanQueueFor: 'ALL',
        blockingJobs: 'RebulidDEBRepoByAnsible'
    )
])
node() {
    ansiColor('xterm') {
        stage('SCM Checkout') {
            checkout scm
        }
    }
}

def branches = [:]
distributions.each { distro ->
    branches[distro] = {
        def distroName = distro
        println  "Dist:" + distroName

        def dist = distroName.split(':')
        def distroFamily = dist[0]
        def distroCode = dist[1]
        def buildImage = ''
        def artifacts = []
        def buildVer = ''

        node {
            ansiColor('xterm') {
                stage('Checkout ' + distroName) {
                    checkout scm
                    def imageName = vendor + '/' + distro
                    buildImage = docker.image(imageName)
                    sh 'git checkout debian/changelog'
                    def version = sh (
                        script: 'dpkg-parsechangelog --show-field Version',
                        returnStdout: true
                    ).trim()
                    buildVer = version + '.' + env.BUILD_NUMBER  + '~' + distroCode
                }
                stage('Build ' + distroName) {
                    buildImage.inside {
                        sh 'dch -b -v ' + buildVer  + ' "' + env.BUILD_TAG  + '"'
                        sh 'sudo apt-get update --allow-releaseinfo-change'
                        sh 'sudo chown jenkins:jenkins ..'
                        sh 'debuild-pbuilder -r"sudo -E" -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 ' + distroName) {
                    buildImage.inside {
                        def debconf_debug = 0 //Set to "5" or "developer" to debug debconf
                        sh 'cd $WORKSPACE/dist/debian/ ; dpkg-scanpackages . /dev/null > Packages; gzip -9c Packages > 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"'
                      
// Ensure abraflexi-webhook-acceptor-sqlite is first in the list
                    def installOrder = [
                        'abraflexi-webhook-acceptor-sqlite',
                    ]

                    def sorted_artifacts = artifacts.toList()
                    installOrder.each { pkgPrefix ->
                        def debFile = null
                        for (item in sorted_artifacts) {
                            def itemStr = item.toString()
                            if (itemStr.startsWith(pkgPrefix) && itemStr.endsWith('.deb')) {
                                debFile = itemStr
                                break
                            }
                        }
                        if (debFile) {
                            def pkgName = debFile.tokenize('/')[-1].replaceFirst(/_.*/, '')
                            sh 'echo -e "${GREEN} installing ' + pkgName + ' on `lsb_release -sc` ${ENDCOLOR} "'
                            sh 'sudo DEBIAN_FRONTEND=noninteractive DEBCONF_DEBUG=' + debconf_debug + ' apt-get -y install ' + pkgName + ' || sudo apt-get -y -f install'
                        }
                    }

                }
                stage('Archive artifacts ' + distroName ) {
                    // Only run if previous stages (Build and Test) succeeded
                    buildImage.inside {
                        // Archive all produced artifacts listed in debian/files
                        artifacts.each { deb_file ->
                            println "Archiving artifact: " + deb_file
                            archiveArtifacts artifacts: 'dist/debian/' + deb_file
                        }

                        // Cleanup: remove any produced files named in debian/files
                        // Try both the dist location and any potential original locations referenced by debian/files
                        sh '''
                            set -e
                            if [ -f debian/files ]; then
                              while read -r file _; do
                                [ -n "$file" ] || continue
                                rm -f "dist/debian/$file" || true
                                rm -f "../$file" || true
                                rm -f "$WORKSPACE/$file" || true
                              done < debian/files
                            fi
                        '''
                    }
                }
            }
        }
    }
}
}

parallel branches

node {
    stage('Publish to Aptly') {
	publishDebToAptly()
    }
}
