# PowerShell script to build and run asset-gui

$projectDir = ".\asset-gui"
$targetDir = ".\arm_am-pom\arm_am\WebContent\adv"

Write-Host "Building asset-gui project..." -ForegroundColor Green

# Check if target directory exists, create if not
if (-not (Test-Path $targetDir)) {
    Write-Host "Creating target directory: $targetDir" -ForegroundColor Yellow
    New-Item -ItemType Directory -Path $targetDir -Force
}

# Change to project directory
Push-Location $projectDir

try {
    # Install dependencies
    Write-Host "Installing Node.js dependencies..." -ForegroundColor Yellow
    npm install

    # Build the project
    Write-Host "Building the project..." -ForegroundColor Yellow
    gulp build --minified

    # Copy release folder to target directory
    Write-Host "Copying release folder to $targetDir..." -ForegroundColor Yellow
    Pop-Location  # Return to original directory to use relative paths correctly
    if (Test-Path ".\asset-gui\release") {
        # Remove existing content in target directory
        if (Test-Path $targetDir) {
            Remove-Item -Path "$targetDir\*" -Recurse -Force
        }
        # Copy new content
        Copy-Item -Path ".\asset-gui\release\*" -Destination $targetDir -Recurse -Force
        Write-Host "Release folder copied successfully!" -ForegroundColor Green
    } else {
        Write-Host "Error: Release folder not found at .\asset-gui\release" -ForegroundColor Red
    }

    # Change back to project directory for serving
    Push-Location $projectDir

    # Start the development server
    Write-Host "Starting development server..." -ForegroundColor Yellow
    gulp serve
} finally {
    # Return to original directory
    Pop-Location
}