package config

import (
	"flag"
	"fmt"

	auth "git.paulbsd.com/paulbsd/adradius/src/go-ad-auth"
	"git.paulbsd.com/paulbsd/adradius/utils"
	"gopkg.in/ini.v1"
)

// GetConfig fetch config from ini file
func (c *Config) GetConfig() (err error) {
	flag.Usage = utils.Usage

	flag.StringVar(&c.ConfigPath, "configfile", "adradius.ini", "config file to use with adradius section")
	flag.Parse()

	config, err := ini.Load(c.ConfigPath)
	if err != nil {
		return
	}

	adradiusSection := config.Section("adradius")
	c.Servers = adradiusSection.Key("servers").Strings(",")
	if len(c.Servers) < 1 {
		return fmt.Errorf("No servers provided in config")
	}
	c.Port = adradiusSection.Key("port").MustInt(389)
	c.BaseDN = adradiusSection.Key("basedn").MustString("dc=example,dc=com")
	c.TLS = adradiusSection.Key("tls").MustBool()
	c.Listen = adradiusSection.Key("listen").MustString(":1812")
	c.Secret = adradiusSection.Key("secret").MustString("secret")
	c.SkipVerify = adradiusSection.Key("skipverify").MustBool()

	return nil
}

// Config is the main configuration
type Config struct {
	ConfigPath string
	Servers    []string
	Port       int
	BaseDN     string
	TLS        bool
	Listen     string
	Secret     string
	Security   auth.SecurityType
	SkipVerify bool
}
