package main

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

// Function to check for updates and restart the server if an update is found
func checkForUpdates() {
	repoURL := "https://weforgecode.xyz/Spitfire/Search.git"
	localDir := "." // Assume the repository is cloned in the current directory

	for {
		err := gitPull(repoURL, localDir)
		if err != nil {
			fmt.Println("Error checking for updates:", err)
			time.Sleep(10 * time.Minute)
			continue
		}

		fmt.Println("Update found. Syncing updates...")
		nodeUpdateSync()

		fmt.Println("Restarting server to apply updates...")
		update()
		time.Sleep(10 * time.Minute)
	}
}

// Function to pull updates from the Git repository
func gitPull(repoURL, localDir string) error {
	cmd := exec.Command("git", "-C", localDir, "pull", repoURL)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

// Function to download updates and restart the server
func update() {
	cmd := exec.Command("sh", "run.sh")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Start()
	if err != nil {
		fmt.Println("Error starting the server:", err)
		return
	}

	os.Exit(0)
}