package config

import (
	"os"

	"github.com/scalingdata/gcfg"
)

// Load loads the configuration by the given filename, throwing an error if it fails.
func Load(fname string) (c *Config, err error) {
	fin, err := os.Open(fname)
	if err != nil {
		return nil, err
	}
	defer fin.Close()

	c = new(Config)
	c.Network = make(map[string]*Network)
	c.User = make(map[string]*User)

	err = gcfg.ReadInto(c, fin)
	if err != nil {
		return nil, err
	}

	for k, user := range c.User {
		user.Username = k
	}

	return c, nil
}
