#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# runc 1.3.5 — low-level OCI container runtime
# Upstream: https://github.com/opencontainers/runc
#
# Pinned to moby v29.5.2's choice (Dockerfile:251). containerd v2.2.3 + runc
# v1.3.5 + moby v29.5.2 form a bilaterally-tested trio; do not mix versions.
#
# Build approach mirrors Arch + Fedora:
#   - BUILDTAGS='seccomp apparmor' (security tags upstream-default + our LSM)
#   - GO111MODULE=on + -mod=vendor (vendor/ ships in the release tarball)
#   - COMMIT=v1.3.5 + VERSION=1.3.5 injected via env (chroot has no .git tree)
#   - CGO_ENABLED=1 for libseccomp link + nsenter constructor C glue
#
# Install paths (Arch + upstream canonical):
#   /usr/sbin/runc                       — the runtime binary
#   /usr/share/man/man8/runc*.8          — generated by md2man-all.sh
#   /usr/share/bash-completion/completions/runc — contrib/completions/bash/runc
#
# CRIU support is enabled in build tags (default) but CRIU is RPC-based — the
# `criu` binary is NOT a build dep and NOT linked. Users opting in to
# checkpoint/restore install `criu` separately (not in our tree as of v1.0).

BUILD_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

configure() {
    set -e
    # The release tarball extracts to a versioned dir; cd in.
    # Pre-orchestrator extraction lands us here automatically; no-op.
    true
}

build() {
    set -e
    export GOTOOLCHAIN=local
    export CGO_ENABLED=1
    export GO111MODULE=on
    export GOFLAGS="-trimpath -mod=vendor -modcacherw"
    export BUILDTAGS='seccomp apparmor'
    # Inject version + commit so make's `git describe` fallback doesn't fire
    # and end up embedding "unknown" or breaking the build entirely.
    export VERSION="1.3.5"
    export COMMIT="v1.3.5"

    make runc -j${IGOS_JOBS}
    make man   # invokes man/md2man-all.sh (needs go-md2man on PATH)
}

check() {
    set -e
    pkg_run_tests "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/package.yml" \
        true
}

do_install() {
    set -e
    # runc's Makefile install rule targets PREFIX=/usr/local by default and
    # BINDIR=$(PREFIX)/sbin. Override PREFIX to land at /usr/sbin/runc, which
    # is upstream-canonical (Arch + Fedora both ship /usr/sbin/runc; Debian
    # ships /usr/sbin/runc as well). containerd-shim-runc-v2 resolves runc
    # via $PATH so the sbin location works as long as /usr/sbin is on PATH
    # (it always is in systemd-managed services).
    make install      PREFIX=/usr DESTDIR="$DESTDIR"
    make install-man  PREFIX=/usr DESTDIR="$DESTDIR"
    make install-bash PREFIX=/usr DESTDIR="$DESTDIR"
}
