package guestnet

import (
	_ "embed"
	"strings"
)

const (
	GuestScriptPath     = "/usr/local/libexec/banger-network-bootstrap"
	ConfigPath          = "/etc/banger-network.conf"
	SystemdServiceName  = "banger-network.service"
	VoidCoreServicePath = "/etc/runit/core-services/20-banger-network.sh"
	DefaultInterface    = "eth0"
	DefaultNetmask      = "255.255.255.0"
)

var (
	//go:embed assets/bootstrap.sh
	bootstrapScript string
	//go:embed assets/systemd.service
	systemdService string
	//go:embed assets/void-core-service.sh
	voidCoreService string
)

func BootstrapScript() string {
	return bootstrapScript
}

func ConfigFile(guestIP, gatewayIP, dns string) []byte {
	lines := []string{
		"GUEST_IP=" + shellQuote(guestIP),
		"GATEWAY_IP=" + shellQuote(gatewayIP),
		"NETMASK=" + shellQuote(DefaultNetmask),
		"INTERFACE=" + shellQuote(DefaultInterface),
		"DNS1=" + shellQuote(dns),
		"DNS2=''",
	}
	return []byte(strings.Join(lines, "\n") + "\n")
}

func SystemdServiceUnit() string {
	return systemdService
}

func VoidCoreService() string {
	return voidCoreService
}

func shellQuote(value string) string {
	return "'" + strings.ReplaceAll(value, "'", `'"'"'`) + "'"
}
