pipeline {
    agent any
    stages {
        stage('Build Windows Debug') {
            when {
                branch 'develop'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm-mingw:latest'
                    reuseNode true
                }
            }
            environment { 
                DEBUG = 1
            }
            steps {
                sh 'bash ./build.sh win32'
                sh 'analyze-build --verbose --cdb build/compile_commands.json --output build/Analysis/win32'
                sh 'bash ./build.sh win64'
                sh 'analyze-build --verbose --cdb build/compile_commands.json --output build/Analysis/win64'
            }
        }
        stage('Build Linux Debug') {
            when {
                branch 'develop'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm:latest'
                    reuseNode true
                }
            }
            environment { 
                DEBUG = 1
            }
            steps {
                sh 'bash ./build.sh lin32'
                sh 'analyze-build --verbose --cdb build/compile_commands.json --output build/Analysis/lin32'
                sh 'bash ./build.sh lin64'
                sh 'analyze-build --verbose --cdb build/compile_commands.json --output build/Analysis/lin64'
            }
        }
        stage('Build MacOSX Debug') {
            when {
                branch 'develop'
                beforeAgent true
            }
            agent {
                docker {
                    image 'osxcross:latest'
                    reuseNode true
                }
            }
            environment { 
                DEBUG = 1
            }
            steps {
                sh 'bash ./build.sh mac'
                sh 'analyze-build --verbose --cdb build/compile_commands.json --output build/Analysis/mac'
            }
        }
        stage('Archive Debug') {
            when {
                branch 'develop'
            }
            steps {
                zip zipFile: 'Debug.zip', archive: true, dir: 'build/Plugin'
                zip zipFile: 'Analysis.zip', archive: true, dir: 'build/Analysis'
                sh 'rm -rf build'
            }
        }
        stage('Build Windows Stageing') {
            when {
                branch 'stage'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm-mingw:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh win32'
                sh 'bash ./build.sh win64'
            }
        }
        stage('Build Linux Stageing') {
            when {
                branch 'stage'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh lin32'
                sh 'bash ./build.sh lin64'
            }
        }
        stage('Build MacOSX Stageing') {
            when {
                branch 'stage'
                beforeAgent true
            }
            agent {
                docker {
                    image 'osxcross:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh mac'
            }
        }
        stage('Archive Stageing') {
            when {
                branch 'stage'
            }
            steps {
                zip zipFile: 'Stage.zip', archive: true, dir: 'build/Plugin'
                sh 'rm -rf build'
            }
        }
        stage('Build Windows Release') {
            when {
                branch 'master'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm-mingw:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh win32'
                sh 'bash ./build.sh win64'
            }
        }
        stage('Build Linux Release') {
            when {
                branch 'master'
                beforeAgent true
            }
            agent {
                docker {
                    image 'llvm:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh lin32'
                sh 'bash ./build.sh lin64'
            }
        }
        stage('Build MacOSX Release') {
            when {
                branch 'master'
                beforeAgent true
            }
            agent {
                docker {
                    image 'osxcross:latest'
                    reuseNode true
                }
            }
            steps {
                sh 'bash ./build.sh mac'
            }
        }
        stage('Archive Release') {
            when {
                branch 'master'
                beforeAgent true
            }
            steps {
                zip zipFile: 'Release.zip', archive: true, dir: 'build/Plugin'
                sh 'rm -rf build'
            }
        }
    }
}
