package utils

import (
	"io"
	"os"
	"os/exec"
	"strconv"
)

// FileExists check file is exists
func FileExists(path string) bool {
	_, err := os.Stat(path)
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}

// PidExists checkou process id is exists
func PidExists(pid int) bool {
	out, _ := exec.Command("kill", "-s", "0", strconv.Itoa(pid)).CombinedOutput()
	if string(out) == "" {
		return true // pid exist
	}
	return false
}

// CopyFile copy file from src to dest
func CopyFile(srcFile, destFile string) error {
	file, err := os.Open(srcFile)
	if err != nil {
		return err
	}

	defer file.Close()

	dest, err := os.Create(destFile)
	if err != nil {
		return err
	}

	defer dest.Close()

	_, err = io.Copy(dest, file)
	if err != nil {
		return err
	}

	return nil
}
