func (p *Package) Equals(p2 *Package) bool {
	return p.Name == p2.Name && p.Version == p2.Version && p.SourceArchitecture == p2.SourceArchitecture &&
		p.Architecture == p2.Architecture && p.Source == p2.Source && p.IsSource == p2.IsSource &&
		p.FilesHash == p2.FilesHash
}
Equals compares two packages to be identical
package deb
import (
	gocontext "context"
	"encoding/json"
	"fmt"
	"path/filepath"
	"strconv"
	"strings"
	"github.com/aptly-dev/aptly/aptly"
	"github.com/aptly-dev/aptly/utils"
)
type Package struct {
	Name         string
	Version      string
	Architecture string
	SourceArchitecture string
	Source string
	Provides []string
	FilesHash uint64
	IsInstaller bool
	IsSource bool
	IsUdeb bool
	V06Plus bool
	deps     *PackageDependencies
	extra    *Stanza
	files    *PackageFiles
	contents []string
	collection *PackageCollection
}
const (
	PackageTypeBinary    = "deb"
	PackageTypeUdeb      = "udeb"
	PackageTypeSource    = "source"
	PackageTypeInstaller = "installer"
)
const (
	ArchitectureAll    = "all"
	ArchitectureAny    = "any"
	ArchitectureSource = "source"
)
var (
	_ json.Marshaler = &Package{}
)
func NewPackageFromControlFile(input Stanza) *Package {
	result := &Package{
		Name:         input["Package"],
		Version:      input["Version"],
		Architecture: input["Architecture"],
		Source:       input["Source"],
		V06Plus:      true,
	}
	delete(input, "Package")
	delete(input, "Version")
	delete(input, "Architecture")
	delete(input, "Source")
	filesize, _ := strconv.ParseInt(input["Size"], 10, 64)
	md5, ok := input["MD5sum"]
	if !ok {
		md5 = input["MD5Sum"]
	}
	result.UpdateFiles(PackageFiles{PackageFile{
		Filename:     filepath.Base(input["Filename"]),
		downloadPath: filepath.Dir(input["Filename"]),
		Checksums: utils.ChecksumInfo{
			Size:   filesize,
			MD5:    strings.TrimSpace(md5),
			SHA1:   strings.TrimSpace(input["SHA1"]),
			SHA256: strings.TrimSpace(input["SHA256"]),
			SHA512: strings.TrimSpace(input["SHA512"]),
		},
	}})
	delete(input, "Filename")
	delete(input, "MD5sum")
	delete(input, "MD5Sum")
	delete(input, "SHA1")
	delete(input, "SHA256")
	delete(input, "SHA512")
	delete(input, "Size")
	depends := &PackageDependencies{}
	depends.Depends = parseDependencies(input, "Depends")
	depends.PreDepends = parseDependencies(input, "Pre-Depends")
	depends.Suggests = parseDependencies(input, "Suggests")
	depends.Recommends = parseDependencies(input, "Recommends")
	result.deps = depends
	result.Provides = parseDependencies(input, "Provides")
	result.extra = &input
	return result
}
func NewSourcePackageFromControlFile(input Stanza) (*Package, error) {
	result := &Package{
		IsSource:           true,
		Name:               input["Package"],
		Version:            input["Version"],
		Architecture:       "source",
		SourceArchitecture: input["Architecture"],
		V06Plus:            true,
	}
	delete(input, "Package")
	delete(input, "Version")
	delete(input, "Architecture")
	var err error
	files := make(PackageFiles, 0, 3)
	files, err = files.ParseSumFields(input)
	if err != nil {
		return nil, err
	}
	delete(input, "Files")
	delete(input, "Checksums-Sha1")
	delete(input, "Checksums-Sha256")
	for i := range files {
		files[i].downloadPath = input["Directory"]
	}
	result.UpdateFiles(files)
	depends := &PackageDependencies{}
	depends.BuildDepends = parseDependencies(input, "Build-Depends")
	depends.BuildDependsInDep = parseDependencies(input, "Build-Depends-Indep")
	result.deps = depends
	result.extra = &input
	return result, nil
}
func NewUdebPackageFromControlFile(input Stanza) *Package {
	p := NewPackageFromControlFile(input)
	p.IsUdeb = true
	return p
}
func NewInstallerPackageFromControlFile(input Stanza, repo *RemoteRepo, component, architecture string, d aptly.Downloader) (*Package, error) {
	p := &Package{
		Name:         "installer",
		Architecture: architecture,
		IsInstaller:  true,
		V06Plus:      true,
		extra:        &Stanza{},
		deps:         &PackageDependencies{},
	}
	files := make(PackageFiles, 0)
	files, err := files.ParseSumField(input[""], func(sum *utils.ChecksumInfo, data string) { sum.SHA256 = data }, false, false)
	if err != nil {
		return nil, err
	}
	relPath := filepath.Join("dists", repo.Distribution, component, fmt.Sprintf("%s-%s", p.Name, architecture), "current", "images")
	for i := range files {
		files[i].downloadPath = relPath
		url := repo.PackageURL(files[i].DownloadURL()).String()
		var size int64
		size, err = d.GetLength(gocontext.TODO(), url)
		if err != nil {
			return nil, err
		}
		files[i].Checksums.Size = size
	}
	p.UpdateFiles(files)
	return p, nil
}
func (p *Package) Key(prefix string) []byte {
	if p.V06Plus {
		return []byte(fmt.Sprintf("%sP%s %s %s %08x", prefix, p.Architecture, p.Name, p.Version, p.FilesHash))
	}
	return p.ShortKey(prefix)
}
func (p *Package) ShortKey(prefix string) []byte {
	return []byte(fmt.Sprintf("%sP%s %s %s", prefix, p.Architecture, p.Name, p.Version))
}
func (p *Package) String() string {
	return fmt.Sprintf("%s_%s_%s", p.Name, p.Version, p.Architecture)
}
func (p *Package) ExtendedStanza() Stanza {
	stanza := p.Stanza()
	stanza["FilesHash"] = fmt.Sprintf("%08x", p.FilesHash)
	stanza["Key"] = string(p.Key(""))
	stanza["ShortKey"] = string(p.ShortKey(""))
	return stanza
}
func (p *Package) MarshalJSON() ([]byte, error) {
	return json.Marshal(p.ExtendedStanza())
}
func (p *Package) GetField(name string) string {
	switch name {
	case "$Source":
		if p.IsSource {
			return ""
		}
		source := p.Source
		if source == "" {
			return p.Name
		} else if pos := strings.Index(source, "("); pos != -1 {
			return strings.TrimSpace(source[:pos])
		}
		return source
	case "$SourceVersion":
		if p.IsSource {
			return ""
		}
		source := p.Source
		if pos := strings.Index(source, "("); pos != -1 {
			if pos2 := strings.LastIndex(source, ")"); pos2 != -1 && pos2 > pos {
				return strings.TrimSpace(source[pos+1 : pos2])
			}
		}
		return p.Version
	case "$Architecture":
		return p.Architecture
	case "$PackageType":
		if p.IsSource {
			return PackageTypeSource
		}
		if p.IsUdeb {
			return PackageTypeUdeb
		}
		return PackageTypeBinary
	case "Name":
		return p.Name
	case "Version":
		return p.Version
	case "Architecture":
		if p.IsSource {
			return p.SourceArchitecture
		}
		return p.Architecture
	case "Source":
		return p.Source
	case "Depends":
		return strings.Join(p.Deps().Depends, ", ")
	case "Pre-Depends":
		return strings.Join(p.Deps().PreDepends, ", ")
	case "Suggests":
		return strings.Join(p.Deps().Suggests, ", ")
	case "Recommends":
		return strings.Join(p.Deps().Recommends, ", ")
	case "Provides":
		return strings.Join(p.Provides, ", ")
	case "Build-Depends":
		return strings.Join(p.Deps().BuildDepends, ", ")
	case "Build-Depends-Indep":
		return strings.Join(p.Deps().BuildDependsInDep, ", ")
	default:
		return p.Extra()[name]
	}
}
func (p *Package) MatchesArchitecture(arch string) bool {
	if p.Architecture == ArchitectureAll && arch != ArchitectureSource {
		return true
	}
	return p.Architecture == arch
}
func (p *Package) MatchesDependency(dep Dependency) bool {
	if dep.Architecture != "" && !p.MatchesArchitecture(dep.Architecture) {
		return false
	}
	if dep.Relation == VersionDontCare {
		if utils.StrSliceHasItem(p.Provides, dep.Pkg) {
			return true
		}
		return dep.Pkg == p.Name
	}
	if dep.Pkg != p.Name {
		return false
	}
	r := CompareVersions(p.Version, dep.Version)
	switch dep.Relation {
	case VersionEqual:
		return r == 0
	case VersionLess:
		return r < 0
	case VersionGreater:
		return r > 0
	case VersionLessOrEqual:
		return r <= 0
	case VersionGreaterOrEqual:
		return r >= 0
	case VersionPatternMatch:
		matched, err := filepath.Match(dep.Version, p.Version)
		return err == nil && matched
	case VersionRegexp:
		return dep.Regexp.FindStringIndex(p.Version) != nil
	}
	panic("unknown relation")
}
func (p *Package) GetName() string {
	return p.Name
}
func (p *Package) GetFullName() string {
	return strings.Join([]string{p.Name, p.Version, p.Architecture}, "_")
}
func (p *Package) GetVersion() string {
	return p.Version
}
func (p *Package) GetArchitecture() string {
	return p.Architecture
}
func (p *Package) GetDependencies(options int) (dependencies []string) {
	deps := p.Deps()
	dependencies = make([]string, 0, 30)
	dependencies = append(dependencies, deps.Depends...)
	dependencies = append(dependencies, deps.PreDepends...)
	if options&DepFollowRecommends == DepFollowRecommends {
		dependencies = append(dependencies, deps.Recommends...)
	}
	if options&DepFollowSuggests == DepFollowSuggests {
		dependencies = append(dependencies, deps.Suggests...)
	}
	if options&DepFollowBuild == DepFollowBuild {
		dependencies = append(dependencies, deps.BuildDepends...)
		dependencies = append(dependencies, deps.BuildDependsInDep...)
	}
	if options&DepFollowSource == DepFollowSource {
		source := p.Source
		if source == "" {
			source = p.Name
		}
		if strings.Contains(source, ")") {
			dependencies = append(dependencies, fmt.Sprintf("%s {source}", source))
		} else {
			dependencies = append(dependencies, fmt.Sprintf("%s (= %s) {source}", source, p.Version))
		}
	}
	return
}
func (p *Package) QualifiedName() string {
	section := p.Extra()["Section"]
	if section != "" {
		return section + "/" + p.Name
	}
	return p.Name
}
func (p *Package) Extra() Stanza {
	if p.extra == nil {
		if p.collection == nil {
			panic("extra == nil && collection == nil")
		}
		p.extra = p.collection.loadExtra(p)
	}
	return *p.extra
}
func (p *Package) Deps() *PackageDependencies {
	if p.deps == nil {
		if p.collection == nil {
			panic("deps == nil && collection == nil")
		}
		p.deps = p.collection.loadDependencies(p)
	}
	return p.deps
}
func (p *Package) Files() PackageFiles {
	if p.files == nil {
		if p.collection == nil {
			panic("files == nil && collection == nil")
		}
		p.files = p.collection.loadFiles(p)
	}
	return *p.files
}
func (p *Package) Contents(packagePool aptly.PackagePool, progress aptly.Progress) []string {
	if p.IsSource {
		return nil
	}
	return p.collection.loadContents(p, packagePool, progress)
}
func (p *Package) CalculateContents(packagePool aptly.PackagePool, progress aptly.Progress) ([]string, error) {
	if p.IsSource {
		return nil, nil
	}
	file := p.Files()[0]
	poolPath, err := file.GetPoolPath(packagePool)
	if err != nil {
		if progress != nil {
			progress.ColoredPrintf("@y[!]@| @!Failed to build pool path: @| %s", err)
		}
		return nil, err
	}
	reader, err := packagePool.Open(poolPath)
	if err != nil {
		if progress != nil {
			progress.ColoredPrintf("@y[!]@| @!Failed to open package in pool: @| %s", err)
		}
		return nil, err
	}
	defer reader.Close()
	contents, err := GetContentsFromDeb(reader, file.Filename)
	if err != nil {
		if progress != nil {
			progress.ColoredPrintf("@y[!]@| @!Failed to generate package contents: @| %s", err)
		}
		return nil, err
	}
	return contents, nil
}
func (p *Package) UpdateFiles(files PackageFiles) {
	p.files = &files
	p.FilesHash = files.Hash()
}
func (p *Package) Stanza() (result Stanza) {
	result = p.Extra().Copy()
	result["Package"] = p.Name
	result["Version"] = p.Version
	if p.IsSource {
		result["Architecture"] = p.SourceArchitecture
	} else {
		result["Architecture"] = p.Architecture
		if p.Source != "" {
			result["Source"] = p.Source
		}
	}
	if p.IsSource {
		md5, sha1, sha256, sha512 := []string{}, []string{}, []string{}, []string{}
		for _, f := range p.Files() {
			if f.Checksums.MD5 != "" {
				md5 = append(md5, fmt.Sprintf(" %s %d %s\n", f.Checksums.MD5, f.Checksums.Size, f.Filename))
			}
			if f.Checksums.SHA1 != "" {
				sha1 = append(sha1, fmt.Sprintf(" %s %d %s\n", f.Checksums.SHA1, f.Checksums.Size, f.Filename))
			}
			if f.Checksums.SHA256 != "" {
				sha256 = append(sha256, fmt.Sprintf(" %s %d %s\n", f.Checksums.SHA256, f.Checksums.Size, f.Filename))
			}
			if f.Checksums.SHA512 != "" {
				sha512 = append(sha512, fmt.Sprintf(" %s %d %s\n", f.Checksums.SHA512, f.Checksums.Size, f.Filename))
			}
		}
		result["Files"] = strings.Join(md5, "")
		if len(sha1) > 0 {
			result["Checksums-Sha1"] = strings.Join(sha1, "")
		}
		if len(sha256) > 0 {
			result["Checksums-Sha256"] = strings.Join(sha256, "")
		}
		if len(sha512) > 0 {
			result["Checksums-Sha512"] = strings.Join(sha512, "")
		}
	} else if p.IsInstaller {
		sha256 := []string{}
		for _, f := range p.Files() {
			sha256 = append(sha256, fmt.Sprintf("%s  %s", f.Checksums.SHA256, f.Filename))
		}
		result[""] = strings.Join(sha256, "\n")
	} else {
		f := p.Files()[0]
		result["Filename"] = f.DownloadURL()
		if f.Checksums.MD5 != "" {
			result["MD5sum"] = f.Checksums.MD5
		}
		if f.Checksums.SHA1 != "" {
			result["SHA1"] = f.Checksums.SHA1
		}
		if f.Checksums.SHA256 != "" {
			result["SHA256"] = f.Checksums.SHA256
		}
		if f.Checksums.SHA512 != "" {
			result["SHA512"] = f.Checksums.SHA512
		}
		result["Size"] = fmt.Sprintf("%d", f.Checksums.Size)
	}
	deps := p.Deps()
	if deps.Depends != nil {
		result["Depends"] = strings.Join(deps.Depends, ", ")
	}
	if deps.PreDepends != nil {
		result["Pre-Depends"] = strings.Join(deps.PreDepends, ", ")
	}
	if deps.Suggests != nil {
		result["Suggests"] = strings.Join(deps.Suggests, ", ")
	}
	if deps.Recommends != nil {
		result["Recommends"] = strings.Join(deps.Recommends, ", ")
	}
	if p.Provides != nil {
		result["Provides"] = strings.Join(p.Provides, ", ")
	}
	if deps.BuildDepends != nil {
		result["Build-Depends"] = strings.Join(deps.BuildDepends, ", ")
	}
	if deps.BuildDependsInDep != nil {
		result["Build-Depends-Indep"] = strings.Join(deps.BuildDependsInDep, ", ")
	}
	return
}