<#
.SYNOPSIS
	Builds a repository 
.DESCRIPTION
	This PowerShell script builds a Git repository by supporting: cmake, configure, autogen, Imakefile, and Makefile.
.PARAMETER path
	Specifies the path to the Git repository (current working dir by default)
.EXAMPLE
	PS> ./build-repo.ps1 C:\Repos\ninja
	⏳ Building πŸ“‚ninja using CMakeLists.txt into πŸ“‚ninja/_My_Build...
	...
	βœ”οΈ Built πŸ“‚ninja in 47 sec
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$path = "$PWD")

function BuildInDir([string]$path) {
	$dirName = (Get-Item "$path").Name
	if (Test-Path "$path/CMakeLists.txt" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using CMakeLists.txt into πŸ“‚$dirName/_My_Build..."
		if (-not(Test-Path "$path/_My_Build/" -pathType container)) { 
			& mkdir "$path/_My_Build/"
		}
		Set-Location "$path/_My_Build/"

		& cmake ..
		if ($lastExitCode -ne "0") { throw "Executing 'cmake ..' has failed" }

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

		& make test
		if ($lastExitCode -ne "0") { throw "Executing 'make test' has failed" }

	} elseif (Test-Path "$path/configure" -pathType leaf) { 
		"⏳ Building πŸ“‚$dirName using 'configure'..."
		Set-Location "$path/"

		& ./configure
		#if ($lastExitCode -ne "0") { throw "Script 'configure' exited with error code $lastExitCode" }

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

		& make test
		if ($lastExitCode -ne "0") { throw "Executing 'make test' has failed" }

	} elseif (Test-Path "$path/autogen.sh" -pathType leaf) { 
		"⏳ Building πŸ“‚$dirName using 'autogen.sh'..."
		Set-Location "$path/"

		& ./autogen.sh
		if ($lastExitCode -ne "0") { throw "Script 'autogen.sh' exited with error code $lastExitCode" }

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

	} elseif (Test-Path "$path/build.gradle" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using build.gradle..."
		Set-Location "$path"

		& gradle build
		if ($lastExitCode -ne "0") { throw "'gradle build' has failed" }

		& gradle test
		if ($lastExitCode -ne "0") { throw "'gradle test' has failed" }

	} elseif (Test-Path "$path/Imakefile" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using Imakefile..."
		Set-Location "$path/"

		& xmkmf 
		if ($lastExitCode -ne "0") { throw "Executing 'xmkmf' has failed" }

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

	} elseif (Test-Path "$path/Makefile" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using Makefile..."
		Set-Location "$path"

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

	} elseif (Test-Path "$path/makefile" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using makefile..."
		Set-Location "$path"

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

	} elseif (Test-Path "$path/compile.sh" -pathType leaf) { 
		"⏳ Building πŸ“‚$dirName using 'compile.sh'..."
		Set-Location "$path/"

		& ./compile.sh
		if ($lastExitCode -ne "0") { throw "Script 'compile.sh' exited with error code $lastExitCode" }

		& make -j4
		if ($lastExitCode -ne "0") { throw "Executing 'make -j4' has failed" }

	} elseif (Test-Path "$path/attower/src/build/DevBuild/build.bat" -pathType leaf) {
		"⏳ Building πŸ“‚$dirName using build.bat ..."
		Set-Location "$path/attower/src/build/DevBuild/"

		& ./build.bat build-all-release
		if ($lastExitCode -ne "0") { throw "Script 'build.bat' exited with error code $lastExitCode" }

	} elseif (Test-Path "$path/$dirName" -pathType container) {
		"⏳ No make rule found, trying subfolder πŸ“‚$($dirName)..."
		BuildInDir "$path/$dirName"
	} else {
		Write-Warning "Sorry, no make rule applies to: πŸ“‚$dirName"
		exit 0 # success
	}
}

try {
	$stopWatch = [system.diagnostics.stopwatch]::startNew()

	if (-not(Test-Path "$path" -pathType container)) { throw "Can't access directory: $path" }

	$previousPath = Get-Location
	BuildInDir "$path"
	Set-Location "$previousPath"

	$repoDirName = (Get-Item "$path").Name
	[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
	"βœ”οΈ Built πŸ“‚$repoDirName in $elapsed sec"
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}