From 8d6f46c4ca2f3c3efd342a25335b925970fbb2ca Mon Sep 17 00:00:00 2001
From: partisan <none@noone.no>
Date: Tue, 10 Sep 2024 23:21:46 +0200
Subject: [PATCH] windows patch

---
 spitfire/build.go | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 45 insertions(+), 2 deletions(-)

diff --git a/spitfire/build.go b/spitfire/build.go
index ad98f10..bee99ea 100644
--- a/spitfire/build.go
+++ b/spitfire/build.go
@@ -5,13 +5,46 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+	"runtime"
 )
 
 // Array to store errors
 var errors []string
 
+// Check and Set MOZILLABUILD environment variable automatically
+func setMozillaBuildEnv() error {
+	// Check if MOZILLABUILD is already set
+	if os.Getenv("MOZILLABUILD") != "" {
+		fmt.Println("MOZILLABUILD environment variable is already set.")
+		return nil
+	}
+
+	// Try to detect the MozillaBuild installation directory
+	defaultPaths := []string{
+		"C:\\mozilla-build",
+		"C:\\Program Files\\mozilla-build",
+		"C:\\Program Files (x86)\\mozilla-build",
+	}
+
+	for _, path := range defaultPaths {
+		if _, err := os.Stat(path); !os.IsNotExist(err) {
+			// Set the MOZILLABUILD environment variable
+			fmt.Printf("Setting MOZILLABUILD environment variable to: %s\n", path)
+			return os.Setenv("MOZILLABUILD", path)
+		}
+	}
+
+	// If no directory was found, return an error
+	return fmt.Errorf("MozillaBuild directory not found. Please install MozillaBuild or set the MOZILLABUILD environment variable manually.")
+}
+
 // Run an external command like scp or rsync
 func runCommand(command string, args ...string) error {
+	// Make sure the MOZILLABUILD environment variable is set for the mach commands
+	if err := setMozillaBuildEnv(); err != nil {
+		return err
+	}
+
 	cmd := exec.Command(command, args...)
 	cmd.Stdout = os.Stdout
 	cmd.Stderr = os.Stderr
@@ -117,9 +150,19 @@ func UpdatePatches(patchesDir, patchesRepo, sourcePath string) {
 			errors = append(errors, "Failed to clone patches repository.")
 		}
 	}
+
+	// Handle platform-specific rsync command
 	fmt.Println("Copying files from patches directory to Firefox source directory...")
-	if err := runCommand("rsync", "-av", "--exclude=.git", patchesDir+"/", sourcePath+"/"); err != nil {
-		errors = append(errors, "Failed to copy files.")
+	if runtime.GOOS == "windows" {
+		// Use robocopy for Windows instead of rsync
+		if err := runCommand("robocopy", patchesDir, sourcePath, "/MIR"); err != nil {
+			errors = append(errors, "Failed to copy files (Windows robocopy).")
+		}
+	} else {
+		// Use rsync for Unix-like systems
+		if err := runCommand("rsync", "-av", "--exclude=.git", patchesDir+"/", sourcePath+"/"); err != nil {
+			errors = append(errors, "Failed to copy files (rsync).")
+		}
 	}
 }