package rosa

import (
	"hakurei.app/fhs"
	"hakurei.app/internal/pkg"
)

func (t Toolchain) newStage0() (pkg.Artifact, string) {
	return t.New("rosa-stage0", 0, t.AppendPresets(nil,
		Bzip2,
	), nil, nil, `
umask 377
tar \
	-vjc \
	-C /stage0 \
	-f /work/stage0-`+triplet()+`.tar.bz2 \
	.
`, pkg.Path(fhs.AbsRoot.Append("stage0"), false, t.AppendPresets(nil,
		LLVM,
		Mksh,
		toyboxEarly,
	)...)), Unversioned
}
func init() {
	artifactsM[Stage0] = Metadata{
		f: Toolchain.newStage0,

		Name:        "rosa-stage0",
		Description: "Rosa OS stage0 toolchain tarball for bootstrap",
	}
}

func init() {
	const version = "20260504"
	artifactsM[stage0Dist] = Metadata{
		f: func(Toolchain) (pkg.Artifact, string) {
			return newTar(
				"https://hakurei.app/seed/"+version+"/"+
					"stage0-"+triplet()+".tar.bz2",
				perArch[string]{
					"amd64":   "IQjFDkiAVLo1XzflgMMiLP3gnVY2hhDMTzl-QqJDCQhcLQ3lLtRzjI5WCxGyW_lk",
					"arm64":   "6fmwl2Umx2QssKQvxxb1JOGkAjzfA_MXKku0jVdGjYGb35OvwEVA5NYtd0HIy3yH",
					"riscv64": "Z2ODV0rIoo9iQRUIu35bsaOBeXc_9qQfGcyb2aGneatzNUJlXh5emSpEV2bOklUL",
				}.unwrap(),
				pkg.TarBzip2,
			), version
		},

		Name:        "stage0-dist",
		Description: "Rosa OS stage0 bootstrap seed",
	}
}

// HasStage0 returns whether a stage0 distribution is available.
func HasStage0() (ok bool) {
	func() {
		defer func() { ok = recover() == nil }()
		toolchainStage0.Load(stage0Dist)
	}()
	return
}
