package rosa

import (
	"strings"

	"hakurei.app/internal/pkg"
)

func (t Toolchain) newLibglvnd() (pkg.Artifact, string) {
	const (
		version  = "1.7.0"
		checksum = "eIQJK2sgFQDHdeFkQO87TrSUaZRFG4y2DrwA8Ut-sGboI59uw1OOiIVqq2AIwnGY"
	)
	return t.NewPackage("libglvnd", version, newFromGitLab(
		"gitlab.freedesktop.org",
		"glvnd/libglvnd",
		"v"+version,
		checksum,
	), nil, &MesonHelper{
		Setup: []KV{
			{"Dx11", "enabled"},
			{"Dglx", "enabled"},
		},
		ScriptCompiled: `
export DISPLAY=':0'
Xvfb &
XVFB_PID="$!"
trap 'kill $XVFB_PID && wait $XVFB_PID' EXIT
`,
	},
		Binutils, // symbols check fail with llvm nm
		Xserver,  // test suite wants X server

		LibXext,
	), version
}
func init() {
	artifactsM[Libglvnd] = Metadata{
		f: Toolchain.newLibglvnd,

		Name:        "libglvnd",
		Description: "The GL Vendor-Neutral Dispatch library",
		Website:     "https://gitlab.freedesktop.org/glvnd/libglvnd",

		Dependencies: P{
			LibXext,
		},

		ID: 12098,
	}
}

func (t Toolchain) newLibdrm() (pkg.Artifact, string) {
	const (
		version  = "2.4.133"
		checksum = "bfj296NcR9DndO11hqDbSRFPqaweSLMqRk3dlCPZpM6FONX1WZ9J4JdbTDMUd1rU"
	)
	return t.NewPackage("libdrm", version, newFromGitLab(
		"gitlab.freedesktop.org",
		"mesa/libdrm",
		"libdrm-"+version,
		checksum,
	), nil, &MesonHelper{
		Setup: []KV{
			{"Dintel", "enabled"},
		},
	},
		Binutils, // symbols check fail with llvm nm

		Libpciaccess,
		KernelHeaders,
	), version
}
func init() {
	artifactsM[Libdrm] = Metadata{
		f: Toolchain.newLibdrm,

		Name:        "libdrm",
		Description: "a userspace library for accessing the DRM",
		Website:     "https://dri.freedesktop.org/",

		Dependencies: P{
			Libpciaccess,
		},

		ID: 1596,
	}
}

func (t Toolchain) newLibva() (pkg.Artifact, string) {
	const (
		version  = "2.23.0"
		checksum = "UmF5tPyWIG_w5kiR3KFpoYbF7UUcaak5tyc-RhOheNTwQlLkPlifreFYCM9FQxbq"
	)
	return t.NewPackage("libva", version, newFromGitHub(
		"intel/libva",
		version,
		checksum,
	), nil, &MesonHelper{
		Setup: []KV{
			{"Dwith_x11", "yes"},
			{"Dwith_glx", "yes"},
			{"Dwith_wayland", "yes"},
		},
	},
		Libdrm,
		LibXfixes,
		Libglvnd,
		Wayland,
		KernelHeaders,
	), version
}
func init() {
	artifactsM[Libva] = Metadata{
		f: Toolchain.newLibva,

		Name:        "libva",
		Description: "an implementation for VA-API (Video Acceleration API)",
		Website:     "https://01.org/vaapi",

		Dependencies: P{
			Libdrm,
			LibXfixes,
			Libglvnd,
			Wayland,
		},

		ID: 1752,
	}
}

func (t Toolchain) newMesa() (pkg.Artifact, string) {
	const (
		version  = "26.1.0"
		checksum = "zU0fjqevySBaoi_5SLW3e2UffmGeBdxOuHmAHTH68n2hV-sjYoqg30koLqFXuk5y"
	)
	return t.NewPackage("mesa", version, newFromGitLab(
		"gitlab.freedesktop.org",
		"mesa/mesa",
		"mesa-"+version,
		checksum,
	), nil, &MesonHelper{
		Setup: []KV{
			{"Dplatforms", "x11,wayland"},
			{"Dvideo-codecs", "all"},

			{"Dglvnd", "enabled"},
			{"Dgbm", "enabled"},

			{"Dgallium-drivers", strings.Join([]string{
				"asahi",     // Apple AGX
				"crocus",    // Intel legacy
				"etnaviv",   // Vivante GPU designs (mostly NXP/Marvell SoCs)
				"freedreno", // Qualcomm Adreno (all Qualcomm SoCs)
				"i915",      // Intel extra legacy
				"iris",      // new Intel (Broadwell+)
				"lima",      // ARM Mali 4xx
				"llvmpipe",  // software renderer
				"nouveau",   // Nvidia
				"panfrost",  // ARM Mali Midgard and up (T/G series)
				"r300",      // very old AMD
				"r600",      // less old AMD
				"radeonsi",  // new AMD (GCN+)
				"softpipe",  // older software renderer
				"svga",      // VMWare virtualized GPU
				"tegra",     // Nvidia Tegra SoCs
				"v3d",       // Broadcom VC5 (Raspberry Pi 4)
				"vc4",       // Broadcom VC4 (Raspberry Pi 0-3)
				"virgl",     // QEMU virtualized GPU (aka VirGL)
				"zink",      // generic OpenGL over Vulkan, experimental

				// d3d12: WSL emulated GPU (aka Dozen)
				// ethosu: accelerator
				// rocket: accelerator
			}, ",")},

			{"Dvulkan-drivers", strings.Join([]string{
				"amd",         // AMD (aka RADV)
				"broadcom",    // Broadcom VC5 (Raspberry Pi 4, aka V3D)
				"freedreno",   // Qualcomm Adreno (all Qualcomm SoCs)
				"intel",       // new Intel (aka ANV)
				"intel_hasvk", // Intel Haswell/Broadwell, "legacy" Vulkan driver (https://www.phoronix.com/news/Intel-HasVK-Drop-Dead-Code)
				"panfrost",    // ARM Mali Midgard and up (T/G series)
				"swrast",      // software renderer (aka Lavapipe)
				"virtio",      // QEMU virtualized GPU (aka VirGL)
				"imagination", // PowerVR Rogue
				"asahi",       // Apple AGX
				"gfxstream",   // Android virtualized GPU

				// nouveau: Nouveau (aka NVK), requires rust
				// microsoft-experimental: WSL virtualized GPU (aka DZN/Dozen)
				// kosmickrisp: macOS-specific
			}, ",")},
			{"Dvulkan-layers", strings.Join([]string{
				"device-select",
				"intel-nullhw",
				"overlay",
				"screenshot",
				"anti-lag",
				"vram-report-limit",
			}, ",")},

			{"Dfreedreno-kmds", "msm,virtio"},
			{"Damdgpu-virtio", "true"},
		},
	},
		M4,
		PythonPackaging,
		PythonMako,
		PythonPyYAML,
		PythonPycparser,
		Glslang,
		SPIRVLLVMTranslator,

		Zlib,
		Zstd,
		Gzip,
		Ncurses,
		Libglvnd,
		Libexpat,
		Libva,
		Libdrm,
		Elfutils,
		Bison,
		Flex,
		LMSensors,
		Libconfig,
		LibdisplayInfo,
		Wayland,
		WaylandProtocols,
		Libxshmfence,
		LibXxf86vm,
		LibXrandr,
		LibxcbUtilKeysyms,
		Libpng,
		Libarchive,
		KernelHeaders,
	), version
}
func init() {
	artifactsM[Mesa] = Metadata{
		f: Toolchain.newMesa,

		Name:        "mesa",
		Description: "open source implementations of OpenGL, OpenGL ES, Vulkan, OpenCL, and more",
		Website:     "https://mesa3d.org",

		Dependencies: P{
			Libdrm,
			Elfutils,
			LMSensors,
			LibdisplayInfo,
			Wayland,
			Libxshmfence,
			LibXxf86vm,
			LibXrandr,
			LibxcbUtilKeysyms,
			Libpng,
		},

		ID: 1970,

		latest: (*Versions).getStable,
	}
}
