package soft

import (
	"fmt"
	"os"
	"os/exec"
	"sunhpc/internal/log"
	"sunhpc/pkg/utils"
)

// installViaPackageManager 使用系统包管理器安装软件
func installViaPackageManager(name, pkgType string) error {
	var cmd *exec.Cmd
	switch pkgType {
	case "rpm":
		// RHEL/CentOS
		if utils.CommandExists("yum") {
			cmd = exec.Command("yum", "install", "-y", name)
		} else if utils.CommandExists("dnf") {
			cmd = exec.Command("dnf", "install", "-y", name)
		} else {
			return fmt.Errorf("未找到 yum 或 dnf 包管理器")
		}
	case "deb":
		// Debian/Ubuntu
		if !utils.CommandExists("apt-get") {
			return fmt.Errorf("未找到 apt-get 包管理器")
		}
		cmd = exec.Command("apt-get", "install", "-y", name)
	default:
		return fmt.Errorf("不支持的包类型: %s", pkgType)
	}

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	log.Infof("执行命令: %s", cmd.String())
	return cmd.Run()
}
