param(
    [Parameter(Mandatory=$true)][string]$AvadaBranch,
    [Parameter(Mandatory=$true)][string]$FusionLibraryBranch,
    [string]$PushTheme = "",
    [string]$PushLibrary = ""
)

$ConfirmPreference = 'None'

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

Write-Host "Cloning Avada..."
git clone -b $AvadaBranch --depth 1 git@github.com:Theme-Fusion/Avada.git Avada
Assert-LastExitCode "git clone Avada"

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

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

if ($PushLibrary -eq "push" -or $PushLibrary -eq "final") {
    Set-Location "includes/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 "../.."
}

if ($PushTheme -eq "push" -or $PushLibrary -eq "final") {
    git add .
    git commit -a -m "grunt - build script"
    git push origin $AvadaBranch
    Assert-LastExitCode "git push Avada"
}

Set-Location ..

Write-Host "Copying Avada to Avada-src..."
Copy-Item -Recurse Avada Avada-src

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

Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/.circleci"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/.sass-cache"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/bin"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/tests"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/includes/lib/assets/js"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada/assets/js"

@(
    "Avada/.DS_Store", "Avada/.editorconfig", "Avada/.gitattributes",
    "Avada/.stylelintignore", "Avada/.stylelintrc.json", "Avada/.eslintrc.json",
    "Avada/.eslintignore", "Avada/.gitignore", "Avada/.gitmodules",
    "Avada/.jscsrc", "Avada/.jshintrc", "Avada/phpunit.xml.dist",
    "Avada/circle.yml", "Avada/Gruntfile.js", "Avada/composer.json",
    "Avada/package.json", "Avada/package-lock.json", "Avada/phpstan.neon",
    "Avada/phpunit.xml", "Avada/assets/js/main.js",
    "Avada/assets/css/animations.css", "Avada/assets/css/bbpress.css",
    "Avada/assets/css/lightbox.css", "Avada/assets/css/rtl.css",
    "Avada/assets/css/shared.css", "Avada/assets/css/style.css",
    "Avada/assets/css/woocommerce.css",
    "Avada/frontend_diagram.xml", "Avada/new_frontend_diagram.xml"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

Get-ChildItem -Path "Avada" -Filter "*.sh"  | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "Avada" -Filter "*.ps1" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "Avada" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "Avada" -Filter "phpcs-php-version.*" | Remove-Item -Force -ErrorAction SilentlyContinue

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

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

Get-ChildItem -Path "Avada/includes/lib" -Filter "phpcs.*" | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "Avada/includes/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 Avada-src..."
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/.circleci"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/.git"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/.sass-cache"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/bin"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/tests"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/includes/lib/node_modules"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada-src/includes/lib/.git"

Get-ChildItem -Path "Avada-src" -Filter "*.sh"  | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path "Avada-src" -Filter "*.ps1" | Remove-Item -Force -ErrorAction SilentlyContinue

@(
    "Avada-src/.DS_Store", "Avada-src/.gitmodules", "Avada-src/.gitignore",
    "Avada-src/includes/lib/.gitignore", "Avada-src/includes/lib/Gruntfile.js",
    "Avada-src/includes/lib/package-lock.json",
    "Avada/frontend_diagram.xml", "Avada/new_frontend_diagram.xml"
) | ForEach-Object { Remove-Item -Force -ErrorAction SilentlyContinue $_ }

# Note: chmod is not applicable on Windows.

Write-Host "ZIP Avada..."
Compress-Archive -Path "Avada" -DestinationPath "Avada.zip" -CompressionLevel Optimal -Force
Rename-Item "Avada" "Avada-normal"
Rename-Item "Avada-src" "Avada"
Compress-Archive -Path "Avada" -DestinationPath "Avada-src.zip" -CompressionLevel Optimal -Force
Rename-Item "Avada" "Avada-src"
Rename-Item "Avada-normal" "Avada"

Write-Host "DONE!"
