package terminfo

import (
	
	
	
	
	
)

// termCache is the terminfo cache.
var termCache = struct {
	db map[string]*Terminfo
	sync.RWMutex
}{
	db: make(map[string]*Terminfo),
}

// Load follows the behavior described in terminfo(5) to find correct the
// terminfo file using the name, reads the file and then returns a Terminfo
// struct that describes the file.
func ( string) (*Terminfo, error) {
	if  == "" {
		return nil, ErrEmptyTermName
	}
	termCache.RLock()
	,  := termCache.db[]
	termCache.RUnlock()
	if  {
		return , nil
	}
	var  []string
	// check $TERMINFO
	if  := os.Getenv("TERMINFO");  != "" {
		 = append(, )
	}
	// check $HOME/.terminfo
	,  := user.Current()
	if  != nil {
		return nil, 
	}
	 = append(, path.Join(.HomeDir, ".terminfo"))
	// check $TERMINFO_DIRS
	if  := os.Getenv("TERMINFO_DIRS");  != "" {
		 = append(, strings.Split(, ":")...)
	}
	// check fallback directories
	 = append(, "/etc/terminfo", "/lib/terminfo", "/usr/share/terminfo")
	for ,  := range  {
		,  = Open(, )
		if  != nil &&  != ErrFileNotFound && !os.IsNotExist() {
			return nil, 
		} else if  == nil {
			return , nil
		}
	}
	return nil, ErrDatabaseDirectoryNotFound
}

// LoadFromEnv loads the terminal info based on the name contained in
// environment variable TERM.
func () (*Terminfo, error) {
	return Load(os.Getenv("TERM"))
}