package utils

import (
	"os"
	"path/filepath"
	"runtime"
)

//HomeDir return user home directory
func HomeDir() string {
	if runtime.GOOS == "windows" {
		return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
	}
	if h := os.Getenv("HOME"); h != "" {
		return h
	}
	return "/"
}

//HiddenFilePrefix get hidden file prefix
func HiddenFilePrefix() string {
	switch runtime.GOOS {
	case "windows":
		return "~"
	default:
		return "."
	}
}

//CacheDir return user cache directory
func CacheDir() string {
	switch runtime.GOOS {
	case "darwin":
		return filepath.Join(HomeDir(), "Library", "Caches")
	case "windows":
		for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
			if v := os.Getenv(ev); v != "" {
				return v
			}
		}
		// Worst case:
		return HomeDir()
	}
	if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
		return xdg
	}
	return filepath.Join(HomeDir(), ".cache")
}
