// Jenkins pipeline for Echoion.
//
// The agent must provide the toolchain the project builds with: GCC 16+
// (C++26 with experimental reflection, -freflection), CMake 4.0+, Node.js
// with npm (the EchoionWeb React/Vite frontend is built during the C++
// build), and the system libraries FFmpeg, SQLite3, OpenSSL, TBB, Qt6 and a
// system Boost (>= 1.87) matching VoidLibrary. A second vendored Boost in the
// same process corrupts URL parsing, so the system copy must be used. Point
// the `agent` at a container/node with that toolchain (e.g. a custom Docker
// image); the `docker` agent below is a template — adjust `image` to yours.
// (Configure with -DECHOION_BUILD_WEB=OFF to skip the frontend if npm is
// unavailable on the agent.)

pipeline {
    agent {
        docker { image 'echoion/ci:gcc16' args '-u root' }
    }

    environment {
        BUILD_DIR = 'build'
        CMAKE_BUILD_TYPE = 'RelWithDebInfo'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                sh 'git submodule update --init --recursive'
            }
        }

        stage('Build') {
            steps {
                sh '''
                    cmake -S . -B "$BUILD_DIR" \
                        -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
                        -DBUILD_TESTS=ON
                    cmake --build "$BUILD_DIR" -j "$(nproc)"
                '''
            }
        }

        stage('Test') {
            steps {
                sh 'ctest --test-dir "$BUILD_DIR" --output-on-failure'
            }
        }

        stage('Package') {
            when { buildingTag() }
            steps {
                sh 'cd "$BUILD_DIR" && cpack -G "TGZ;DEB"'
            }
        }
    }

    post {
        success {
            archiveArtifacts artifacts: 'build/plugins/echoion_plugin.so, build/bin/EchoionDesktop, build/*.tar.gz, build/*.deb',
                             allowEmptyArchive: true,
                             fingerprint: true
        }
    }
}
