def getBranch() {
    if (env.BRANCH_NAME != null && !env.BRANCH_NAME.isEmpty() ) {
        return env.BRANCH_NAME
    } else {
        return 'develop'
    }
}

void checkout_project() {
    checkout([
        $class: 'GitSCM',
        branches: [
            [name: 'refs/heads/' + getBranch()]
        ],
        extensions: [
            [$class: 'CleanBeforeCheckout'],
            [$class: 'SubmoduleOption', 
                disableSubmodules: false, 
                recursiveSubmodules: true, 
                trackingSubmodules: false,
                parentCredentials: true, 
                shallow: true
            ]
        ],
        submoduleCfg: [],
        userRemoteConfigs: [
            [credentialsId: 'gitea-jenkins', url: 'https://git.minres.com/SystemC/SystemC-Components-Test.git']
        ]
    ])
}

void build_n_test_project() {
    sh'''
 		python3 -mvenv .venv
		. .venv/bin/activate
		pip3 install -r requirements.txt
        cmake -S . -B build --preset Release
        cmake --build build -j12
        cmake --build build --target test
    '''
}

pipeline {
    agent none
    options { 
        // using the Timestamper plugin we can add timestamps to the console log
        timestamps() 
        skipStagesAfterUnstable() 
    }

    stages {
        stage('SCC test pipeline') {
            parallel {
                stage('ubuntu-22.04') {
                    agent {docker { image 'ubuntu-22.04' } }
                    stages {
                        stage('Checkout') { steps { checkout_project() }}
                        stage('Build & test') { steps { build_n_test_project() }}
                    }
                }
//                stage('U20.04') {
//                    agent {docker { image 'ubuntu-20.04' } }
//                    stages {
//                        stage('Checkout') { steps { checkout_project() }}
//                        stage('Build & test') { steps { build_n_test_project() }}
//                    }
//                }
                stage('centos7') {
                    agent {docker { image 'centos7' } }
                    stages {
                        stage('Checkout') { steps { checkout_project() }}
                        stage('Build & test') { steps { 
						    sh'''
						 		python3 -mvenv .venv
								. .venv/bin/activate
								pip3 install -r requirements.txt
								conan install --requires 'b2/[*]' --build='*'
						        cmake -S . -B build --preset Release
						        cmake --build build -j12
						        cmake --build build --target test
						    '''
						 }}
                    }
                }
                stage('rockylinux8') {
                    agent {docker { image 'rockylinux8' } }
                    stages {
                        stage('Checkout') { steps { checkout_project() }}
                        stage('Build & test') { steps { build_n_test_project() }}
                    }
                }
                //
                stage('Format check') {
                    agent {docker { image 'ubuntu-riscv' } }
                    stages {
                        stage('Checkout') { steps { checkout_project() }}
                        stage('Build & check format') { steps { 
						 	sh'''
						 		python3 -mvenv .venv
								. .venv/bin/activate
								pip3 install -r requirements.txt
						        cmake -S . -B build --preset Release
						        cmake --build build --target format-check
						    '''
						}}
                    }
                }
            }
        }
    }
    
    post {
        success {
            rocketSend ":thumbsup: SCC test run passed, results at ${env.RUN_DISPLAY_URL} " 
        }
        failure {
            rocketSend ":thumbsdown: SCC test failed, please check ${env.RUN_DISPLAY_URL} " 
            emailext recipientProviders: [culprits(), requestor()],
                subject: "SCC Test Pipeline Failed: ${currentBuild.fullDisplayName}",
                body: """
                <p>Build Status: ${currentBuild.currentResult}</p>
                <p> Check logs at <a href='${env.BUILD_URL}console'> Build Console Logs </a> or at <a href='${env.RUN_DISPLAY_URL}'> Overview </a></p>
                """
        }
    }
}