package platform

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

func (s *System) TailFile(path string, lines int) string {
	raw, err := os.ReadFile(path)
	if err != nil {
		return fmt.Sprintf("read %s: %v", path, err)
	}
	all := strings.Split(strings.TrimRight(string(raw), "\n"), "\n")
	if lines <= 0 || len(all) <= lines {
		return string(raw)
	}
	return strings.Join(all[len(all)-lines:], "\n")
}

func (s *System) CheckTools(names []string) []ToolStatus {
	out := make([]ToolStatus, 0, len(names))
	for _, name := range names {
		path, err := exec.LookPath(name)
		out = append(out, ToolStatus{Name: name, Path: path, OK: err == nil})
	}
	return out
}
