// Add the Gitea webhook configured for this Jenkins installation.

pipeline {
    agent { label 'rust_nodocker' }

    options {
        disableConcurrentBuilds()
        skipDefaultCheckout(true)
    }

    environment {
        ASSET_SOURCE_DIR = 'shared-tasks'
        ASSET_PUBLISH_ROOT = '/mnt/assets/agent-tasks'
    }

    stages {
        stage('Checkout') {
            steps {
                deleteDir()
                checkout scm
            }
        }

        stage('Discover publishable tasks') {
            steps {
                sh '''
                    set -eu

                    if [ ! -e "${ASSET_SOURCE_DIR}" ]; then
                        echo "No ${ASSET_SOURCE_DIR}/ directory; nothing is marked for publication."
                        exit 0
                    fi

                    test -d "${ASSET_SOURCE_DIR}" && test ! -L "${ASSET_SOURCE_DIR}" || {
                        echo "ERROR: ${ASSET_SOURCE_DIR} must be a real directory." >&2
                        exit 1
                    }

                    unexpected_type=$(find "${ASSET_SOURCE_DIR}" -mindepth 1 ! -type f -print -quit)
                    test -z "${unexpected_type}" || {
                        echo "ERROR: publishable assets must be regular files: ${unexpected_type}" >&2
                        exit 1
                    }

                    task_count=0
                    for task in "${ASSET_SOURCE_DIR}"/*.task.md; do
                        test -e "${task}" || continue
                        test -f "${task}" && test ! -L "${task}" && test -s "${task}" || {
                            echo "ERROR: task must be a nonempty regular file: ${task}" >&2
                            exit 1
                        }
                        task_count=$((task_count + 1))
                    done

                    unexpected_name=$(find "${ASSET_SOURCE_DIR}" -mindepth 1 -type f ! -name '*.task.md' -print -quit)
                    test -z "${unexpected_name}" || {
                        echo "ERROR: unexpected file in ${ASSET_SOURCE_DIR}: ${unexpected_name}" >&2
                        exit 1
                    }

                    if [ "${task_count}" -eq 0 ]; then
                        echo "No shared task files are marked for publication."
                    else
                        echo "Validated ${task_count} shared task files for publication."
                    fi
                '''
            }
        }

        stage('Publish assets') {
            when {
                branch 'main'
            }
            steps {
                sh '''
                    set -eu

                    if [ ! -d "${ASSET_SOURCE_DIR}" ]; then
                        echo "Nothing is marked for publication."
                        exit 0
                    fi

                    set -- "${ASSET_SOURCE_DIR}"/*.task.md
                    if [ ! -e "$1" ]; then
                        echo "Nothing is marked for publication."
                        exit 0
                    fi

                    case "${ASSET_PUBLISH_ROOT}" in
                        /*) ;;
                        *)
                            echo "ERROR: ASSET_PUBLISH_ROOT must be absolute." >&2
                            exit 1
                            ;;
                    esac

                    mkdir -p "${ASSET_PUBLISH_ROOT}"
                    chmod 0755 "${ASSET_PUBLISH_ROOT}"

                    for source in "$@"; do
                        name=${source##*/}
                        destination="${ASSET_PUBLISH_ROOT}/${name}"
                        temporary="${destination}.tmp-${BUILD_NUMBER}-$$"

                        trap 'rm -f "${temporary}"' 0 1 2 3 15
                        cp "${source}" "${temporary}"
                        chmod 0644 "${temporary}"
                        mv "${temporary}" "${destination}"
                        trap - 0 1 2 3 15
                    done

                    echo "Published task files below ${ASSET_PUBLISH_ROOT}."
                '''
            }
        }
    }
}
