package config

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

        "k8s.io/client-go/rest"
        "k8s.io/client-go/tools/clientcmd"
)

const (
	DefaultUpdateInterval = 30
	DefaultProfilePath = "/etc/linuxptp"
)

func GetKubeConfig() (*rest.Config, error) {
        configFromFlags := func(kubeConfig string) (*rest.Config, error) {
                if _, err := os.Stat(kubeConfig); err != nil {
                        return nil, fmt.Errorf("Cannot stat kubeconfig '%s'", kubeConfig)
                }
                return clientcmd.BuildConfigFromFlags("", kubeConfig)
        }

        // If an env variable is specified with the config location, use that
        kubeConfig := os.Getenv("KUBECONFIG")
        if len(kubeConfig) > 0 {
                return configFromFlags(kubeConfig)
        }
        // If no explicit location, try the in-cluster config
        if c, err := rest.InClusterConfig(); err == nil {
                return c, nil
        }
        // If no in-cluster config, try the default location in the user's home directory
        if usr, err := user.Current(); err == nil {
                kubeConfig := filepath.Join(usr.HomeDir, ".kube", "config")
                return configFromFlags(kubeConfig)
        }

        return nil, fmt.Errorf("Could not locate a kubeconfig")
}
