package main

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

	"vrc_osc_go/internal/app"
)

func main() {
	var configPath string
	var mode string
	var noGUI bool
	flag.StringVar(&configPath, "config", "config/config.toml", "path to config.toml")
	flag.StringVar(&mode, "mode", "", "generate-json or extract-log")
	flag.BoolVar(&noGUI, "no-gui", false, "do not launch the GUI companion")
	flag.Parse()

	if mode == "" && !noGUI {
		launchGUI()
	}

	if err := app.Run(configPath, mode); err != nil {
		log.SetOutput(os.Stderr)
		log.Fatalf("vrc_osc_go: %v", err)
	}
}

func launchGUI() {
	exe, err := os.Executable()
	if err != nil {
		return
	}
	gui := filepath.Join(filepath.Dir(exe), "vrc_osc_gui.exe")
	if _, err := os.Stat(gui); err != nil {
		return
	}
	_ = exec.Command(gui).Start()
}
