package zipalign

import (
	"path/filepath"

	"notabug.org/Umnik/GoAndroidSDK/v2/components/misc"
	"notabug.org/Umnik/GoAndroidSDK/v2/components/sdk"
)

func zipalignFromPath(dirPath string, err error) (*Zipalign, error) {
	if err != nil {
		return nil, err
	}
	binPath := filepath.Join(dirPath, "zipalign")
	if err = misc.TestFile(binPath); err != nil {
		return nil, err
	}
	return &Zipalign{binPath: binPath}, nil
}

// NewZipalignLastVersion creates Zipalign from last available build-tools version
func NewZipalignLastVersion(sdk sdk.SDK) (*Zipalign, error) {
	return zipalignFromPath(sdk.LastBuildToolsVersion())
}

// NewZipalignSpecificVersion creates Zipalign from specified build-tools version
func NewZipalignSpecificVersion(sdk sdk.SDK, version string) (*Zipalign, error) {
	return zipalignFromPath(sdk.SpecificBuildToolsVersion(version))
}
