package main

import (
	"embed"
	"flag"
	"io/fs"
	"log"
	"net/http"
	"os"
)

const (
	defaultDevMode   = true
	configPath       = "./config"
	usbKvmConfigPath = configPath + "/usbkvm.json"
)

var (
	developent = flag.Bool("dev", defaultDevMode, "Enable development mode with local static files")
	mode       = flag.String("mode", "usbkvm", "Mode of operation: usbkvm, ipkvm or debug")
	tool       = flag.String("tool", "", "Run debug tool, must be used with -mode=debug")
)

/* Web Server Static Files */
//go:embed www
var embeddedFiles embed.FS
var webfs http.FileSystem

func init() {
	// Initiate the web server static files
	if *developent {
		webfs = http.Dir("./www")
	} else {
		// Embed the ./www folder and trim the prefix
		subFS, err := fs.Sub(embeddedFiles, "www")
		if err != nil {
			log.Fatal(err)
		}
		webfs = http.FS(subFS)
	}

	// Initiate the config folder if not exists
	err := os.MkdirAll("./config", 0755)
	if err != nil {
		log.Fatal("Failed to create config folder:", err)
	}

}

func main() {
	flag.Parse()

	switch *mode {
	case "cfgchip":
		err := SetupHIDCommunication()
		if err != nil {
			log.Fatal(err)
		}
	case "debug":
		err := handle_debug_tool()
		if err != nil {
			log.Fatal(err)
		}
	case "ipkvm":
		//Check runtime dependencies
		err := run_dependency_precheck()
		if err != nil {
			log.Fatal(err)
		}

		//Start IP-KVM mode
		err = init_ipkvm_mode()
		if err != nil {
			log.Fatal(err)
		}
	case "usbkvm":
		//Check runtime dependencies
		err := run_dependency_precheck()
		if err != nil {
			log.Fatal(err)
		}

		//Load config file or create default one
		kvmCfg, err := loadUsbKvmConfig()
		if err != nil {
			log.Fatal("Failed to load or create USB KVM config:", err)
		}

		//Start USB KVM mode
		err = startUsbKvmMode(kvmCfg)
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatalf("Unknown mode: %s. Supported modes are: usbkvm, capture", *mode)
	}
}
