<#

make-nuget.ps1

This script creates NuGet packages for:

 * MapGuide API base
 * MapGuide Web API
 * mg-desktop core
 * mg-desktop WinForms controls
 * CS-Map dictionaries

#>
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True)]
    [ValidateSet("x86", "x64")]
    [string]$cpu,
    
    [Parameter(Mandatory=$True)]
    [ValidateScript({Test-Path $_})]
    [string]$desktopBaseDir,
    
    [Parameter(Mandatory=$True)]
    [ValidateScript({Test-Path $_})]
    [string]$csMapBaseDir,

    [Parameter(Mandatory=$True)]
    [ValidateScript({Test-Path $_})]
    [string]$webBinDir,

    [Int]$major = 1,

    [Int]$minor = 0,

    [Int]$patch = 0,

    [Int]$rev = 0
)

$fullVer = "$major.$minor.$patch.$rev"

Write-Host "CPU: $cpu"
Write-Host "mg-desktop base dir: $desktopBaseDir"
Write-Host "cs-map base dir: $csMapBaseDir"
Write-Host "web bin base dir: $webBinDir"
Write-Host "Version: $fullVer"

Function Prepare-Staging {
    Param(
        [string]$packageName,
        [string]$contentDirName,
        [string]$baseDir,
        [string[]]$contentFileList,
        [string[]]$libFileList
    )

    Write-Host "[prepare]: Copy $packageName files to nuget staging"
    $destinationFolder = ".\$cpu\$packageName\$contentDirName"
    if (!(Test-Path -path $destinationFolder)) {
        New-Item $destinationFolder -Type Directory | Out-Null
    }
    foreach ($file in $contentFileList) {
        Copy-Item -Path "$baseDir\$file" $destinationFolder -Force
    }
    foreach ($file in $libFileList) {
        Copy-Item -Path "$baseDir\$file" ".\$cpu\$packageName\lib\net40"
    }
}

# Copy files to staging areas

# api-base
$baseFileSetContent = @(
    "ACE.dll"
    "GEOS.dll",
    "MgFoundation.dll",
    "MgGeometry.dll",
    "MgMdfModel.dll",
    "MgMdfParser.dll",
    "MgPlatformBase.dll",
    "xerces-c_3_1mg.dll",
    "FoundationUnmanagedApi.dll",
    "GeometryUnmanagedApi.dll",
    "PlatformBaseUnmanagedApi.dll"
)
$baseFileSetLibs = @(
    "OSGeo.MapGuide.Foundation.dll",
    "OSGeo.MapGuide.Geometry.dll",
    "OSGeo.MapGuide.PlatformBase.dll",
    "OSGeo.MapGuide.Foundation.xml",
    "OSGeo.MapGuide.Geometry.xml",
    "OSGeo.MapGuide.PlatformBase.xml"
)
Prepare-Staging "api-base" "mapguide-api-base" $webBinDir $baseFileSetContent $baseFileSetLibs

# api-web
$webFileSetContent = @(
    "MgWebApp.dll"
    "MgWebSupport.dll",
    "MgHttpHandler.dll",
    "MgMapGuideCommon.dll",
    "MapGuideCommonUnmanagedApi.dll",
    "lib_json.dll",
    "WebUnmanagedApi.dll"
)
$webFileSetLibs = @(
    "OSGeo.MapGuide.MapGuideCommon.dll",
    "OSGeo.MapGuide.Web.dll",
    "OSGeo.MapGuide.MapGuideCommon.xml",
    "OSGeo.MapGuide.Web.xml"
)
Prepare-Staging "api-web" "mapguide-api-web" $webBinDir $webFileSetContent $webFileSetLibs

# api-desktop
Write-Host "[prepare]: Copy api-desktop files to nuget staging"
$destDir = ".\$cpu\api-desktop\mg-desktop"
if (!(Test-Path -path $destDir)) {
    New-Item $destDir -Type Directory | Out-Null
}
Copy-Item -Path "$desktopBaseDir\gd.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MapGuideDesktopUnmanagedApi.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgDesktop.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgGwsCommon.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgGwsQueryEngine.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgGwsResource.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgRenderers.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\MgStylization.dll" $destDir -Force
Copy-Item -Path "$desktopBaseDir\Platform.ini" $destDir -Force
Copy-Item -Path "$desktopBaseDir\FDO" $destDir -Force -Recurse
Copy-Item -Path "$desktopBaseDir\Resources" $destDir -Force -Recurse
Copy-Item -Path "$desktopBaseDir\Schema" $destDir -Force -Recurse
Copy-Item -Path "$desktopBaseDir\OSGeo.MapGuide.Desktop.dll" ".\$cpu\api-desktop\lib\net40" -Recurse -Force
Copy-Item -Path "$desktopBaseDir\OSGeo.MapGuide.Desktop.xml" ".\$cpu\api-desktop\lib\net40" -Recurse -Force

# api-desktop-viewer
$viewerFileSetContent = @()
$viewerFileSetLibs = @(
    "OSGeo.MapGuide.Viewer.dll",
    "OSGeo.MapGuide.Viewer.Desktop.dll",
    "OSGeo.MapGuide.Viewer.xml"
)
Prepare-Staging "api-desktop-viewer" "" $desktopBaseDir $viewerFileSetContent $viewerFileSetLibs

#cs-map-dictionaries
Write-Host "[prepare]: Copy cs-map-dictionaries files to nuget staging"
$destDir = ".\cs-map-dicts\dictionaries"
if (!(Test-Path -path $destDir)) {
    New-Item $destDir -Type Directory | Out-Null
}
Copy-Item -Path "$csMapBaseDir\*.CSD" $destDir -Force
Copy-Item -Path "$csMapBaseDir\*.gdc" $destDir -Force
Copy-Item -Path "$csMapBaseDir\*.txt" $destDir -Force
Copy-Item -Path "$csMapBaseDir\OSTN02._02" $destDir -Force
Copy-Item -Path "$csMapBaseDir\OSTN97._nt" $destDir -Force
Copy-Item -Path "$csMapBaseDir\*.csv" $destDir -Force

# Write out templated nuspec files, plugging in the version number
$nuspecFiles = @(
    "mapguide-api-base",
    "mapguide-api-web",
    "mg-desktop",
    "mg-desktop-viewer",
    "cs-map-dictionaries"
)
foreach ($nuspec in $nuspecFiles) {
    if (Test-Path -path $nuspec) {
        Remove-Item -Path $nuspec
    }
    $tpl = ".\$nuspec.nuspec.tpl"
    Write-Host "[prepare]: $nuspec"
    (Get-Content $tpl).Replace("MG_CPU", $cpu).Replace("MG_VERSION", $fullVer) | Set-Content ".\$nuspec-$cpu.nuspec"
}

& .\NuGet.exe pack ".\mapguide-api-base-$cpu.nuspec" -BasePath "$cpu\api-base"
& .\NuGet.exe pack ".\mapguide-api-web-$cpu.nuspec" -BasePath "$cpu\api-web"
& .\NuGet.exe pack ".\mg-desktop-$cpu.nuspec" -BasePath "$cpu\api-desktop"
& .\NuGet.exe pack ".\mg-desktop-viewer-$cpu.nuspec" -BasePath "$cpu\api-desktop-viewer"
& .\NuGet.exe pack ".\cs-map-dictionaries-$cpu.nuspec" -BasePath cs-map-dicts