//go:build windows

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func consoleCmd(vars map[string]string, prof Profile, consoleMsg, tailMsg, tailFile, _ string) *exec.Cmd {
	script, err := os.CreateTemp("", "restray-shell-*.bat")
	if err != nil {
		return nil
	}
	fmt.Fprintf(script, "@echo off\r\n")
	fmt.Fprintf(script, "cd /d \"%s\"\r\n", configDir())
	fmt.Fprintf(script, "echo %s\r\n", consoleMsg)
	if tailFile != "" {
		fmt.Fprintf(script, "echo %s\r\n", tailMsg)
		fmt.Fprintf(script, "echo.\r\n")
		fmt.Fprintf(script, "powershell -Command \"Get-Content -Path '%s' -Wait\"\r\n", tailFile)
	}
	script.Close()

	cmd := exec.Command("cmd", "/c", "start", "", "cmd", "/k", script.Name())
	env := os.Environ()
	for k, v := range vars {
		env = append(env, k+"="+v)
	}
	cmd.Env = envWithBackendPath(env, prof)
	showConsole(cmd)
	return cmd
}
