pipeline {
    agent any

    environment {
        PACKAGE_NAME = "espos-kernel"
        APTLY_REPO = "espos-unstable"

        APTLY_HOST = "ubuntu@elordenador.org"

        DEB_FILE = "*.deb"
    }

    stages {

        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Install Build Dependencies') {
            steps {
                sh '''
                sudo apt update

                sudo apt install -y \
                    build-essential \
                    bc \
                    bison \
                    flex \
                    libssl-dev \
                    libelf-dev \
                    dwarves \
                    fakeroot \
                    rsync \
                    kmod \
                    cpio \
                    wget \
                    curl \
                    xz-utils \
                    debhelper \
                    lintian \
                    libdw-dev
                '''
            }
        }

        stage('Build Kernel') {
            steps {
                sh '''
                chmod +x build-kernel.sh
                ./build-kernel.sh

                echo "=== Generated packages ==="
                ls -lah *.deb
                '''
            }
        }

        stage('Lintian Check') {
            steps {
                sh '''
                lintian *.deb || true
                '''
            }
        }

        stage('Archive Packages') {
            steps {
                archiveArtifacts(
                    artifacts: '*.deb',
                    fingerprint: true
                )
            }
        }

        stage('Upload to Aptly') {
            steps {
                sshagent(['srv01_elordenador_org']) {
                    sh '''
                    mkdir -p ~/.ssh
                    ssh-keyscan -H elordenador.org >> ~/.ssh/known_hosts

                    scp ${DEB_FILE} ${APTLY_HOST}:/tmp/
                    '''
                }
            }
        }

        stage('Publish Repository') {
            steps {
                sshagent(['srv01_elordenador_org']) {

                    withCredentials([
                        string(
                            credentialsId: 'aptly_gpg_pass',
                            variable: 'GPG_PASS'
                        )
                    ]) {

                        sh '''
                        ssh ${APTLY_HOST} "

                            aptly repo add ${APTLY_REPO} /tmp/*.deb

                            if aptly publish list | grep -q ${APTLY_REPO}
                            then
                                aptly publish update \
                                    -passphrase='${GPG_PASS}' \
                                    ${APTLY_REPO}
                            else
                                aptly publish repo \
                                    -distribution=${APTLY_REPO} \
                                    -component=main \
                                    -passphrase='${GPG_PASS}' \
                                    ${APTLY_REPO}
                            fi

                            rm -f /tmp/*.deb
                        "
                        '''
                    }
                }
            }
        }
    }

    post {
        success {
            echo 'Kernel publicado correctamente'
        }

        failure {
            echo 'Error construyendo kernel'
        }
    }
}