package main

import (
	// Standard
	"embed"
	"flag"
	"fmt"
	"os"
	"path"
)

const VERSION = "v1"

var (
	flagConfigFile string
	flagVersion    bool
	flagDev        bool
	config         Config

	//go:embed sql
	sqlFS embed.FS

	//go:embed views
	viewFS embed.FS

	//go:embed static
	staticFS embed.FS
)

func initCmdline() {
	confDir, err := os.UserConfigDir()
	if err != nil {
		logger.Error("application", "error", err)
		os.Exit(1)
	}
	configFilename := path.Join(confDir, "datagraph.yaml")

	flag.StringVar(&flagConfigFile, "config", configFilename, "Filename of configuration file")
	flag.BoolVar(&flagVersion, "version", false, "Display version and exit")
	flag.BoolVar(&flagDev, "dev", false, "Reload HTML templates from disk for every request")
	flag.Parse()
}
func main() {
	initLog()
	initCmdline()

	if flagVersion {
		fmt.Println(VERSION)
		os.Exit(0)
	}

	logger.Info("application", "version", VERSION)
	readConfig()

	err := initDB()
	if err != nil {
		logger.Error("database", "error", err)
		os.Exit(1)
	}

	err = initWebserver()
	if err != nil {
		logger.Error("webserver", "error", err)
		os.Exit(1)
	}
}
