package rosa

import "hakurei.app/internal/pkg"

func (t Toolchain) newToybox(suffix, script string) (pkg.Artifact, string) {
	const (
		version  = "0.8.13"
		checksum = "rZ1V1ATDte2WeQZanxLVoiRGdfPXhMlEo5-exX-e-ml8cGn9qOv0ABEUVZpX3wTI"
	)
	return t.NewPackage("toybox"+suffix, version, newTar(
		"https://landley.net/toybox/downloads/toybox-"+version+".tar.gz",
		checksum,
		pkg.TarGzip,
	), &PackageAttr{
		// uses source tree as scratch space
		Writable:    true,
		EnterSource: true,

		Flag: TEarly,
	}, &MakeHelper{
		OmitDefaults:  true,
		InPlace:       true,
		SkipConfigure: true,

		ScriptMakeEarly: `
LDFLAGS="${LDFLAGS:-''} -static"

chmod +w /bin/
ln -rs "$(which bash)" /bin/ || true

chmod +w kconfig tests
rm \
	tests/du.test \
	tests/sed.test \
	tests/tar.test \
	tests/ls.test \
	tests/taskset.test

make defconfig
sed -i \
	's/^CONFIG_TOYBOX_ZHELP=y$/CONFIG_TOYBOX_ZHELP=0/' \
	.config
` + script,
		SkipCheck: t.isStage0(),
		Check: []string{
			"USER=cure",
			"tests",
		},
		Install: "PREFIX=/work/system/bin make install_flat",
		Script: `
mkdir -p /work/usr/bin
ln -s ../../system/bin/env /work/usr/bin
`,
	},
		Bash,
		Gzip,

		KernelHeaders,
	), version
}
func init() {
	artifactsM[Toybox] = Metadata{
		f: func(t Toolchain) (pkg.Artifact, string) {
			return t.newToybox("", "")
		},

		Name:        "toybox",
		Description: "many common Linux command line utilities",
		Website:     "https://landley.net/toybox/",

		ID: 13818,
	}

	artifactsM[toyboxEarly] = Metadata{
		f: func(t Toolchain) (pkg.Artifact, string) {
			return t.newToybox("-early", `
echo '
CONFIG_EXPR=y
CONFIG_TR=y
CONFIG_AWK=y
CONFIG_DIFF=y
' >> .config
`)
		},

		Name:        "toybox-early",
		Description: "a build of toybox with unfinished tools enabled to break dependency loops",
		Website:     "https://landley.net/toybox/",
	}
}
