pipeline {
    agent any
    stages {
        stage('Build-Linux') { 
            tools {
                jdk 'JDK21'
            }
            steps {
                sh 'java -version'
                sh 'echo $JAVA_HOME'
				sh 'rm -rf build/linux'
                sh 'mkdir -p build/linux'
                sh 'cmake -B build/linux -DCMAKE_BUILD_TYPE=Release' 
                sh 'cmake --build build/linux'
            }
        }
        stage('Build-Windows') { 
            tools {
                jdk 'JDK21-WIN'
            }
            steps {
                sh 'java -version'
                sh 'echo $JAVA_HOME'
				sh 'rm -rf build/windows'
                sh 'mkdir -p build/windows'
                sh 'cmake -B build/windows -DCMAKE_TOOLCHAIN_FILE=mingw.cmake -DCMAKE_BUILD_TYPE=Release' 
                sh 'cmake --build build/windows'
            }
        }
        stage('Package-Artifacts') {
            steps {
                sh '''
                    rm -rf dist
                    mkdir -p dist/native/windows
                    mkdir -p dist/native/linux

                    # copy linux .so and .so.* files
                    find build/linux -type f -name "*.so*" -exec cp {} dist/native/linux/ \\;

                    # copy windows dll files
                    find build/windows -type f -name "*.dll" -exec cp {} dist/native/windows/ \\;

                    # create zip
                    cd dist
                    zip -r ../VoidLoader.zip native
                '''
            }

            post {
                success {
                    archiveArtifacts artifacts: 'VoidLoader.zip', fingerprint: true
                }
            }
        }
        stage('Publish-To-Gitea') {
            steps {
                withCredentials([string(credentialsId: 'GITEA_TOKEN', variable: 'GITEA_TOKEN')]) {
                    sh '''
                        GITEA_URL="https://git.faumaray.ru"
                        REPO="VoidLoader/VoidLoaderCore"
                        TAG="build-${BUILD_NUMBER}"

                        curl -X POST -H "Authorization: token $GITEA_TOKEN" \
                            -H "Content-Type: application/json" \
                            -d "{\\"tag_name\\": \\"$TAG\\", \\"name\\": \\"$TAG\\"}" \
                            $GITEA_URL/api/v1/repos/$REPO/releases || true

                        curl -X POST -H "Authorization: token $GITEA_TOKEN" \
                            -F "attachment=@VoidLoader.zip" \
                            "$GITEA_URL/api/v1/repos/$REPO/releases/$TAG/assets"
                    '''
                }
            }
        }

    }
}

