param(
    [Parameter(Mandatory=$true)][string]$AvadaBranch,
    [Parameter(Mandatory=$true)][string]$FusionCoreBranch,
    [Parameter(Mandatory=$true)][string]$FusionBuilderBranch,
    [Parameter(Mandatory=$true)][string]$FusionLibraryBranch,
    [Parameter(Mandatory=$true)][string]$FWLBBranch,
    [string]$Mode = ""
)

# Usage:
#   powershell -ExecutionPolicy Bypass -File build-release.ps1 development development development development development
#   powershell -ExecutionPolicy Bypass -File build-release.ps1 development development development development development push
#   powershell -ExecutionPolicy Bypass -File build-release.ps1 development development development development development final

$ConfirmPreference = 'None'

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

Write-Host "------------------------"
Write-Host "Creating folder in ../avada-release-pack"
Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "../avada-release-pack"
New-Item -ItemType Directory -Force "../avada-release-pack" | Out-Null

# Copy sub-scripts to the release pack folder
Copy-Item "build-avada.ps1" "../avada-release-pack/build-avada.ps1"
Copy-Item "build-fb.ps1"    "../avada-release-pack/build-fb.ps1"
Copy-Item "build-fc.ps1"    "../avada-release-pack/build-fc.ps1"
Copy-Item "build-fwlb.ps1"  "../avada-release-pack/build-fwlb.ps1"

Set-Location "../avada-release-pack"

& ".\build-avada.ps1" -AvadaBranch $AvadaBranch -FusionLibraryBranch $FusionLibraryBranch -PushTheme $Mode -PushLibrary $Mode
Assert-LastExitCode "build-avada.ps1"
Remove-Item "build-avada.ps1" -Force -ErrorAction SilentlyContinue

& ".\build-fc.ps1" -Branch $FusionCoreBranch -Push $Mode
Assert-LastExitCode "build-fc.ps1"
Remove-Item "build-fc.ps1" -Force -ErrorAction SilentlyContinue

& ".\build-fb.ps1" -FBBranch $FusionBuilderBranch -FusionLibraryBranch $FusionLibraryBranch -PushPlugin $Mode -PushLibrary $Mode
Assert-LastExitCode "build-fb.ps1"
Remove-Item "build-fb.ps1" -Force -ErrorAction SilentlyContinue

& ".\build-fwlb.ps1" -Branch $FWLBBranch -Push $Mode
Assert-LastExitCode "build-fwlb.ps1"
Remove-Item "build-fwlb.ps1" -Force -ErrorAction SilentlyContinue

if ($Mode -eq "push" -or $Mode -eq "final") {
    Write-Host "Cloning Languages..."
    git clone --depth 1 git@github.com:Theme-Fusion/Localization-l10n.git languages
    Assert-LastExitCode "git clone Localization-l10n"

    Write-Host "Copying language files to the languages folder..."
    Copy-Item "Avada/languages/Avada.pot"                                             "languages/Avada/Avada.pot"
    Copy-Item "fusion-core/languages/fusion-core.pot"                                 "languages/fusion-core/fusion-core.pot"
    Copy-Item "fusion-builder/languages/fusion-builder.pot"                           "languages/fusion-builder/fusion-builder.pot"
    Copy-Item "fusion-white-label-branding/languages/fusion-white-label-branding.pot" "languages/fusion-white-label-branding/fusion-white-label-branding.pot"

    Set-Location languages
    Write-Host "Installing & running grunt in the languages folder..."
    npm install
    Assert-LastExitCode "npm install (languages)"
    npm audit fix
    grunt
    Write-Host "grunt (languages) finished with exit code $LASTEXITCODE (warnings are non-fatal)."
    Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "node_modules"
    Set-Location ..
}

if ($Mode -eq "push" -or $Mode -eq "final") {
    Write-Host "Committing & pushing new languages to the GitHub repository..."
    Set-Location languages
    git add .
    git commit -a -m "Updating language files"
    git push origin master
    Assert-LastExitCode "git push languages"
    Set-Location ..
}

if ($Mode -eq "final") {
    Write-Host "Starting to build the Full Package..."
    git clone -b full-package-assets --depth 1 git@github.com:Theme-Fusion/Avada.git Avada_Full_Package
    Assert-LastExitCode "git clone Avada full-package-assets"
    Copy-Item "Avada.zip" "Avada_Full_Package/Avada Theme/Avada.zip"
    New-Item -ItemType Directory -Force "Avada_Full_Package/PSD" | Out-Null
    Invoke-WebRequest -Uri "https://updates.theme-fusion.com/psd/psd.zip" -OutFile "Avada_Full_Package/PSD/psd.zip"
    Copy-Item -Recurse "languages" "Avada_Full_Package/Languages"

    # Cleanup Languages folder
    Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada_Full_Package/Languages/.git"
    Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada_Full_Package/Languages/node_modules"
    Remove-Item -Force -ErrorAction SilentlyContinue "Avada_Full_Package/Languages/Gruntfile.js"
    Remove-Item -Force -ErrorAction SilentlyContinue "Avada_Full_Package/Languages/.gitignore"
    Get-ChildItem "Avada_Full_Package/Languages" -Filter "*.json" | Remove-Item -Force -ErrorAction SilentlyContinue
    Get-ChildItem "Avada_Full_Package/Languages/Avada"                       -Filter "*.zip" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
    Get-ChildItem "Avada_Full_Package/Languages/fusion-core"                 -Filter "*.zip" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
    Get-ChildItem "Avada_Full_Package/Languages/fusion-builder"              -Filter "*.zip" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
    Get-ChildItem "Avada_Full_Package/Languages/fusion-white-label-branding" -Filter "*.zip" -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
    Remove-Item -Recurse -Force -Confirm:$false -ErrorAction SilentlyContinue "Avada_Full_Package/.git"

    # Note: chmod is not applicable on Windows.

    Write-Host "Zipping Avada_Full_Package..."
    Compress-Archive -Path "Avada_Full_Package" -DestinationPath "Avada_Full_Package.zip" -CompressionLevel Optimal -Force
}

Set-Location ..

Write-Host "DONE!"
Write-Host "You can get the final zip files from the ../avada-release-pack folder."
