// Copyright Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;
using System.IO;

public class RuntimeSpeechRecognizerVosk : ModuleRules
{
	// In earlier UE versions (such as 4.27), the architecture string is empty.
	protected string ArchitectureString
	{
		get
		{
			if (Target.Architecture.ToString().Length > 0)
			{
				return Target.Architecture.ToString();
			}
			else
			{
				return "x64";
			}
		}
	}
	
	public RuntimeSpeechRecognizerVosk(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;
		
		PublicDependencyModuleNames.AddRange(
			new string[]
			{
				"Core",
				"RuntimeSpeechRecognizer"
			}
		);

		PrivateDependencyModuleNames.AddRange(
			new string[]
			{
				"CoreUObject",
				"Engine",
				"Json",
				"SignalProcessing",
				"AudioPlatformConfiguration"
			}
		);
		
		PrivateDependencyModuleNames.Add("Projects");
		
		if (Target.Platform == UnrealTargetPlatform.Android)
		{
			PrivateDependencyModuleNames.Add("Launch");

			string PluginPath = Utils.MakePathRelativeTo(ModuleDirectory, Target.RelativeEnginePath);
			AdditionalPropertiesForReceipt.Add("AndroidPlugin", Path.Combine(PluginPath, "RuntimeSpeechRecognizerVosk_APL.xml"));
		}
		
		string PlatformDir = Target.Platform.ToString();
		string IncDirPath = Path.Combine(ModuleDirectory, "..", "ThirdParty", "vosk", "Include");
		string LibDirPath = "";
		string RelativeLibDirPath = "";
		string LibFileName = "UNSUPPORTED_PLATFORM";
		if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "vosk", PlatformDir);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			LibFileName = "libvosk.dll";
			PublicAdditionalLibraries.Add(Path.Combine(LibDirPath, "vosk_api.lib"));
			RuntimeDependencies.Add(Path.Combine(LibDirPath, LibFileName));
			PublicDelayLoadDLLs.Add(LibFileName);
			PrivateIncludePaths.Add(IncDirPath);
		}
		else if (Target.Platform == UnrealTargetPlatform.Android)
		{
			RelativeLibDirPath = Path.Combine("..", "ThirdParty", "vosk", PlatformDir);
			LibDirPath = Path.Combine(ModuleDirectory, RelativeLibDirPath);
			LibFileName = "libvosk.so";

			string[] Architectures = new string[]
			{
				"arm64-v8a",
				"armeabi-v7a",
			};

			foreach (string Arch in Architectures)
			{
				string LibPath = Path.Combine(LibDirPath, Arch, LibFileName);
				if (File.Exists(LibPath))
				{
					PublicAdditionalLibraries.Add(LibPath);
					RuntimeDependencies.Add(LibPath);
				}
			}

			PrivateIncludePaths.Add(IncDirPath);
		}

		string RelativeSharedLibPath = Path.Combine(RelativeLibDirPath, LibFileName).Replace('\\', '/');

		PublicDefinitions.Add("VOSK_SHAREDLIB_PATH=" + RelativeSharedLibPath);
	}
}