package main

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

var (
	configPath = flag.String("f", "./coinproxy.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 coinInit() {
	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)
	//	if db.initBoltDb() == false {
	//		log.Error("init bolt db error")
	//		os.Exit(0)
	//	}

	for _, cointype := range Gconfig.CoinType.Type {
		coin, err := template.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)
		}
	}

}
