package soft

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

// extractBinary 解压二进制压缩包到目标目录
func extractBinary(binPath, destDir string) error {
	// 确保目标目录存在
	if err := os.MkdirAll(destDir, 0755); err != nil {
		return err
	}

	// 根据扩展名选择解压命令
	var cmd *exec.Cmd
	switch {
	case strings.HasSuffix(binPath, ".tar.gz"), strings.HasSuffix(binPath, ".tgz"):
		cmd = exec.Command("tar", "xzf", binPath, "-C", destDir)
	case strings.HasSuffix(binPath, ".tar.bz2"):
		cmd = exec.Command("tar", "xjf", binPath, "-C", destDir)
	case strings.HasSuffix(binPath, ".zip"):
		cmd = exec.Command("unzip", binPath, "-d", destDir)
	default:
		return fmt.Errorf("不支持的压缩格式: %s", binPath)
	}

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}
