package apksigner

import (
	"path/filepath"

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

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

// NewApkSignerLastVersion creates Apksigner from last available version in sdk build-tools
func NewApkSignerLastVersion(sdk sdk.SDK) (*Apksigner, error) {
	return apksignerFromPath(sdk.LastBuildToolsVersion())
}

// NewApksSignerSpecificVersion creates Apksigner from specified version in sdk build-tools
func NewApksSignerSpecificVersion(sdk sdk.SDK, version string) (*Apksigner, error) {
	return apksignerFromPath(sdk.SpecificBuildToolsVersion(version))
}
