// See https://github.com/bxparks/AUniter/tree/develop/jenkins for
// a description of AUniter integration with Jenkins.
//
// The following variables are used:
//
//  * AUNITER_ARDUINO_BINARY - defined in the Jenkins system configuration
//  * WORKSPACE - automatically filled in by Jenkins
//  * BOARDS - defined in the "This project is parameterized" section of the
//    Jenkins Pipeline configuration
//  * BADGE_BUCKET - defined in "This project is parameterized" section.
//
pipeline {
    agent { label 'master' }
    stages {
        stage('Setup') {
            steps {
                dir('AUniter') {
                    git url: 'https://github.com/bxparks/AUniter',
                        branch: 'develop'
                }
                dir('libraries/AUnit') {
                    git url: 'https://github.com/bxparks/AUnit',
                        branch: 'develop'
                }
                dir('libraries/AceSegment') {
                    git url: 'https://github.com/bxparks/AceSegment',
                        branch: 'develop'
                }
                dir('libraries/AceRoutine') {
                    git url: 'https://github.com/bxparks/AceRoutine',
                        branch: 'develop'
                }
                dir('libraries/Time') {
                    git url: 'https://github.com/PaulStoffregen/Time',
                        branch: 'master'
                }
                dir('libraries/FastCRC') {
                    git url: 'https://github.com/FrankBoesing/FastCRC',
                        branch: 'master'
                }
                dir('libraries/SSD1306Ascii') {
                    git url: 'https://github.com/greiman/SSD1306Ascii',
                        branch: 'master'
                }
            }
        }
        stage('Verify Examples') {
            steps {
                sh "AUniter/tools/auniter.sh \
                    --config libraries/AceTime/tests/auniter.ini \
                    verify \
                    --sketchbook $WORKSPACE \
                    $BOARDS \
                    \$(find libraries/AceTime/examples -mindepth 1 -maxdepth 1 -type d)"
            }
        }
        stage('Verify Tests') {
            steps {
                sh "AUniter/tools/auniter.sh \
                    --config libraries/AceTime/tests/auniter.ini \
                    verify \
                    --sketchbook $WORKSPACE \
                    $BOARDS \
                    libraries/AceTime/tests/*Test"
            }
        }
        stage('Test') {
            steps {
                sh "AUniter/tools/auniter.sh \
                    --config libraries/AceTime/tests/auniter.ini \
                    test \
                    --skip_missing_port \
                    --sketchbook $WORKSPACE \
                    $BOARDS \
                    libraries/AceTime/tests/*Test"
            }
        }
    }
    post {
        failure {
            script {
                if (env.BADGE_BUCKET?.trim()) {
                    sh "AUniter/BadgeService/set-badge-status.sh \
                        $BADGE_BUCKET AceTime FAILED"
                }
            }
        }
        success {
            script {
                if (env.BADGE_BUCKET?.trim()) {
                    sh "AUniter/BadgeService/set-badge-status.sh \
                        $BADGE_BUCKET AceTime PASSED"
                }
            }
        }
    }
}
