pipeline {
    agent { label 'pyDABFActory-CIAgent' }

    parameters {
        booleanParam(name: 'BUILD_ALL_PROFILES', defaultValue: true, description: 'Build all profiles one by one')
        string(name: 'PROFILE', defaultValue: 'demo', description: 'Profile to build when BUILD_ALL_PROFILES is false')
        booleanParam(name: 'FAKE_LEGACY', defaultValue: false, description: 'Use fake lh_config/lh_build wrappers')
        booleanParam(name: 'KEEP_CONTAINERS', defaultValue: false, description: 'Stop at end but do not remove runtime containers')
    }

    stages {
        stage('Run RetroDebian workflow') {
            steps {
                sh '''
                    set -eu

                    CMD="python3 builder/py/orchestrate.py run"

                    if [ "$BUILD_ALL_PROFILES" = "true" ]; then
                        CMD="$CMD --all-profiles"
                    else
                        CMD="$CMD --profile $PROFILE"
                    fi

                    if [ "$FAKE_LEGACY" = "true" ]; then
                        CMD="$CMD --fake-legacy"
                    fi

                    if [ "$KEEP_CONTAINERS" = "true" ]; then
                        CMD="$CMD --keep-containers"
                    fi

                    echo "$CMD"
                    exec sh -lc "$CMD"
                '''
            }
        }
    }

    post {
        always {
            archiveArtifacts artifacts: 'artifacts/**/*', allowEmptyArchive: true
            archiveArtifacts artifacts: 'live/**/*', allowEmptyArchive: true
        }
    }
}
