// Package config has definitions about the overall configuration of Collector.
package config

import (
	"fmt"
	"os"
	"time"

	flag "github.com/spf13/pflag"
)

const (
	// Time to wait before retrying a failed operation.
	RETRYDURATION = time.Duration(5) * time.Second
	DockerHub     = "registry-1.docker.io"
)

var (
	FilterRepos = false

	// Directories/files dependent on Environment variables
	BANYANHOSTDIR = func() string {
		if os.Getenv("BANYAN_HOST_DIR") == "" {
			os.Setenv("BANYAN_HOST_DIR", os.Getenv("HOME")+"/.banyan")
		}
		return os.Getenv("BANYAN_HOST_DIR")
	}
	BANYANDIR = func() string {
		if os.Getenv("BANYAN_DIR") != "" {
			return os.Getenv("BANYAN_DIR")
		}
		return BANYANHOSTDIR()
	}
	COLLECTORDIR = func() string {
		if os.Getenv("COLLECTOR_DIR") == "" {
			fmt.Fprintf(os.Stderr, "Please set the environment variable COLLECTOR_DIR to the parent")
			fmt.Fprintf(os.Stderr, " of the \"data\" scripts directory...\n\n")
			//printExampleUsage()
			//fmt.Fprintf(os.Stderr, "  e.g.,\tcd COLLECTOR_SOURCE_DIRECTORY\n")
			//fmt.Fprintf(os.Stderr, "\tsudo COLLECTOR_DIR=$PWD collector [options] REGISTRY [REPO1 REPO2 ...]\n\n")
			return ""
		}
		return os.Getenv("COLLECTOR_DIR")
	}
	BanyanOutDir = flag.String("banyanoutdir", BANYANDIR()+"/hostcollector/banyanout",
		"Output directory for collected data")
	// Dests is setup as a flag when main calls DefineDestsFlag().
	Dests *string

	// BanyanUpdate is a function to log interesting updates as collector execution proceeds.
	BanyanUpdate BanyanUpdateFunc = func(status ...string) {}
)

func init() {
	toHide := flag.Lookup("banyanoutdir")
	if toHide != nil {
		toHide.Hidden = true
	}
}

type BanyanUpdateFunc func(status ...string)

// DefineDestsFlag is called by the importing package, e.g., main, to create the dests flag.
func DefineDestsFlag(def string) {
	Dests = flag.StringP("dests", "d", def,
		"One or more ',' separated destinations for output generated by scripts. e.g., file or file,custom")
}
