package config

import (
	"fmt"
	"os"
	"path"
	"path/filepath"

	"log"
	"os/user"

	occn "git.loafle.net/overflow/commons-go/config/noauthprobe"
	occp "git.loafle.net/overflow/commons-go/config/probe"
)

var (
	rootDir string
)

func init() {
	exePath, err := os.Executable()
	if err != nil {
		panic(err)
	}

	binDir := filepath.Dir(exePath)

	if "debug" == filepath.Base(exePath) {
		rootDir = path.Join(binDir, "_build")
	} else if "/tmp" == binDir {
		usr, err := user.Current()
		if err != nil {
			log.Fatal(err)
		}
		rootDir = path.Join(usr.HomeDir, "/go/src/git.loafle.net/overflow/probe/_build")
	} else {
		rootDir = filepath.Clean(fmt.Sprintf("%s/..", binDir))
	}
}

func RootDir() string {
	return rootDir
}

func BinDir() string {
	return path.Join(RootDir(), occp.PathBin)
}

func ConfigDir() string {
	return path.Join(RootDir(), occp.PathConfig)
}

func JREDir() string {
	return path.Join(RootDir(), occp.PathJRE)
}

func JavaBinPath() string {
	return path.Join(JREDir(), "bin", "java")
}

func LogsDir() string {
	return path.Join(RootDir(), occp.PathLogs)
}

func ProbeConfigFilePath() string {
	return path.Join(BinDir(), occp.ConfigFileName)
}

func ProbeLoggingConfigFilePath() string {
	return path.Join(BinDir(), occp.LoggingConfigFileName)
}

func NoAuthProbeConfigFilePath() string {
	return path.Join(BinDir(), occn.ConfigFileName)
}
