package main

import (
	"fmt"

	"git.openpdu.org/OpenPDU/openpdu/events"
	"git.openpdu.org/OpenPDU/openpdu/syslog"
	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

func init() {
	viper.SetConfigName("openpdu") // name of config file (without extension)
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".")             // optionally look for config in the working directory
	viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in
	err := viper.ReadInConfig()          // Find and read the config file
	if err != nil {                      // Handle errors reading the config file
		syslog.Err(fmt.Sprintf("Can't read config file: %s \n", err.Error()))
	}
	viper.WatchConfig()
	viper.OnConfigChange(func(e fsnotify.Event) {
		//The Viper configuration has changed to perform the responding operation
		fmt.Println("Config file changed:", e.Name)
		events.FireEvent("config_changed")
	})
}
