//go:build linux

package main

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

	"github.com/spf13/cobra"

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

type cmdAdminCluster struct {
	global *cmdGlobal
}

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
}

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)
}
