/**
# Copyright (c) 2025, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package cudacompat

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/Masterminds/semver/v3"
	"github.com/google/uuid"
	"github.com/urfave/cli/v3"

	"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
	"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
)

const (
	defaultCudaCompatPath = "/usr/local/cuda/compat"
	// cudaCompatLdsoconfdFilenamePattern specifies the pattern for the filename
	// in ld.so.conf.d that includes a reference to the CUDA compat path.
	// The 00-compat prefix is chosen to ensure that these libraries have a
	// higher precedence than other libraries on the system.
	cudaCompatLdsoconfdFilenamePattern = "00-compat-*.conf"
)

type command struct {
	logger logger.Interface
}

type options struct {
	cudaCompatContainerRoot string
	hostDriverVersion       string
	hostCudaVersion         string
	// containerSpec allows the path to the container spec to be specified for
	// testing.
	containerSpec string
}

// NewCommand constructs a cuda-compat command with the specified logger
func NewCommand(logger logger.Interface) *cli.Command {
	c := command{
		logger: logger,
	}
	return c.build()
}

// build the enable-cuda-compat command
func (m command) build() *cli.Command {
	options := &options{}

	// Create the 'enable-cuda-compat' command
	c := cli.Command{
		Name:  "enable-cuda-compat",
		Usage: "This hook ensures that the folder containing the CUDA compat libraries is added to the ldconfig search path if required.",
		Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
			return ctx, m.validateFlags(cmd, options)
		},
		Action: func(ctx context.Context, cmd *cli.Command) error {
			return m.run(cmd, options)
		},
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:        "host-driver-version",
				Usage:       "Specify the host driver version. If the CUDA compat libraries detected in the container do not have a higher MAJOR version, the hook is a no-op.",
				Destination: &options.hostDriverVersion,
			},
			&cli.StringFlag{
				Name:        "host-cuda-version",
				Usage:       "Specify the CUDA version supported by the host driver.",
				Destination: &options.hostCudaVersion,
			},
			&cli.StringFlag{
				Name:        "cuda-compat-container-root",
				Usage:       "Specify the folder in which CUDA compat libraries are located in the container",
				Value:       defaultCudaCompatPath,
				Destination: &options.cudaCompatContainerRoot,
			},
			&cli.StringFlag{
				Name:        "container-spec",
				Hidden:      true,
				Category:    "testing-only",
				Usage:       "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN",
				Destination: &options.containerSpec,
			},
		},
	}

	return &c
}

func (m command) validateFlags(_ *cli.Command, _ *options) error {
	return nil
}

func (m command) run(_ *cli.Command, o *options) error {
	// If neither the host driver version nor the host cuda version is specified
	// the hook is a no-op.
	if o.hostDriverVersion == "" && o.hostCudaVersion == "" {
		return nil
	}

	s, err := oci.LoadContainerState(o.containerSpec)
	if err != nil {
		return fmt.Errorf("failed to load container state: %w", err)
	}

	containerRootDir, err := s.GetContainerRoot()
	if err != nil {
		return fmt.Errorf("failed to determined container root: %w", err)
	}

	containerRoot, err := newRoot(containerRootDir)
	if err != nil {
		return fmt.Errorf("failed to open container root: %w", err)
	}
	defer containerRoot.Close()

	containerForwardCompatDir, err := m.getContainerForwardCompatDir(containerRoot, o)
	if err != nil {
		return fmt.Errorf("failed to get container forward compat directory: %w", err)
	}
	if containerForwardCompatDir == "" {
		return nil
	}

	return m.createLdsoconfdFile(containerRoot, cudaCompatLdsoconfdFilenamePattern, containerForwardCompatDir)
}

func (m command) getContainerForwardCompatDir(containerRoot *root, o *options) (string, error) {
	if o.hostDriverVersion == "" && o.hostCudaVersion == "" {
		m.logger.Debugf("Neither a host driver version nor a host CUDA version was specified")
		return "", nil
	}
	if !containerRoot.hasPath(o.cudaCompatContainerRoot) {
		m.logger.Debugf("No CUDA forward compatibility libraries directory in container")
		return "", nil
	}

	libs, err := containerRoot.globFiles(filepath.Join(o.cudaCompatContainerRoot, "libcuda.so.*.*"))
	if err != nil {
		m.logger.Warningf("Failed to find CUDA compat library: %w", err)
		return "", nil
	}

	if len(libs) == 0 {
		m.logger.Debugf("No CUDA forward compatibility libraries container")
		return "", nil
	}

	if len(libs) != 1 {
		m.logger.Warningf("Unexpected number of CUDA compat libraries in container: %v", libs)
		return "", nil
	}

	libCudaCompatPath := libs[0]

	libCudaCompatFile, err := containerRoot.Open(libCudaCompatPath)
	if err != nil {
		return "", fmt.Errorf("failed to open CUDA compat library %q: %w", libCudaCompatPath, err)
	}
	defer libCudaCompatFile.Close()

	useCompatLibs, err := m.useCompatLibraries(libCudaCompatFile, o.hostDriverVersion, o.hostCudaVersion)
	if err != nil {
		return "", err
	}
	if !useCompatLibs {
		return "", nil
	}

	resolvedCompatDir := strings.TrimPrefix(filepath.Dir(libCudaCompatPath), containerRoot.Name())
	return resolvedCompatDir, nil
}

func (m command) useCompatLibraries(libcudaCompatFile *os.File, hostDriverVersion string, hostCUDAVersion string) (bool, error) {
	compatDriverVersion := strings.TrimPrefix(filepath.Base(libcudaCompatFile.Name()), "libcuda.so.")

	// Parse the version strings up front so that the checks below -- both against
	// the ELF header and the fallback major-version comparison -- operate on
	// well-formed semantic versions. If a version string cannot be parsed we log
	// a warning and leave it unset; the checks below account for missing versions.
	compatDriverSemver, err := semver.NewVersion(compatDriverVersion)
	if err != nil {
		m.logger.Warningf("failed to parse compat driver version %q: %v", compatDriverVersion, err)
	}

	hostDriverSemver, err := semver.NewVersion(hostDriverVersion)
	if err != nil {
		m.logger.Warningf("failed to parse host driver version %q: %v", hostDriverVersion, err)
	}

	hostCUDASemver, err := semver.NewVersion(hostCUDAVersion)
	if err != nil {
		m.logger.Warningf("failed to parse host CUDA version %q: %v", hostCUDAVersion, err)
	}

	// First check the ELF header of the libcuda.so included in the compat directory.
	// If this is present, we use the ELF header to determine whether the CUDA compat
	// libraries in the container should be used over the host driver libraries.
	cudaCompatHeader, err := GetCUDACompatElfHeaderFromReader(libcudaCompatFile)
	if err != nil {
		m.logger.Warningf("failed to get ELF header from CUDA compat library: %w", err)
	}
	if cudaCompatHeader != nil {
		return cudaCompatHeader.UseCompat(compatDriverSemver, hostDriverSemver, hostCUDASemver), nil
	}

	// If the host CUDA version is specified, we need to inspect the ELF header
	// of the compat libraries in the container to determine whether these
	// should be used. Return early if we cannot read the ELF header.
	if hostCUDAVersion != "" {
		return false, nil
	}

	// If we could not determine the host driver version or the compat driver
	// version, we don't use the CUDA compat libraries in the container.
	if hostDriverSemver == nil || compatDriverSemver == nil {
		return false, nil
	}

	// If we reach this point, it means we could not read the ELF header but
	// the host driver version is specified. We fall back to comparing the major
	// versions of the host driver and compat driver.
	if hostDriverSemver.Major() < compatDriverSemver.Major() {
		return true, nil
	}

	m.logger.Debugf("Compat major version is not greater than the host driver major version (%v >= %v)", hostDriverVersion, compatDriverVersion)
	return false, nil
}

// createLdsoconfdFile creates a file at /etc/ld.so.conf.d/ in the specified root.
// The file is created at /etc/ld.so.conf.d/{{ .pattern }} using `CreateTemp` and
// contains the specified directories on each line.
func (m command) createLdsoconfdFile(root *root, pattern string, dirs ...string) error {
	if len(dirs) == 0 {
		m.logger.Debugf("No directories to add to /etc/ld.so.conf")
		return nil
	}

	ldsoconfdDirPath := "/etc/ld.so.conf.d"
	if err := root.MkdirAll(ldsoconfdDirPath, 0755); err != nil {
		return fmt.Errorf("failed to create ld.so.conf.d: %w", err)
	}

	configFileName := strings.ReplaceAll(pattern, "*", uuid.NewString())
	configFile, err := root.Create(filepath.Join(ldsoconfdDirPath, configFileName))
	if err != nil {
		return fmt.Errorf("failed to create config file: %w", err)
	}
	defer configFile.Close()

	m.logger.Debugf("Adding directories %v to %v", dirs, configFile.Name())

	added := make(map[string]bool)
	for _, dir := range dirs {
		if added[dir] {
			continue
		}
		_, err = fmt.Fprintf(configFile, "%s\n", dir)
		if err != nil {
			return fmt.Errorf("failed to write to config file: %w", err)
		}
		added[dir] = true
	}

	// The created file needs to be world readable for the cases where the container is run as a non-root user.
	if err := configFile.Chmod(0644); err != nil {
		return fmt.Errorf("failed to chmod config file: %w", err)
	}

	return nil
}
