#POSTBUILD powershell -ExecutionPolicy Unrestricted $(ProjectDir)deploy.ps1 -ProjectDir $(ProjectDir) -SolutionDir $(SolutionDir) -OutDir $(OutDir) -ConfigurationName $(ConfigurationName)
param([string]$OutDir,
    [string]$ConfigurationName,
    [string]$SolutionDir,
    [string]$ProjectDir);

$copyAssembly = 0
$copyWorkspace = 0
$signAssembly = 1
$ProductName="C4IT - F4SD - WebApi for M42"


$targetPath = "\\srvwsm001.imagoverum.com\c$\Program Files (x86)\Matrix42\Matrix42 Workplace Management"
$minifyApi = "https://www.toptal.com/developers/javascript-minifier/api/raw"


#minify js file
$workdirPath = "$ProjectDir${OutDir}"
$workdirPathZip = "$workdirPath\zip"


Get-ChildItem -Path "$workdirPathZip" -File -Recurse | Remove-Item
Expand-Archive -Path "$workdirPath\F4SD - M42.zip" -DestinationPath $workdirPathZip

$buildDate = $((get-date).ToLocalTime()).ToString("yyyy-MM-dd")
$jsCode = Get-Content -Path "$workdirPath\F4SD.js" -Raw        
$body = @{input=$jsCode}
$contentType = 'application/x-www-form-urlencoded' 
$resp = Invoke-WebRequest -Method POST -Uri $minifyApi -body $body -ContentType $contentType    
$workspaceVersion = (Get-Content -Raw -Path "$workdirPath\workspace.json" | ConvertFrom-Json).version
[IO.File]::WriteAllLines("$workdirPath\F4SD.min.js","/*v$workspaceVersion ($buildDate)*/" + $resp.Content)

$finalWorkspacePath = "$workdirPathZip\Files\WM\workspaces\C4IT_F4SD"
New-Item -ItemType Directory -Force -Path $finalWorkspacePath | out-null
Copy-Item "$workdirPath\F4SD.min.js" -Destination "$finalWorkspacePath"
Copy-Item "$workdirPath\F4SDIcons.svg" -Destination "$finalWorkspacePath"
Copy-Item "$workdirPath\workspace.json" -Destination "$finalWorkspacePath"

$finalAssemblyPath = "$workdirPathZip\Assemblies\svc\bin\"
New-Item -ItemType Directory -Force -Path $finalWorkspacePath | out-null

if($signAssembly -eq 1)
    {
        
        $SignTool="$SolutionDir\..\..\Workspaces\Common Code\Tools\signtool.exe"
        $TimeStamp="http://rfc3161timestamp.globalsign.com/advanced"
        & $SignTool sign /a /tr $TimeStamp /td SHA256 /fd SHA256  /d $ProductName C:\Users\dm134\source\repos\F4SDM42WebApi\F4SDM42WebApi\bin\Release\C4ITF4SDM42WebApi.dll C:\Users\dm134\source\repos\F4SDM42WebApi\F4SDM42WebApi\bin\Release\C4ITF4SDM42WebApiHelper.dll
    }





Get-ChildItem -Path "$ProjectDir${OutDir}C4ITF4SD*" -Include *.dll | Copy-Item -Destination $finalAssemblyPath

Remove-Item "$workdirPath\F4SD - M42 v$workspaceVersion.zip"
Compress-Archive -Path "$workdirPathZip\*" -DestinationPath "$workdirPath\F4SD - M42 v$workspaceVersion.zip"




IF( $ConfigurationName -eq "Release_and_copy" -OR $ConfigurationName -eq "Debug_and_copy" )
{   
    #Copy Assembly
    if($copyAssembly -eq 1)
    {
        Get-ChildItem -Path "$ProjectDir${OutDir}C4ITF4SD*" -Include *.dll | Copy-Item -Destination "$targetPath\svc\bin\"
    }
    #Copy Workspace
    if($copyWorkspace -eq 1)
    {
        $targetWorkspacePath = "$targetPath\WM\workspaces\C4IT_F4SD"
        New-Item -ItemType Directory -Force -Path $targetWorkspacePath | out-null
        Get-ChildItem -Path "$finalWorkspacePath\*" -Include * | Copy-Item -Destination $targetWorkspacePath
    }

}