package rosa

import (
	"slices"
	"strings"

	"hakurei.app/internal/pkg"
)

func (t Toolchain) newPerl() (pkg.Artifact, string) {
	const (
		version  = "5.42.2"
		checksum = "Me_xFfgkRnVyG0sE6a74TktK2OUq9Z1LVJNEu_9RdZG3S2fbjfzNiuk2SJqHAgbm"
	)
	return t.NewPackage("perl", version, newTar(
		"https://www.cpan.org/src/5.0/perl-"+version+".tar.gz",
		checksum,
		pkg.TarGzip,
	), &PackageAttr{
		// uses source tree as scratch space
		Writable: true,
		Chmod:    true,

		ScriptEarly: `
echo 'print STDOUT "1..0 # Skip broken test\n";' > ext/Pod-Html/t/htmldir3.t
chmod +w /system/bin && rm -f /system/bin/ps # perl does not like toybox ps
`,

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

		ConfigureName: "./Configure",
		Configure: []KV{
			{"-des"},
			{"Dprefix", "/system"},
			{"Dcc", "clang"},
			{"Dcflags", "--std=gnu99"},
			{"Dldflags", `"${LDFLAGS:-''}"`},
			{"Doptimize", "'-O2 -fno-strict-aliasing'"},
			{"Duseithreads"},
			{"Duseshrplib"},
		},
		Check: []string{
			"TEST_JOBS=" + jobsLE,
			"test_harness",
		},
		Install: `LD_LIBRARY_PATH="$PWD" ./perl -Ilib -I. installperl --destdir=/work`,
	}), version
}
func init() {
	artifactsM[Perl] = Metadata{
		f: Toolchain.newPerl,

		Name:        "perl",
		Description: "The Perl Programming language",
		Website:     "https://www.perl.org/",

		ID: 13599,

		// odd-even versioning
		latest: (*Versions).getStable,
	}
}

// newViaPerlModuleBuild installs a perl module via Build.PL.
func (t Toolchain) newViaPerlModuleBuild(
	name, version string,
	source pkg.Artifact,
	patches []KV,
	extra ...PArtifact,
) pkg.Artifact {
	if name == "" || version == "" {
		panic("names must be non-empty")
	}
	return t.New("perl-"+name, 0, t.AppendPresets(nil,
		slices.Concat(P{Perl}, extra)...,
	), nil, nil, `
cd /usr/src/`+name+`
perl Build.PL --prefix=/system
./Build build
./Build test
./Build install --destdir=/work
`, pkg.Path(AbsUsrSrc.Append(name), true, t.NewPatchedSource(
		"perl-"+name, version, source, false, patches...,
	)))
}

func (t Toolchain) newPerlModuleBuild() (pkg.Artifact, string) {
	const (
		version  = "0.4234"
		checksum = "ZKxEFG4hE1rqZt52zBL2LRZBMkYzhjb5-cTBXcsyA52EbPeeYyVxU176yAea8-Di"
	)
	return t.newViaPerlModuleBuild("Module-Build", version, newTar(
		"https://cpan.metacpan.org/authors/id/L/LE/LEONT/"+
			"Module-Build-"+version+".tar.gz",
		checksum,
		pkg.TarGzip,
	), nil), version
}
func init() {
	artifactsM[PerlModuleBuild] = Metadata{
		f: Toolchain.newPerlModuleBuild,

		Name:        "perl-Module::Build",
		Description: "build and install Perl modules",
		Website:     "https://metacpan.org/release/Module-Build",

		Dependencies: P{
			Perl,
		},
	}
}

// newViaPerlMakeMaker installs a perl module via Makefile.PL.
func (t Toolchain) newViaPerlMakeMaker(
	name, version string,
	source pkg.Artifact,
	patches []KV,
	extra ...PArtifact,
) pkg.Artifact {
	return t.NewPackage("perl-"+name, version, source, &PackageAttr{
		// uses source tree as scratch space
		Writable:    true,
		Chmod:       true,
		EnterSource: true,

		Patches: patches,
	}, &MakeHelper{
		OmitDefaults: true,
		InPlace:      true,

		ConfigureName: "perl Makefile.PL",
		Configure: []KV{
			{"PREFIX", "/system"},
		},
		Check: []string{"test"},
	}, slices.Concat(extra, P{
		Perl,
	})...)
}

func (t Toolchain) newPerlLocaleGettext() (pkg.Artifact, string) {
	const (
		version  = "1.07"
		checksum = "cFq4BKFD1MWSoa7lsrPjpdo9kzPqd0jlRcBFUyL1L1isw8m3D_Sge_ff0MAu_9J3"
	)
	return t.newViaPerlMakeMaker("Locale::gettext", version, newFromCPAN(
		"PVANDRY",
		"Locale-gettext",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlLocaleGettext] = Metadata{
		f: Toolchain.newPerlLocaleGettext,

		Name:        "perl-Locale::gettext",
		Description: "message handling functions",
		Website:     "https://metacpan.org/release/Locale-gettext",

		ID: 7523,
	}
}

func (t Toolchain) newPerlPodParser() (pkg.Artifact, string) {
	const (
		version  = "1.67"
		checksum = "RdURu9mOfExk_loCp6abxlcQV3FycSNbTqhRS9i6JUqnYfGGEgercK30g0gjYyqe"
	)
	return t.newViaPerlMakeMaker("Pod::Parser", version, newFromCPAN(
		"MAREKR",
		"Pod-Parser",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlPodParser] = Metadata{
		f: Toolchain.newPerlPodParser,

		Name:        "perl-Pod::Parser",
		Description: "base class for creating POD filters and translators",
		Website:     "https://metacpan.org/release/Pod-Parser",

		ID: 3244,
	}
}

func (t Toolchain) newPerlSGMLS() (pkg.Artifact, string) {
	const (
		version  = "1.1"
		checksum = "aZijn4MUqD-wfyZgdcCruCwl4SgDdu25cNmJ4_UvdAk9a7uz4gzMQdoeB6DQ6QOy"
	)
	return t.newViaPerlMakeMaker("SGMLS", version, newFromCPAN(
		"RAAB",
		"SGMLSpm",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlSGMLS] = Metadata{
		f: Toolchain.newPerlSGMLS,

		Name:        "perl-SGMLS",
		Description: "class for postprocessing the output from the sgmls and nsgmls parsers",
		Website:     "https://metacpan.org/release/RAAB/SGMLSpm-1.1",

		ID: 389576,

		latest: func(v *Versions) string {
			for _, s := range v.Stable {
				_, m, ok := strings.Cut(s, ".")
				if !ok {
					continue
				}
				if len(m) > 1 && m[0] == '0' {
					continue
				}
				return s
			}
			return v.Latest
		},
	}
}

func (t Toolchain) newPerlTermReadKey() (pkg.Artifact, string) {
	const (
		version  = "2.38"
		checksum = "qerL8Xo7kD0f42PZoiEbmE8Roc_S9pOa27LXelY4DN_0UNy_u5wLrGHI8utNlaiI"
	)
	return t.newViaPerlMakeMaker("Term::ReadKey", version, newFromCPAN(
		"JSTOWE",
		"TermReadKey",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlTermReadKey] = Metadata{
		f: Toolchain.newPerlTermReadKey,

		Name:        "perl-Term::ReadKey",
		Description: "a perl module for simple terminal control",
		Website:     "https://metacpan.org/release/TermReadKey",

		ID: 3372,
	}
}

func (t Toolchain) newPerlTextCharWidth() (pkg.Artifact, string) {
	const (
		version  = "0.04"
		checksum = "G2p5RHU4_HiZ23ZusBA_enTlVMxz0J4esUx4CGcOPhY6xYTbp-aXWRN6lYZpzBw2"
	)
	return t.newViaPerlMakeMaker("Text::CharWidth", version, newFromCPAN(
		"KUBOTA",
		"Text-CharWidth",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlTextCharWidth] = Metadata{
		f: Toolchain.newPerlTextCharWidth,

		Name:        "perl-Text::CharWidth",
		Description: "get number of occupied columns of a string on terminal",
		Website:     "https://metacpan.org/release/Text-CharWidth",

		ID: 14380,
	}
}

func (t Toolchain) newPerlTextWrapI18N() (pkg.Artifact, string) {
	const (
		version  = "0.06"
		checksum = "Vmo89qLgxUqyQ6QmWJVqu60aQAUjrNKRjFQSXGnvClxofzRjiCa6idzPgJ4VkixM"
	)
	return t.newViaPerlMakeMaker("Text::WrapI18N", version, newFromCPAN(
		"KUBOTA",
		"Text-WrapI18N",
		version,
		checksum,
	), nil,
		PerlTextCharWidth,
	), version
}
func init() {
	artifactsM[PerlTextWrapI18N] = Metadata{
		f: Toolchain.newPerlTextWrapI18N,

		Name:        "perl-Text::WrapI18N",
		Description: "line wrapping module",
		Website:     "https://metacpan.org/release/Text-WrapI18N",

		Dependencies: P{
			PerlTextCharWidth,
		},

		ID: 14385,
	}
}

func (t Toolchain) newPerlMIMECharset() (pkg.Artifact, string) {
	const (
		version  = "1.013.1"
		checksum = "Ou_ukcrOa1cgtE3mptinb-os3bdL1SXzbRDFZQF3prrJj-drc3rp_huay7iDLJol"
	)
	return t.newViaPerlMakeMaker("MIME::Charset", version, newFromCPAN(
		"NEZUMI",
		"MIME-Charset",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlMIMECharset] = Metadata{
		f: Toolchain.newPerlMIMECharset,

		Name:        "perl-MIME::Charset",
		Description: "Charset Information for MIME",
		Website:     "https://metacpan.org/release/MIME-Charset",

		ID: 3070,
	}
}

func (t Toolchain) newPerlUnicodeLineBreak() (pkg.Artifact, string) {
	const (
		version  = "2019.001"
		checksum = "ZHVkh7EDgAUHnTpvXsnPAuWpgNoBImtY_9_8TIbo2co_WgUwEb0MtXPhI8pAZ5OH"
	)
	return t.newViaPerlMakeMaker("Unicode::LineBreak", version, newFromCPAN(
		"NEZUMI",
		"Unicode-LineBreak",
		version,
		checksum,
	), nil,
		PerlMIMECharset,
	), version
}
func init() {
	artifactsM[PerlUnicodeLineBreak] = Metadata{
		f: Toolchain.newPerlUnicodeLineBreak,

		Name:        "perl-Unicode::LineBreak",
		Description: "String as Sequence of UAX #29 Grapheme Clusters",
		Website:     "https://metacpan.org/release/Unicode-LineBreak",

		Dependencies: P{
			PerlMIMECharset,
		},

		ID: 6033,
	}
}

func (t Toolchain) newPerlYAMLTiny() (pkg.Artifact, string) {
	const (
		version  = "1.76"
		checksum = "V1MV4KPym1LxSw8CRXqPR3K-l1hGHbT5Ob4t-9xju6R9X_CWyw6hI8wsMaNdHdBY"
	)
	return t.newViaPerlMakeMaker("YAML::Tiny", version, newFromCPAN(
		"ETHER",
		"YAML-Tiny",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlYAMLTiny] = Metadata{
		f: Toolchain.newPerlYAMLTiny,

		Name:        "perl-YAML::Tiny",
		Description: "read/write YAML files with as little code as possible",
		Website:     "https://metacpan.org/release/YAML-Tiny",

		ID: 3549,
	}
}

func (t Toolchain) newPerlTestCmd() (pkg.Artifact, string) {
	const (
		version  = "1.09"
		checksum = "gpGUwyC9IozDiYSgW_kXARNfXsTPFa6cTowJmmCBbPqcs2-pONZca_SB06FGy-7H"
	)
	return t.newViaPerlMakeMaker("Test::Cmd", version, newFromCPAN(
		"NEILB",
		"Test-Cmd",
		version,
		checksum,
	), nil), version
}
func init() {
	artifactsM[PerlTestCmd] = Metadata{
		f: Toolchain.newPerlTestCmd,

		Name:        "perl-Test::Cmd",
		Description: "portable testing of commands and scripts",
		Website:     "https://metacpan.org/release/Test-Cmd",

		ID: 6014,
	}
}
