package main

import (
	"crypto/hmac"
	"crypto/sha512"
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"path"
	"text/template"
)

const pxeUserTplPath = "pxelinux.cfg/tpl"
const ipxeUserTplPath = "pxelinux.cfg/tpl.ipxe"
const pxeUserPath = "pxelinux.cfg"

func RegisterUserMAC(ent ARPEntry, ip net.IP, username string) error {
	if err := registerUser(ipxeUserTplPath, fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x.ipxe", ent.HWAddress[0], ent.HWAddress[1], ent.HWAddress[2], ent.HWAddress[3], ent.HWAddress[4], ent.HWAddress[5]), username, ip); err != nil {
		return err
	} else {
		return registerUser(pxeUserTplPath, fmt.Sprintf("%02x-%02x-%02x-%02x-%02x-%02x-%02x", ent.HWType, ent.HWAddress[0], ent.HWAddress[1], ent.HWAddress[2], ent.HWAddress[3], ent.HWAddress[4], ent.HWAddress[5]), username, ip)
	}
}

func RegisterUserIP(ip net.IP, username string) error {
	return registerUser(pxeUserTplPath, fmt.Sprintf("%02X%02X%02X%02X", ip.To4()[0], ip.To4()[1], ip.To4()[2], ip.To4()[3]), username, ip)
}

func registerUser(tplPath string, filename string, username string, ip net.IP) error {
	if pxeTplCnt, err := ioutil.ReadFile(path.Join(tftpDir, tplPath)); err != nil {
		return err
	} else if userfd, err := os.OpenFile(path.Join(tftpDir, pxeUserPath, filename), os.O_RDWR|os.O_CREATE, 0644); err != nil {
		return err
	} else {
		defer userfd.Close()

		pkey := hmac.New(sha512.New512_224, []byte(loginSalt))

		if pxeTmpl, err := template.New("pxeUser").Parse(string(pxeTplCnt)); err != nil {
			return err
		} else if err := pxeTmpl.Execute(userfd, map[string]string{
			"username": username,
			"pkey":     fmt.Sprintf("%x", pkey.Sum([]byte(username))),
			"ip":       fmt.Sprintf("%d.%d.%d.%d", ip.To4()[0], ip.To4()[1], ip.To4()[2], ip.To4()[3]),
		}); err != nil {
			return err
		}
	}
	return nil
}
