//go:build linux && amd64
// +build linux,amd64

package ebpftracer

import (
	"embed"
	"fmt"
	"github.com/coroot/coroot-node-agent/flags"
	"golang.org/x/mod/semver"
	"os"
)

//go:embed ebpf/bin/amd64
var binData embed.FS

func EbpfCode(v string) (string, []byte, error) {
	if *flags.EbpfFilePath != "" {
		filePath := *flags.EbpfFilePath
		// Construct the full path to the desired file
		data, err := os.ReadFile(filePath)
		if err != nil {
			return "", nil, err
		}
		return filePath, data, nil
	}

	var filePath string
	var err error
	versions := []string{"v5.12", "v5.6", "v4.20", "v4.16"}
	for _, version := range versions {
		if semver.Compare(v, version) >= 0 {
			filePath = fmt.Sprintf("ebpf/bin/amd64/%s_amd64.o", version)
			break
		}
	}

	// Construct the full path to the desired file
	data, err := binData.ReadFile(filePath)
	if err != nil {
		return "", nil, err
	}
	return filePath, data, nil
}
