// !!! !!!
// WARNING: Code automatically generated. Editing discouraged.
// !!! !!!

package {{.PackageName}}

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

////////////////////////////////////////////////////////////////////////////
// Constant and data type/structure definitions

const progname = "{{.ProgramName}}" // os.Args[0]

// The {{.StructName}} struct defines the structure to hold the commandline values
type {{.StructName}} struct { {{range .Options}}{{if ne .Name "SEPARATOR" }}
	{{.Name}}		{{ if eq .Type "duration" }}time.Duration{{else}}{{.Type}}{{end}}	// {{.Usage}}{{end}}{{end}}
}

////////////////////////////////////////////////////////////////////////////
// Global variables definitions

// {{.StructVar}} holds the actual values from the command line parameters
var {{.StructVar}} {{.StructName}}

////////////////////////////////////////////////////////////////////////////
// Commandline definitions

func initVars() {

	// set default values for command line parameters{{range .Options}}{{if eq .Name "SEPARATOR" }}

{{else}}{{if (stringsContains .Flag ",")}}{{$cf := .}}{{range (stringsSplit .Flag ",")}}
	flag.{{clk2uc $cf.Type}}Var(&{{$.StructVar}}.{{$cf.Name}}, "{{.}}", {{$cf.Value}},
		"{{$cf.Usage}}"){{end}}{{else}}
	flag.{{clk2uc .Type}}Var(&{{$.StructVar}}.{{.Name}}, "{{.Flag}}", {{.Value}},
		"{{.Usage}}"){{end}}{{end}}{{end}}
}

func initVals() {
	exists := false
	// Now override those default values from environment variables{{range .Options}}{{if eq .Name "SEPARATOR" }}

{{else}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .Flag)}}{{if eq .Type "string" }}{{if (stringsContains .Flag ",")}}{{$cf := .}}{{range (stringsSplit .Flag ",")}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .)}}
	if len({{$.StructVar}}.{{$cf.Name}}) == 0 &&
		len(os.Getenv("{{$envVar}}")) != 0 {
		{{$.StructVar}}.{{$cf.Name}} = os.Getenv("{{$envVar}}")
	}{{end}}{{else}}
	if len({{$.StructVar}}.{{.Name}}) == 0 &&
		len(os.Getenv("{{$envVar}}")) != 0 {
		{{$.StructVar}}.{{.Name}} = os.Getenv("{{$envVar}}")
	}{{end}}{{else}}{{if eq .Type "bool" }}
	if _, exists = os.LookupEnv("{{$envVar}}"); {{$.StructVar}}.{{.Name}}|| exists {
		{{$.StructVar}}.{{.Name}} = true
	}{{else}}{{if eq .Type "int" }}{{if (stringsContains .Flag ",")}}{{$cf := .}}{{range (stringsSplit .Flag ",")}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .)}}
	if {{$.StructVar}}.{{$cf.Name}} == 0 &&
		len(os.Getenv("{{$envVar}}")) != 0 {
		if i, err := strconv.Atoi(os.Getenv("{{$envVar}}")); err == nil {
			{{$.StructVar}}.{{$cf.Name}} = i
		}
	}{{end}}{{else}}
	if {{$.StructVar}}.{{.Name}} == 0 &&
		len(os.Getenv("{{$envVar}}")) != 0 {
		if i, err := strconv.Atoi(os.Getenv("{{$envVar}}")); err == nil {
			{{$.StructVar}}.{{.Name}} = i
		}
	}{{end}}{{end}}{{end}}{{end}}{{end}}{{end}}

}

{{if eqf .UsageSummary "True" }}
const usageSummary = "{{range .Options}}{{if eq .Name "SEPARATOR" }}\n{{else}}{{$envVar := printf "%s_%s" (clk2ss $.ProgramName) (clk2ss .Flag)}}  -{{.Flag}}\t{{.Usage}} ({{$envVar}})\n{{end}}{{end}}\nDetails:\n\n"
{{end}}

// Usage function shows help on commandline usage
func Usage() {
	fmt.Fprintf(os.Stderr,
		"{{.UsageLead}}",
		progname){{if eq .UsageSummary "TRUE" }}
	fmt.Fprintf(os.Stderr, usageSummary){{end}}
	flag.PrintDefaults()
	fmt.Fprintf(os.Stderr,
		"{{.UsageEnd}}")
	os.Exit(0)
}

