package comfunc

import (
	
	
	
	
)

// Workdir get
func () string {
	,  := os.Getwd()
	return 
}

// ExpandHome will parse first `~` as user home dir path.
func ( string) string {
	if len() == 0 {
		return 
	}

	if [0] != '~' {
		return 
	}

	if len() > 1 && [1] != '/' && [1] != '\\' {
		return 
	}

	,  := os.UserHomeDir()
	if  != nil {
		return 
	}
	return  + [1:]
}

// ExecCmd an command and return output.
//
// Usage:
//
//	ExecCmd("ls", []string{"-al"})
func ( string,  []string,  ...string) (string, error) {
	// create a new Cmd instance
	 := exec.Command(, ...)
	if len() > 0 {
		.Dir = [0]
	}

	,  := .Output()
	return string(), 
}

// ShellExec exec command by shell
// cmdLine e.g. "ls -al"
func ( string,  ...string) (string, error) {
	// shell := "/bin/sh"
	 := "sh"
	if len() > 0 {
		 = [0]
	}

	 := exec.Command(, "-c", )
	,  := .Output()
	return string(), 
}

// curShellCache value
var curShellCache string

// CurrentShell get current used shell env file.
//
// return like: "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "bash"
func ( bool,  ...string) ( string) {
	var  error

	 := ""
	if len() > 0 {
		 = [0]
	}

	if curShellCache == "" {
		// 检查父进程名称
		 := os.Getenv("GOPROCESS")
		if  != "" {
			 = 
		} else {
			 = os.Getenv("SHELL") // 适用于 Unix-like 系统
			if len() == 0 {
				// TODO check on Windows git bash
				,  = ShellExec("echo $SHELL")
				if  != nil {
					 = 
				}
			}
			 = strings.TrimSpace()
		}

		// fix: 去除 .exe 后缀
		if  := strings.IndexByte(, '.');  > 0 {
			 = [:]
		}

		// cache result
		curShellCache = 
	} else {
		 = curShellCache
	}

	if  && len() > 0 {
		 = filepath.Base()
	} else if len() == 0 {
		 = 
	}
	return
}

// HasShellEnv has shell env check.
//
// Usage:
//
//	HasShellEnv("sh")
//	HasShellEnv("bash")
func ( string) bool {
	// can also use: "echo $0"
	,  := ShellExec("echo OK", )
	if  != nil {
		return false
	}
	return strings.TrimSpace() == "OK"
}