package sborepo

import (
	"fmt"

	"gitlab.com/M0M097/pkg/internal/must"
)

// PackageCreator holds the fields needed to create a new Package instance.
// It can be used as an anonymous struct to conveniently call its Create method
// without having to manually set each field. All omitted fields will be set to
// an empty string.
type PackageCreator struct {
	Name,
	Path,
	Version,
	Homepage,
	Download,
	MD5Sum,
	Download64,
	MD5Sum64,
	Requires,
	Maintainer,
	Email,
	SlackBuildContent,
	READMEContent,
	SlackDescContent string
}

// Create creates a new Package instance based on the provided PackageCreator
// fields. It generates the necessary directory structure and files for the
// package including the .info, README, and slack-desc files. It returns the
// generated Package instance.
func (pc PackageCreator) Create() Package {
	p := Package{Name: pc.Name, ParentPath: pc.Path}
	must.Mkdir(p.Path())
	must.Content2File(p.SlackBuild(), pc.SlackBuildContent)
	must.Content2File(p.Readme(), pc.READMEContent)
	must.Content2File(p.SlackDesc(), pc.SlackDescContent)
	f := must.Create(p.InfoFile())
	defer f.Close()
	fmt.Fprintf(f, `PRGNAM="%s"
VERSION="%s"
HOMEPAGE="%s"
DOWNLOAD="%s"
MD5SUM="%s"
DOWNLOAD_x86_64="%s"
MD5SUM_x86_64="%s"
REQUIRES="%s"
MAINTAINER="%s"
EMAIL="%s"
`,
		p.Name, pc.Version, pc.Homepage, pc.Download, pc.MD5Sum,
		pc.Download64, pc.MD5Sum64, pc.Requires, pc.Maintainer, pc.Email,
	)
	return p
}
