package main

import (
	"flag"
	"log"
	"os"
	"path/filepath"
)

func ParseFlags() (string, error) {
	// String that contains the configured configuration path
	var configPath string

	exe, _ := os.Executable()
	defaultCfgPath := filepath.Dir(exe)
	defaultCfgPath = filepath.Join(defaultCfgPath, "/config.yml")
	// Set up a CLI flag called "-config" to allow users
	// to supply the configuration file
	flag.StringVar(&configPath, "config", defaultCfgPath, "path to config file")

	// Actually parse the flags
	flag.Parse()

	// Validate the path first
	if err := ValidateConfigPath(configPath); err != nil {
		return "", err
	}

	// Return the configuration path
	return configPath, nil
}

func main() {

	// Generate our config based on the config supplied
	// by the user in the flags
	cfgPath, err := ParseFlags()
	if err != nil {
		log.Fatal(err)
	}

	cfg := NewConfig(cfgPath)

	log := NewLog()
	log.Info("Running FlexDXCluster version 0.3")
	log.Infof("Callsign: %s", cfg.SQLite.Callsign)

	DeleteDatabase("./flex.sqlite", log)

	log.Infof("Gotify Push Enabled: %v", cfg.Gotify.Enable)
	if cfg.Gotify.Enable {
		log.Infof("Gotify Push MewDXCC %v - NewBand %v - NewMode %v - NewBandAndMode %v", cfg.Gotify.NewDXCC, cfg.Gotify.NewBand, cfg.Gotify.NewMode, cfg.Gotify.NewBandAndMode)
	}

	// Load country.xml to get all the DXCC number
	Countries := LoadCountryFile()

	// Database to keep track of all spots
	fRepo := NewFlexDXDatabase("flex.sqlite")
	defer fRepo.db.Close()

	// Database connection to Log4OM
	cRepo := NewLog4OMContactsRepository(cfg.SQLite.SQLitePath)
	defer cRepo.db.Close()

	TCPServer := NewTCPServer(cfg.TelnetServer.Host, cfg.TelnetServer.Port)
	TCPClient := NewTCPClient(TCPServer, Countries)
	FlexClient := NewFlexClient(*fRepo, TCPServer, TCPClient.SpotChanToFlex)
	// HTTPServer := NewHTTPServer(*cRepo, *fRepo, TCPServer, TCPClient.SpotChanToHTTPServer)

	go FlexClient.StartFlexClient()
	go TCPClient.StartClient()
	go TCPServer.StartServer()

	CheckSignal(TCPClient, TCPServer, FlexClient, fRepo, cRepo)

}