package cmd

import (
	"bytes"
	"fmt"
	"github.com/33cn/chain33/common/log"
	"os/exec"
	"strings"
)

var cmdLog = log.New("pkg", "app")

func GetLocalArchType() string {
	cmd := exec.Command("arch")
	var buf bytes.Buffer
	cmd.Stdout = &buf
	err := cmd.Run()
	if err != nil {
		cmdLog.Error("GetLocalArchType", "err", err)
		return ""
	}
	if buf.String() != "" {
		lines := strings.Split(buf.String(), "\n")
		if len(lines) == 2 {
			return strings.Trim(lines[0], " ")
		}
	}
	return ""
}

func GetLocalUUID() string {
	cmd := exec.Command("dmidecode")
	var buf, errbuf bytes.Buffer
	cmd.Stdout = &buf
	cmd.Stderr = &errbuf
	err := cmd.Start()
	if err != nil {
		fmt.Println("Start", err)
	}
	err = cmd.Wait()
	if err != nil {
		fmt.Println("Wait", err)
	}
	fmt.Println("err", errbuf.String())
	fmt.Println("uuid", buf.String())
	if buf.String() != "" {
		lines := strings.Split(buf.String(), "\n")
		if len(lines) > 0 {
			for _, l := range lines {
				if strings.Contains(l, "UUID") {
					return strings.Trim(l, "\t")
				}
			}

		}
	}
	return ""
}

