package main

import (
	"log"

	"net/http"
	_ "net/http/pprof"

	"git.paulbsd.com/paulbsd/adradius/src/adradius"
	"git.paulbsd.com/paulbsd/adradius/src/config"
	"github.com/sevlyar/go-daemon"
)

func main() {
	var cfg config.Config
	var err error

	cntxt := &daemon.Context{
		PidFileName: "/var/run/adradius.pid",
		PidFilePerm: 0644,
		LogFileName: "/var/log/adradius.log",
		LogFilePerm: 0640,
		Umask:       027,
	}

	d, err := cntxt.Reborn()
	if err != nil {
		log.Fatal("Unable to run: ", err)
	}
	if d != nil {
		return
	}
	defer cntxt.Release()

	log.Print("- - - - - - - - - - - - - - -")
	log.Print("daemon started")

	err = cfg.GetConfig()
	if err != nil {
		log.Fatal(err)
	}

	ldapcfg, err := adradius.SetADRadiusConfig(&cfg)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		log.Println(http.ListenAndServe("localhost:6060", nil))
	}()

	adradius.RunServer(&cfg, ldapcfg)
	if err != nil {
		log.Fatal(err)
	}

}
