package main

import (
	"context"
	"flag"
	"log"
	"os/signal"
	"syscall"

	"gitea.hostxtra.co.uk/mrhid6/vantage/agent/internal/config"
	agentsync "gitea.hostxtra.co.uk/mrhid6/vantage/agent/internal/sync"
)

var Version = "dev"

func main() {
	genKey := flag.String("generate-key", "", "Generate SSH keypair and upload with this label")
	flag.Parse()

	cfg, err := config.Load()
	if err != nil {
		log.Fatalf("failed to load config: %v", err)
	}

	if *genKey != "" {
		if err := agentsync.GenerateAndUpload(cfg, *genKey); err != nil {
			log.Fatalf("key generation failed: %v", err)
		}
		return
	}

	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
	defer stop()

	log.Printf("vantage-agent %s starting (server=%s, poll=%s)", Version, cfg.ServerURL, cfg.PollInterval)
	if err := agentsync.Run(ctx, cfg, Version); err != nil {
		log.Fatalf("agent error: %v", err)
	}
}
