package config

import (
	"os"
	"os/user"
	"path/filepath"

	log "git.wntrmute.dev/kyle/goutils/log"
)

func FindConfigPath() string {
	paths := []string{"/home/kyle/code/kdhcp/kdhcpd.yaml"}
	user, err := user.Current()
	if err == nil {
		if homedir := user.HomeDir; homedir != "" {
			paths = append(paths, filepath.Join(homedir, "code", "kdhcp", "kdhcpd.yaml"))
			paths = append(paths, filepath.Join(homedir, ".config", "kdhcp", "kdhcpd.yaml"))
		}
	}

	paths = append(paths, "/etc/kdhcp/kdhcpd.yaml")

	for _, path := range paths {
		log.Debugf("config: looking for config file at %s", path)
		_, err = os.Stat(path)
		if os.IsNotExist(err) {
			continue
		}

		if err != nil {
			log.Debugf("config: while trying to stat config file at '%s': %s", path, err)
			continue
		}

		return path
	}

	return ""
}