package config

import (
    "path"
    "gitea.sunhpc.com/kelvin/sunhpc/internal/pkg/util"
)

var ConfigFile = "/etc/sunhpc/sunhpc.conf"

type BuildConfig struct {
	Bindir         string `yaml:"bindir,omitempty" default:"@BINDIR@"`
	Sysconfdir     string `yaml:"sysconfdir,omitempty" default:"@SYSCONFDIR@"`
	Localstatedir  string `yaml:"localstatedir,omitempty" default:"@LOCALSTATEDIR@"`
	Cachedir       string `yaml:"cachedir,omitempty" default:"@CACHEDIR@"`
	Ipxesource     string `yaml:"ipxesource,omitempty" default:"@IPXESOURCE@"`
	Srvdir         string `yaml:"srvdir,omitempty" default:"@SRVDIR@"`
	Firewallddir   string `yaml:"firewallddir,omitempty" default:"@FIREWALLDDIR@"`
	Systemddir     string `yaml:"systemddir,omitempty" default:"@SYSTEMDDIR@"`
	Datadir        string `yaml:"datadir,omitempty" default:"@DATADIR@"`
	WWOverlaydir   string `yaml:"wwoverlaydir,omitempty" default:"@WWOVERLAYDIR@"`
	WWChrootdir    string `yaml:"wwchrootdir,omitempty" default:"@WWCHROOTDIR@"`
	WWProvisiondir string `yaml:"wwprovisiondir,omitempty" default:"@WWPROVISIONDIR@"`
	WWClientdir    string `yaml:"wwclientdir,omitempty" default:"@WWCLIENTDIR@"`
}

const Version = "1.0.0"
const Release = "1"

type TFTPConf struct {
	EnabledP    *bool  `yaml:"enabled" default:"true"`
	TftpRoot    string `yaml:"tftproot,omitempty" default:"@TFTPDIR@"`
	SystemdName string `yaml:"systemd name,omitempty" default:"tftp"`

	IpxeBinaries map[string]string `yaml:"ipxe,omitempty" default:"{\"00:09\": \"ipxe-snponly-x86_64.efi\",\"00:00\": \"undionly.kpxe\",\"00:0B\": \"arm64-efi/snponly.efi\",\"00:07\":  \"ipxe-snponly-x86_64.efi\"}"`
}

func (conf TFTPConf) Enabled() bool {
	return util.BoolP(conf.EnabledP)
}

// SunhpcConf adds additional Sunhpc-specific configuration to
// BaseConf.
type SunhpcConf struct {
	Port               int    `yaml:"port,omitempty" default:"9873"`
	TLSPort            int    `yaml:"tls port,omitempty" default:"9874"`
	SecureP            *bool  `yaml:"secure,omitempty" default:"true"`
	TLSEnabledP        *bool  `yaml:"tls,omitempty"`
	UpdateInterval     int    `yaml:"update interval,omitempty" default:"60"`
	AutobuildOverlaysP *bool  `yaml:"autobuild overlays,omitempty" default:"true"`
	EnableHostOverlayP *bool  `yaml:"host overlay,omitempty" default:"true"`
	GrubBootP          *bool  `yaml:"grubboot,omitempty" default:"false"`
	SystemdName        string `yaml:"systemd name,omitempty"`
}

func (conf SunhpcConf) Secure() bool {
	return util.BoolP(conf.SecureP)
}

func (conf SunhpcConf) TLSEnabled() bool {
	return util.BoolP(conf.TLSEnabledP)
}

func (conf SunhpcConf) AutobuildOverlays() bool {
	return util.BoolP(conf.AutobuildOverlaysP)
}

func (conf SunhpcConf) EnableHostOverlay() bool {
	return util.BoolP(conf.EnableHostOverlayP)
}

func (conf SunhpcConf) GrubBoot() bool {
	return util.BoolP(conf.GrubBootP)
}

func (paths BuildConfig) NodesConf() string {
	return path.Join(paths.Sysconfdir, "sunhpc", "nodes.conf")
}

func (paths BuildConfig) AuthenticationConf() string {
	return path.Join(paths.Sysconfdir, "sunhpc", "auth.conf")
}

func (paths BuildConfig) OciBlobCachedir() string {
	return path.Join(paths.Cachedir, "sunhpc")
}

func (paths BuildConfig) SiteOverlaydir() string {
	return paths.WWOverlaydir
}

func (paths BuildConfig) DistributionOverlaydir() string {
	return path.Join(paths.Datadir, "sunhpc", "overlays")
}

func (paths BuildConfig) OverlayProvisiondir() string {
	return path.Join(paths.WWProvisiondir, "overlays")
}