param(
    [Parameter(Mandatory=$true)][string]$Branch,
    [string]$Push = ""
)

$ConfirmPreference = 'None'

function Assert-LastExitCode {
    param([string]$Message)
    if ($LASTEXITCODE -ne 0) {
        Write-Error "FAILED ($LASTEXITCODE): $Message"
        exit $LASTEXITCODE
    }
}

Write-Host "Cloning Fusion-Core..."
git clone -b $Branch --depth 1 git@github.com:Theme-Fusion/Fusion-Core.git fusion-core
Assert-LastExitCode "git clone Fusion-Core"

Write-Host "Installing & running Grunt on Fusion-Core..."
Set-Location fusion-core
npm install
Assert-LastExitCode "npm install (Fusion-Core)"
Get-ChildItem "languages" -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
grunt
Assert-LastExitCode "grunt (Fusion-Core)"

if ($Push -eq "push" -or $Push -eq "final") {
    git add .
    git commit -a -m "grunt - build script"
    git push origin $Branch
    Assert-LastExitCode "git push Fusion-Core"
}

Set-Location ..

Write-Host "Copying Fusion-Core to fusion-core-src..."
Copy-Item -Recurse fusion-core fusion-core-src

# Note: original script references ./Avada/ here (copy-paste artifact in the bash original). Mirroring faithfully.
Write-Host "Removing unnecessary files from Fusion-Core..."
Get-ChildItem -Path "./Avada/" -Recurse -Filter "*.map" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "./Avada/" -Recurse -Filter "*.less" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue

Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-core/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-core/.circleci"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-core/node_modules"

@(
    "fusion-core/.csscomb.json", "fusion-core/.csslintrc", "fusion-core/.editorconfig",
    "fusion-core/.eslintrc.json", "fusion-core/.gitignore", "fusion-core/.jscsrc",
    "fusion-core/.jshintignore", "fusion-core/.jshintrc", "fusion-core/.eslintignore",
    "fusion-core/.stylelintignore", "fusion-core/.stylelintrc.json",
    "fusion-core/circle.yml", "fusion-core/.DS_Store", "fusion-core/composer.json",
    "fusion-core/Gruntfile.js", "fusion-core/package.json", "fusion-core/package-lock.json",
    "fusion-core/phpstan.neon", "fusion-core/css/style.css"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

Get-ChildItem -Path "fusion-core" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "fusion-core/js" -Filter "*.js" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue

$keepRemoving = $true
while ($keepRemoving) {
    $emptyDirs = Get-ChildItem -Path "." -Recurse -Directory -ErrorAction SilentlyContinue |
        Where-Object { !(Get-ChildItem -Path $_.FullName -Force -ErrorAction SilentlyContinue) }
    if ($emptyDirs) { $emptyDirs | Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue } else { $keepRemoving = $false }
}

Write-Host "Removing unnecessary files from fusion-core-src..."
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-core-src/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-core-src/node_modules"

@(
    "fusion-core-src/circle.yml", "fusion-core-src/.DS_Store", "fusion-core-src/Gruntfile.js",
    "fusion-core-src/package.json", "fusion-core/phpstan.neon"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

Get-ChildItem -Path "fusion-core-src" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue

# Note: chmod is not applicable on Windows.

Write-Host "ZIP the new fusion-core plugin..."
Compress-Archive -Path "fusion-core" -DestinationPath "fusion-core.zip" -CompressionLevel Optimal -Force
Rename-Item "fusion-core" "fusion-core-normal"
Rename-Item "fusion-core-src" "fusion-core"
Compress-Archive -Path "fusion-core" -DestinationPath "fusion-core-src.zip" -CompressionLevel Optimal -Force
Rename-Item "fusion-core" "fusion-core-src"
Rename-Item "fusion-core-normal" "fusion-core"

Write-Host "DONE!"
