# PowerShell script to checkout arm_am-pom and asset-gui projects

# Configuration
$SVN_BASE_URL = "https://svn.armundia.com/svn/PRJarm_am/branches/ISPBEstero/ADV360-BES-JB8_B2024_2"
$SVN_BASE_URL_BW2_LIB = "https://svn.armundia.com/svn/PRJbw2/branches/LIB-JK10"
$SVN_BASE_URL_ARM_LIB = "https://svn.armundia.com/svn/PRJarm_am/branches/LIB-JK10"
$WORKSPACE_DIR = "C:\Dev2012\source\WindSurf\adv8"

# Project paths
$PROJECTS = @{
    "arm_am-pom"         = "$SVN_BASE_URL/arm_am-pom"
    "asset-gui"          = "$SVN_BASE_URL/asset-gui"
    "AmazonAWS.S3"       = "$SVN_BASE_URL_BW2_LIB/AmazonAWS.S3"
    "arm-core-batch"     = "$SVN_BASE_URL_ARM_LIB/arm-batch-module"
    "arm-core-document"  = "$SVN_BASE_URL_BW2_LIB/arm-core-document-ispb"
    "arm-core-event"     = "$SVN_BASE_URL_BW2_LIB/arm-core-event"
    "arm-core-perfo"     = "$SVN_BASE_URL_ARM_LIB/arm-perfo-ispb"
    "arm-core-print"     = "$SVN_BASE_URL/arm-core-print/arm-core-print"
    "arm-core-querybuilder" = "$SVN_BASE_URL_ARM_LIB/arm-core-querybuilder"
    "arm-core-quest"     = "$SVN_BASE_URL/arm-core-quest"
    "arm-core-users"     = "$SVN_BASE_URL_ARM_LIB/arm-core-users"
    "arm-core-utility"   = "$SVN_BASE_URL_ARM_LIB/arm-core-utility"
    "arm-core-utility-extra" = "$SVN_BASE_URL_ARM_LIB/arm-core-utility-extra"
    "arm-core-var"       = "$SVN_BASE_URL_ARM_LIB/arm-calcolo-VaR"
    "arm-fileExporter"   = "$SVN_BASE_URL_ARM_LIB/arm-fileExporter"
    "arm-limiti"         = "$SVN_BASE_URL_ARM_LIB/arm-limiti"
    "arm-quadratore-portafogli" = "$SVN_BASE_URL_ARM_LIB/arm-quadratore-portafogli"
    "arm-util"           = "$SVN_BASE_URL_ARM_LIB/arm-util"
    "aspose-manager"     = "$SVN_BASE_URL/arm-core-print/aspose-manager"
    "data-quality"       = "$SVN_BASE_URL_ARM_LIB/data-quality-default"
    "shared-javaEE"      = "$SVN_BASE_URL_ARM_LIB/Shared-JavaEE"
  }

# Create workspace directory if it doesn't exist
if (-not (Test-Path $WORKSPACE_DIR)) {
    New-Item -ItemType Directory -Path $WORKSPACE_DIR -Force
}

# Function to checkout or update a project
function Checkout-Project {
    param (
        [string]$projectName,
        [string]$svnUrl,
        [string]$targetDir
    )

    $projectPath = Join-Path $targetDir $projectName

    if (Test-Path $projectPath) {
        Write-Host "Updating $projectName..." -ForegroundColor Yellow
        Set-Location $projectPath
        svn update
    } else {
        Write-Host "Checking out $projectName..." -ForegroundColor Yellow
        Set-Location $targetDir
        svn checkout $svnUrl $projectName
    }

    if ($LASTEXITCODE -eq 0) {
        Write-Host "$projectName successfully checked out/updated" -ForegroundColor Green
    } else {
        Write-Host "Error checking out/updating $projectName" -ForegroundColor Red
    }
}

# Store the original directory
$originalDirectory = Get-Location

try {
    # Checkout/update each project
    foreach ($project in $PROJECTS.GetEnumerator()) {
        Checkout-Project -projectName $project.Key -svnUrl $project.Value -targetDir $WORKSPACE_DIR
    }

    # Array of projects to build in order
    $BUILD_ORDER = @(
        # Base libraries
        "shared-javaEE",              # Base Java EE utilities
        "arm-core-utility",           # core utilities
        "arm-core-utility-extra",     # additional utilities
        "arm-util",                   # general utilities
        
        # Core functionality
        "arm-core-document",          # Document handling
        "arm-core-event",             # Event system
        "arm-core-users",             # User management
        "arm-core-querybuilder",      # Query building
        "arm-core-print",             # Printing system
        
        # Business modules
        "arm-core-quest",             # Questionnaires
        "arm-core-batch",             # Batch processing
        "arm-core-perfo",             # Performance
        "arm-core-var",               # Risk calculation
        "arm-fileExporter",           # File export
        "arm-limiti",                 # Limits management
        "arm-quadratore-portafogli",  # Portfolio management
        
        # Additional components
        "data-quality",               # Data quality
        "AmazonAWS.S3",              # AWS S3 integration
        "aspose-manager"              # Document processing
    )

    # Build each project in order
    foreach ($project in $BUILD_ORDER) {
        Write-Host "Building $project..." -ForegroundColor Yellow
        Set-Location (Join-Path $WORKSPACE_DIR $project)
        mvn clean install -DskipTests
        
        if ($LASTEXITCODE -eq 0) {
            Write-Host "$project successfully built and installed" -ForegroundColor Green
        } else {
            Write-Host "Error building $project" -ForegroundColor Red
            exit 1
        }
    }

} catch {
    Write-Host "An error occurred: $_" -ForegroundColor Red
} finally {
    # Restore original directory
    Set-Location $originalDirectory
}