package main

import (
	"commonapi"
	"flag"
	"fmt"
	Log "mlog"
	"os"
	"runtime"
	//"github.com/bolt-master"
	tml "github.com/toml-master"
)

var (
	configPath = flag.String("f", "./transcollect.toml", "configfile")
	Gconfig    config
	log        *Log.Logger
	CPUNUM            = runtime.NumCPU()
	BucketSC   string = "SC"
)

type config struct {
	Title      string
	Log        loginfo      `toml:"log"`
	DB         databaseinfo `toml:"db"`
	HttpServer httpServer   `toml:"httpserver"`
	CoinType   cointype
}

type loginfo struct {
	Loglevel string
	Logpath  string
}

type httpServer struct {
	HttpBindAddr string
}

type databaseinfo struct {
	DbPath string
}

type cointype struct {
	Type []string
}

func sysInit() {
	runtime.GOMAXPROCS(CPUNUM)
	flag.PrintDefaults()
	flag.Parse()

	if _, err := tml.DecodeFile(*configPath, &Gconfig); err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	fmt.Printf("%+v\n", Gconfig)
	var err error
	log, err = Log.New(Gconfig.Log.Logpath, Gconfig.Log.Loglevel)
	if err != nil {
		log.Error(err.Error())
		os.Exit(0)
	}

	initChanHandler(CPUNUM)

	for _, cointype := range Gconfig.CoinType.Type {
		coin, err := commonapi.New(cointype)
		if err != nil {
			log.Error(err.Error())
			os.Exit(0)
		}

		if coin.InitDriver(nil) == false {
			log.Error("InitDriver error.driver name:%s", cointype)
			os.Exit(0)
		}
	}

}
