// Don't forget to add http://192.168.1.37:8080/gitea-webhook/post as a gitea webhook

pipeline {
    agent { label 'rust_no_docker' }
    /* ===============
    BEGIN USER CONFIG
    ================== */

    environment {
        DEPLOY_DIR = "/mnt/books/rust_doc/efo/"           // Important, all books are in /mnt/book
        MDBOOK_BUILD_DIR = "target/doc/"
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm                              // Checkout intead of full clone
            }
        }

        stage('Update') {
            steps {
                sh "rustup update"
                // sh "rustup show"
            }
        }

        /* ===============
         END USER CONFIG
        ================== */

        stage('Build Book') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'origin/review' || env.BRANCH_NAME == 'review' || env.BRANCH_NAME == 'origin/dev-ci' || env.BRANCH_NAME == 'dev-ci') {
                        sh 'cargo test'
                        sh 'cargo doc'
                        sh 'echo "Deploying to ${DEPLOY_DIR}"'
                        sh 'mkdir -p ${DEPLOY_DIR}'
                        sh 'rsync -a --delete  ${MDBOOK_BUILD_DIR}/ ${DEPLOY_DIR}'
                    } else if (env.BRANCH_NAME == 'origin/master' || env.BRANCH_NAME == 'master') {
                        sh 'cargo test'
                        sh 'cargo doc'
                        sh 'echo "Deploying to ${DEPLOY_DIR}"'
                        sh 'mkdir -p ${DEPLOY_DIR}'
                        sh 'rsync -a --delete  ${MDBOOK_BUILD_DIR}/ ${DEPLOY_DIR}'
                    } else {
                        echo "Unknown branch: ${env.BRANCH_NAME} - skipping build."
                    }
                }
            }
        }
    }

    post {
        success {
            publishChecks conclusion: 'SUCCESS', name: 'Build Successful', summary: 'The build was successful', text: 'The build was sucessful', title: 'Build Successful'
            echo '✅ Build Deployed!'
        }
        failure {
            publishChecks conclusion: 'FAILURE', name: 'Build Failed', summary: 'The build failed', text: 'The build was a failure', title: 'Build failed'
            echo '❌Build Failed to Deploy'
        }
        unstable{
            publishChecks conclusion: 'ACTION_REQUIRED', name: 'Build Unstable', summary: 'Unstable build', text: 'Unstable build', title: 'Build Unstable'
        }
    }
}
