//go:build linux

package main

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/spf13/cobra"

	"github.com/lxc/incus/v6/cmd/incus/color"
	"github.com/lxc/incus/v6/internal/i18n"
	cli "github.com/lxc/incus/v6/shared/cmd"
	"github.com/lxc/incus/v6/shared/util"
)

type cmdAdminCluster struct {
	global *cmdGlobal
}

// Command returns a cobra.Command for use with (*cobra.Command).AddCommand.
func (c *cmdAdminCluster) Command() *cobra.Command {
	cmd := &cobra.Command{}
	cmd.Use = cli.U("cluster")
	cmd.Short = i18n.G("Low-level cluster administration commands")
	cmd.Long = cli.FormatSection(color.DescriptionPrefix, i18n.G(
		`Low level administration tools for inspecting and recovering clusters.`))

	cmd.Run = c.Run
	return cmd
}

// Run runs the actual command logic.
func (c *cmdAdminCluster) Run(_ *cobra.Command, args []string) {
	env := getEnviron()
	path, _ := exec.LookPath("incusd")
	if path == "" {
		if util.PathExists("/usr/libexec/incus/incusd") {
			path = "/usr/libexec/incus/incusd"
		} else if util.PathExists("/usr/lib/incus/incusd") {
			path = "/usr/lib/incus/incusd"
		} else if util.PathExists("/opt/incus/bin/incusd") {
			path = "/opt/incus/bin/incusd"
			env = append(env, "LD_LIBRARY_PATH=/opt/incus/lib/")
		}
	}

	if path == "" {
		fmt.Println(i18n.G(`The "cluster" subcommand requires access to internal server data.
To do so, it's actually part of the "incusd" binary rather than "incus".

You can invoke it through "incusd cluster".`))
		os.Exit(1) // nolint:revive
	}

	_ = doExec(path, append([]string{"incusd", "admin", "cluster"}, args...), env)
}
