package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"strings"
)

var target = "../www/"

func main() {
	fmt.Println("=============start==============")
	dir := flag.String("dir", "", "dir")
	flag.Parse()
	target += *dir + "/"
	fmt.Println("target_dir", target)
	os.RemoveAll(target + "static")
	os.Remove(target + "index.html")
	os.Remove(target + "favicon.ico")

	copyDir("./dist", target)
}
func FormatPath(s string) string {
	switch runtime.GOOS {
	case "windows":
		return strings.Replace(s, "/", "\\", -1)
	case "darwin", "linux":
		return strings.Replace(s, "\\", "/", -1)
	default:
		return s
	}
}

func copyDir(src string, dest string) {
	src = FormatPath(src)
	dest = FormatPath(dest)
	var cmd *exec.Cmd
	switch runtime.GOOS {
	case "windows":
		cmd = exec.Command("xcopy", src, dest, "/I", "/E")
	case "darwin", "linux":
		cmd = exec.Command("cp", "-R", src, dest)
	}

	outPut, e := cmd.Output()
	if e != nil {
		fmt.Println("copy error", e.Error())
		return
	}
	fmt.Println(string(outPut))
}
