def repository = "registry.c.test-chamber-13.lan"
def repositoryCreds = "harbor-repository-creds"

def label = "kubernetes-${UUID.randomUUID().toString()}"
def templateName = "pipeline-worker"
podTemplate(
    label: label,
    name: templateName,
    yaml: functions.podYaml(
        repo: repository,
        templateName: templateName,
        alpine: true,
        fedora: true
    )
) {
    node (label) {
        def workspace = pwd()

        stage ("Pull FFMpeg Builder") {
            dir("ffmpeg") {
                checkout ([
                    $class: "GitSCM",
                    branches: [
                        [
                            name: "refs/heads/master",
                        ],
                    ],
                    userRemoteConfigs: [
                        [
                            url: "https://github.com/markus-perl/ffmpeg-build-script.git",
                        ],
                    ],
                    extensions: [
                        [
                            $class: "CloneOption",
                            shallow: true,
                        ],
                        [
                            $class: "CheckoutOption",
                            timeout: 2,
                        ],
                    ],
                ])
            }
        }

        stage ("Build FFMpeg") {
            container ("fedora") {
                writeFile(file: workspace + "/test-chamber-13.lan.root.crt", text: functions.getCurrentRootCA())
                sh """
                    cd ${workspace}/ffmpeg
                    sed -r -i -e 's~^LDFLAGS="-L\\\$WORKSPACE\\/lib"\$~LDFLAGS="-L\\\$WORKSPACE\\/lib -L\\\$WORKSPACE\\/lib64"~g' ./build-ffmpeg
                    dnf install curl gcc-g++ libtool make xz python perl perl-devel openssl-devel diffutils bzip2 glibc-static libstdc++-static vid.stab-devel -y
                    SKIPINSTALL=yes ./build-ffmpeg --full-static --enable-gpl-and-non-free --build
                """
            }
        }

        functions.pushArtifact(
            repoCreds: "nexus-generic-upload-bot",
            fileName: "ffmpeg",
            filePath: "${workspace}/ffmpeg/workspace/bin/",
            fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/ffmpeg/"
        )
        functions.pushArtifact(
            repoCreds: "nexus-generic-upload-bot",
            fileName: "ffprobe",
            filePath: "${workspace}/ffmpeg/workspace/bin/",
            fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/ffmpeg/"
        )
        functions.pushArtifact(
            repoCreds: "nexus-generic-upload-bot",
            fileName: "ffplay",
            filePath: "${workspace}/ffmpeg/workspace/bin/",
            fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/ffmpeg/"
        )
    }
}