param(
    [Parameter(Mandatory=$true)][string]$FBBranch,
    [Parameter(Mandatory=$true)][string]$FusionLibraryBranch,
    [string]$PushPlugin = "",
    [string]$PushLibrary = ""
)

$ConfirmPreference = 'None'

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

Write-Host "Cloning Fusion-Builder..."
git clone -b $FBBranch --depth 1 git@github.com:Theme-Fusion/Fusion-Builder.git fusion-builder
Assert-LastExitCode "git clone Fusion-Builder"

Write-Host "Installing & running Grunt on Fusion-Builder..."
Set-Location fusion-builder
Get-ChildItem "languages" -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue

Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "inc/lib"
git clone -b $FusionLibraryBranch --depth 1 git@github.com:Theme-Fusion/Fusion-Library.git inc/lib
Assert-LastExitCode "git clone Fusion-Library into Fusion-Builder"

if ($PushLibrary -eq "push") {
    Set-Location "inc/lib"
    npm install
    Assert-LastExitCode "npm install (Fusion-Library)"
    grunt
    Assert-LastExitCode "grunt (Fusion-Library)"
    git add .
    git commit -a -m "grunt - build script"
    git push origin $FusionLibraryBranch
    Assert-LastExitCode "git push Fusion-Library"
    Set-Location "../.."
}

npm install
Assert-LastExitCode "npm install (Fusion-Builder)"
grunt
Assert-LastExitCode "grunt (Fusion-Builder)"

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

Set-Location ..

Write-Host "Copying Fusion-Builder to fusion-builder-src..."
Copy-Item -Recurse fusion-builder fusion-builder-src

Write-Host "Removing unnecessary files from Fusion-Builder..."
Get-ChildItem -Path "./fusion-builder/" -Recurse -Filter "*.map" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "./fusion-builder/" -Recurse -Filter "*.less" | Remove-Item -Force -ErrorAction SilentlyContinue

Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/inc/lib/assets/js"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/.circleci"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/tests"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/assets/js/general"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/assets/js/library"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/php-to-js"

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

Get-ChildItem -Path "fusion-builder" -Filter "*.sh"  | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "fusion-builder" -Filter "*.ps1" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "fusion-builder" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue

# Fusion-Library cleanup
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/inc/lib/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/inc/lib/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/inc/lib/.circleci"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder/inc/lib/tests"

@(
    "fusion-builder/inc/lib/phpunit.xml.dist", "fusion-builder/inc/lib/circle.yml",
    "fusion-builder/inc/lib/.jshintrc", "fusion-builder/inc/lib/package.json",
    "fusion-builder/inc/lib/composer.json", "fusion-builder/inc/lib/.eslintrc.json",
    "fusion-builder/inc/lib/.eslintignore", "fusion-builder/inc/lib/.editorconfig",
    "fusion-builder/inc/lib/.jscsrc", "fusion-builder/inc/lib/.gitignore",
    "fusion-builder/inc/lib/Gruntfile.js", "fusion-builder/inc/lib/package-lock.json",
    "fusion-builder/inc/lib/inc/fusion-app/css/fusion-builder-frame.css",
    "fusion-builder/inc/lib/inc/fusion-app/assets/fonts/icomoon.css",
    "fusion-builder/inc/lib/inc/fusion-app/assets/css/jquery-ui/jquery-ui.css",
    "fusion-builder/inc/lib/assets/less/fusion-custom-icons.less",
    "fusion-builder/inc/lib/assets/less/icomoon.less",
    "fusion-builder/inc/lib/assets/css/icomoon.css",
    "fusion-builder/inc/lib/inc/redux/framework/package.json"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

Get-ChildItem -Path "fusion-builder/inc/lib" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "fusion-builder/inc/lib" -Filter "phpcs-php-version.*" | 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-builder-src..."
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/tests"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/php-to-js"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/inc/lib/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "fusion-builder-src/inc/lib/.git"

@(
    "fusion-builder-src/.DS_Store", "fusion-builder-src/Gruntfile.js",
    "fusion-builder-src/package-lock.json",
    "fusion-builder-src/inc/lib/package-lock.json"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

# Note: chmod is not applicable on Windows.

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

Write-Host "DONE!"
