// Package detect implements nest's environment and install detection logic
// (AGENTS.md §5.4).
package detect

import "os"

// InContainer reports whether nest is running inside a container (§5.4).
func InContainer(getenv func(string) string) bool {
	if getenv("container") != "" {
		return true
	}
	for _, path := range []string{"/.dockerenv", "/run/.containerenv"} {
		if _, err := os.Stat(path); err == nil {
			return true
		}
	}
	return false
}

// Executable reports whether path exists and is executable.
func Executable(path string) bool {
	st, err := os.Stat(path)
	return err == nil && !st.IsDir() && st.Mode().Perm()&0o111 != 0
}

// AnyExecutable reports whether any of the paths exists and is executable.
// That is the install-detection rule for catalog tools (§5.4).
func AnyExecutable(paths []string) bool {
	for _, p := range paths {
		if Executable(p) {
			return true
		}
	}
	return false
}
