package dns

import (
	
	
	
	
	
)

// ClientConfig wraps the contents of the /etc/resolv.conf file.
type ClientConfig struct {
	Servers  []string // servers to use
	Search   []string // suffixes to append to local name
	Port     string   // what port to use
	Ndots    int      // number of dots in name to trigger absolute lookup
	Timeout  int      // seconds before giving up on packet
	Attempts int      // lost packets before giving up on server, not used in the package dns
}

// ClientConfigFromFile parses a resolv.conf(5) like file and returns
// a *ClientConfig.
func ( string) (*ClientConfig, error) {
	,  := os.Open()
	if  != nil {
		return nil, 
	}
	defer .Close()
	return ClientConfigFromReader()
}

// ClientConfigFromReader works like ClientConfigFromFile but takes an io.Reader as argument
func ( io.Reader) (*ClientConfig, error) {
	 := new(ClientConfig)
	 := bufio.NewScanner()
	.Servers = make([]string, 0)
	.Search = make([]string, 0)
	.Port = "53"
	.Ndots = 1
	.Timeout = 5
	.Attempts = 2

	for .Scan() {
		if  := .Err();  != nil {
			return nil, 
		}
		 := .Text()
		 := strings.Fields()
		if len() < 1 {
			continue
		}
		switch [0] {
		case "nameserver": // add one name server
			if len() > 1 {
				// One more check: make sure server name is
				// just an IP address.  Otherwise we need DNS
				// to look it up.
				 := [1]
				.Servers = append(.Servers, )
			}

		case "domain": // set search path to just this domain
			if len() > 1 {
				.Search = make([]string, 1)
				.Search[0] = [1]
			} else {
				.Search = make([]string, 0)
			}

		case "search": // set search path to given servers
			.Search = cloneSlice([1:])

		case "options": // magic options
			for ,  := range [1:] {
				switch {
				case len() >= 6 && [:6] == "ndots:":
					,  := strconv.Atoi([6:])
					if  < 0 {
						 = 0
					} else if  > 15 {
						 = 15
					}
					.Ndots = 
				case len() >= 8 && [:8] == "timeout:":
					,  := strconv.Atoi([8:])
					if  < 1 {
						 = 1
					}
					.Timeout = 
				case len() >= 9 && [:9] == "attempts:":
					,  := strconv.Atoi([9:])
					if  < 1 {
						 = 1
					}
					.Attempts = 
				case  == "rotate":
					/* not imp */
				}
			}
		}
	}
	return , nil
}

// NameList returns all of the names that should be queried based on the
// config. It is based off of go's net/dns name building, but it does not
// check the length of the resulting names.
func ( *ClientConfig) ( string) []string {
	// if this domain is already fully qualified, no append needed.
	if IsFqdn() {
		return []string{}
	}

	// Check to see if the name has more labels than Ndots. Do this before making
	// the domain fully qualified.
	 := CountLabel() > .Ndots
	// Make the domain fully qualified.
	 = Fqdn()

	// Make a list of names based off search.
	 := []string{}

	// If name has enough dots, try that first.
	if  {
		 = append(, )
	}
	for ,  := range .Search {
		 = append(, Fqdn(+))
	}
	// If we didn't have enough dots, try after suffixes.
	if ! {
		 = append(, )
	}
	return 
}