package main

import (
	"flag"
	"fmt"
	"os"
)

// Flags holds the command-line arguments. One-shot output (CSV / InfluxDB line
// protocol to stdout) remains the default; -server switches to the long-lived
// service that exposes the Prometheus endpoint and pushes to Influx.
type Flags struct {
	ConfigPath string

	// One-shot output controls (default mode).
	Format string

	// Service mode.
	Server bool

	// HTTP output overrides (service mode).
	HTTPBind        string
	HTTPPort        uint
	HTTPMetricsPath string
}

// ParseFlags parses the command line into app.flags, printing the version and
// exiting when -version is supplied.
func (a *App) ParseFlags() {
	a.flags = new(Flags)
	flag.Usage = func() {
		fmt.Printf("%s: %s.\n\nUsage:\n", serviceName, serviceDescription)
		flag.PrintDefaults()
	}

	// Version.
	var printVer bool
	flag.BoolVar(&printVer, "version", false, "print version and exit")
	flag.BoolVar(&printVer, "v", false, "print version and exit (shorthand)")

	// Configuration path override.
	usage := "load configuration from `FILE`"
	flag.StringVar(&a.flags.ConfigPath, "config", "", usage)
	flag.StringVar(&a.flags.ConfigPath, "c", "", usage+" (shorthand)")

	// One-shot output controls.
	flag.StringVar(&a.flags.Format, "format", "csv", "output format: csv | influx")

	// Service mode.
	flag.BoolVar(&a.flags.Server, "server", false, "run as a service: Prometheus HTTP endpoint and scheduled InfluxDB output")

	// HTTP output overrides (service mode).
	flag.StringVar(&a.flags.HTTPBind, "http-bind", "", "bind address for the HTTP server")
	flag.UintVar(&a.flags.HTTPPort, "http-port", 0, "bind port for the HTTP server")
	flag.StringVar(&a.flags.HTTPMetricsPath, "http-metrics-path", "", "path for the Prometheus metrics endpoint")

	flag.Parse()

	if printVer {
		printVersion()
		os.Exit(0)
	}
}
