<#
    This a small lasmerge wrapper.
    Run it with:
    PowerShell -NoProfile -ExecutionPolicy Bypass -file lasmerge_wrapper.ps1 "c:\path\to\ground" "c:\path\to\object"
#>

param($lasFilePathGround, $lasFilePathObject);

$lasmerge = 'lasmerge.exe'
Write-Host "Executing Post 27F Transformation"
Write-Host $lasFilePathGround
Write-Host $lasFilePathObject

# loop over all laz files
$lasFilesObject = Get-ChildItem $lasFilePathObject\* -Include *.las, *.laz

Get-ChildItem $lasFilePath\* -Include *.las, *.laz |
   ForEach-Object {
        $allArguments = ""

        # get filename with full path 
        $fileNameFullGround = $_.FullName
        $fileNameFullObject = 
        Write-Host "Starting $fileNameFullGround"       

        # generating output filename
        $outputFilename = $_.Name.split("_BE")[0] # will be used to match object file name
        $outputFile = Join-Path $lasFilePathGround $outputFilename
        $outputFile = Join-Path $outputFile ".laz"

        # getting object las filename 
        Get-ChildItem $lasFilePathObject\* -Include *.las, *.laz | ForEach-Object {
            if($_.Name -match $outputFilename){
                $fileNameObject = $_.FullName
            }
        }

        # setting up lasRotate arguments
        $allArguments += @(" -i " + $fileNameFullGround)
        $allArguments += @(" -i " + $fileNameObject)
        $allArguments += @(" -o " + $outputFile)
        $allArguments += @(" -v")

        Write-Host "Executing ..."
        $Command = $allArguments -join ' '
        
        Try{
            $Process = Start-Process -FilePath $lasmerge -ArgumentList $allArguments -Wait #-ArgumentList "$allArguments" -Wait
        }
        Catch{
            Write-Host "Failed with files $fileNameFullGround $fileNameObject"
            Continue
        }           
   }
Write-Host "All Done!"