package bootstrap

import (
	"flag"
	"fmt"
	"git.loafle.net/overflow/overflow_probe/discovery/conf"
	log "github.com/cihub/seelog"
	"os"
	"runtime"
	"git.loafle.net/overflow/overflow_probe/discovery"
)

func init() {
	paramA := flag.String("conf", "/overflow/collector", "Config file path")
	//paramB := flag.Int("b", 1, "int type param")
	//paramC := flag.Bool("c", false, "bool type param")
	flag.Parse()
	//fmt.Println("Args: ", *paramA, *paramB, *paramC)
	fmt.Println("Args: ", *paramA)
	//if len(os.Args) < /* paramcount */ {
	//  flag.Usage()
	//  os.Exit(1)
	//}

	if err := conf.LoadConfig(*paramA); err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}
}

func Start()  {
	runtime.GOMAXPROCS(runtime.NumCPU())
	defer log.Flush()
	stop := make(chan bool, 1)
	go handleShell(stop)
	handleSignal(stop)
	collector.Start()

	if <-stop {
		// Comes from shell cmd 'stop' or quit signals
		stopHandleShell()
		collector.Stop()
		close(stop)
		log.Flush()
		os.Exit(0)
	}
}

//func main() {
//
//	runtime.GOMAXPROCS(runtime.NumCPU())
//
//	defer log.Flush()
//
//	stop := make(chan bool, 1)
//	go handleShell(stop)
//	handleSignal(stop)
//
//	collector.Start()
//
//	if <-stop {
//		// Comes from shell cmd 'stop' or quit signals
//		stopHandleShell()
//		collector.Stop()
//		close(stop)
//		log.Flush()
//		os.Exit(0)
//	}
//}
