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

package easygenapi

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

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

const progname = "easygen" // os.Args[0]

// The Options struct defines the structure to hold the commandline values
type Options struct { 
	HTML		bool	// treat the template file as html instead of text
	TemplateStr		string	// template string (in text)
	TemplateFile		string	// .tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)
	ExtYaml		string	// `extension` of yaml file
	ExtTmpl		string	// `extension` of template file
	StrFrom		string	// replace from, the from string used for the replace function
	StrTo		string	// replace to, the to string used for the replace function
	IntV		int	// int var
	Batch		time.Duration	// batch interval
	Debug		int	// debugging `level`
}

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

// Opts holds the actual values from the command line parameters
var Opts Options

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

func initVars() {

	// set default values for command line parameters
	flag.BoolVar(&Opts.HTML, "html", false,
		"treat the template file as html instead of text")
	flag.StringVar(&Opts.TemplateStr, "ts", "",
		"template string (in text)")
	flag.StringVar(&Opts.TemplateFile, "f", "",
		".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)")
	flag.StringVar(&Opts.TemplateFile, "tf", "",
		".tmpl (comma-separated) template file `name(s)` (default: same as .yaml file)")
	flag.StringVar(&Opts.ExtYaml, "ey", ".yaml",
		"`extension` of yaml file")
	flag.StringVar(&Opts.ExtTmpl, "et", ".tmpl",
		"`extension` of template file")
	flag.StringVar(&Opts.StrFrom, "rf", "",
		"replace from, the from string used for the replace function")
	flag.StringVar(&Opts.StrTo, "rt", "",
		"replace to, the to string used for the replace function")
	flag.IntVar(&Opts.IntV, "intv", 0,
		"int var")
	flag.DurationVar(&Opts.Batch, "batch", 120*time.Second,
		"batch interval")
	flag.IntVar(&Opts.Debug, "d", 0,
		"debugging `level`")
	flag.IntVar(&Opts.Debug, "debug", 0,
		"debugging `level`")
}

func initVals() {
	exists := false
	// Now override those default values from environment variables
	if _, exists = os.LookupEnv("EASYGEN_HTML"); Opts.HTML|| exists {
		Opts.HTML = true
	}
	if len(Opts.TemplateStr) == 0 &&
		len(os.Getenv("EASYGEN_TS")) != 0 {
		Opts.TemplateStr = os.Getenv("EASYGEN_TS")
	}
	if len(Opts.TemplateFile) == 0 &&
		len(os.Getenv("EASYGEN_F")) != 0 {
		Opts.TemplateFile = os.Getenv("EASYGEN_F")
	}
	if len(Opts.TemplateFile) == 0 &&
		len(os.Getenv("EASYGEN_TF")) != 0 {
		Opts.TemplateFile = os.Getenv("EASYGEN_TF")
	}
	if len(Opts.ExtYaml) == 0 &&
		len(os.Getenv("EASYGEN_EY")) != 0 {
		Opts.ExtYaml = os.Getenv("EASYGEN_EY")
	}
	if len(Opts.ExtTmpl) == 0 &&
		len(os.Getenv("EASYGEN_ET")) != 0 {
		Opts.ExtTmpl = os.Getenv("EASYGEN_ET")
	}
	if len(Opts.StrFrom) == 0 &&
		len(os.Getenv("EASYGEN_RF")) != 0 {
		Opts.StrFrom = os.Getenv("EASYGEN_RF")
	}
	if len(Opts.StrTo) == 0 &&
		len(os.Getenv("EASYGEN_RT")) != 0 {
		Opts.StrTo = os.Getenv("EASYGEN_RT")
	}
	if Opts.IntV == 0 &&
		len(os.Getenv("EASYGEN_INTV")) != 0 {
		if i, err := strconv.Atoi(os.Getenv("EASYGEN_INTV")); err == nil {
			Opts.IntV = i
		}
	}
	if Opts.Debug == 0 &&
		len(os.Getenv("EASYGEN_D")) != 0 {
		if i, err := strconv.Atoi(os.Getenv("EASYGEN_D")); err == nil {
			Opts.Debug = i
		}
	}
	if Opts.Debug == 0 &&
		len(os.Getenv("EASYGEN_DEBUG")) != 0 {
		if i, err := strconv.Atoi(os.Getenv("EASYGEN_DEBUG")); err == nil {
			Opts.Debug = i
		}
	}

}



// Usage function shows help on commandline usage
func Usage() {
	fmt.Fprintf(os.Stderr,
		"\nUsage:\n %s [flags] YamlFileName [YamlFileName...]\n\nFlags:\n\n",
		progname)
	flag.PrintDefaults()
	fmt.Fprintf(os.Stderr,
		"\nYamlFileName(s): The name for the .yaml data and .tmpl template file\n\tOnly the name part, without extension. Can include the path as well.\n")
	os.Exit(0)
}

