package server

//! For Go only implementation
//! To run the server from a Go process

// func initServer(opts *options.Options, binName, inputDir, destDir string) {
// 	start := time.Now()

// 	if runtime.GOOS == "windows" {
// 		binName = binName + ".exe"
// 	}

// 	subDir := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
// 	binDirPath := filepath.Join(destDir, subDir)
// 	binPath := filepath.Join(binDirPath, binName)

// 	runBuildCMD(opts, destDir, binDirPath, binPath)

// 	fmt.Fprintf(os.Stdout, "[webframework] Build %s complete %s", binName, time.Now().Sub(start))

// 	// TODO: Only if dev command
// 	runServer(opts, destDir)
// }

// func runBuildCMD(opts *options.Options, destDir, binDirPath, binPath string) {

// 	if _, err := os.Stat(binDirPath); os.IsNotExist(err) {
// 		err := os.MkdirAll(binDirPath, os.ModePerm)
// 		if err != nil {
// 			log.Fatalf("\nFailed to make binary directory while running server command at %s\n%s\n", binDirPath, err)
// 		}
// 	}

// 	cmd := exec.Command("go", "build", "-o", binPath)
// 	cmd.Dir = destDir

// 	stdout, err := cmd.Output()
// 	if err != nil {
// 		log.Fatalf("\nFailed to exec command\n%s\n", err.Error())
// 		return
// 	}

// 	fmt.Println(string(stdout))
// }

// func runServer(opts *options.Options, destDir string) {
// 	options, err := json.Marshal(opts)
// 	if err != nil {
// 		log.Fatalf("\nFailed to marshal options to JSON\n%s\n", err)
// 	}

// 	go func() {
// 		cmd := exec.Command("bash", "-c", fmt.Sprintf("cd %s && go run server.go %s", destDir, "\""+strings.Replace(string(options), "\"", "\\\"", -1)+"\""))

// 		stdout, err := cmd.StdoutPipe()
// 		if err != nil {
// 			log.Fatalf("\nFailed to exec command\n%s\n", err.Error())
// 			return
// 		}

// 		cmd.Start()

// 		scanner := bufio.NewScanner(stdout)
// 		scanner.Split(bufio.ScanWords)

// 		for scanner.Scan() {
// 			m := scanner.Text()
// 			fmt.Println(m)
// 		}

// 		cmd.Wait()
// 	}()
// }

// /home/james/Projects/packages/webframework/modules/framework/bin/linux-amd64/compiler "{\"Name\":\"webframework\",\"root\":\"/home/james/Projects/packages/webframework/project\",\"port\":3000,\"ext\":\".html\",\"appDir\":\"app\",\"appPath\":\"/home/james/Projects/packages/webframework/project/src/app\",\"outDir\":\"build\",\"outPath\":\"/home/james/Projects/packages/webframework/project/build\",\"cachePath\":\"/home/james/Projects/packages/webframework/project/build/cache\",\"CacheClientPath\":\"/home/james/Projects/packages/webframework/project/build/client\",\"depGraphPath\":\"/home/james/Projects/packages/webframework/project/build/cache/dep_graph.json\",\"dev\":true,\"moduleDev\":true,\"public\":\"src/public\",\"styles\":\"src/styles\"}" "{\"action\": \"STARTING_BUILD\", \"message\": \"Starting build...\", \"actionPath\": \".\", \"ext\": \"any\"}"
