# Script to handle network drive mapping and file copying
# Author: Nathan
# Date: 2025-04-23

# Function to check if a drive is mapped
function Test-DriveMapped {
    param (
        [string]$DriveLetter
    )
    $drive = Get-PSDrive -Name $DriveLetter -ErrorAction SilentlyContinue
    return $null -ne $drive
}

# Function to remove a mapped drive
function Remove-MappedDrive {
    param (
        [string]$DriveLetter
    )
    if (Test-DriveMapped -DriveLetter $DriveLetter) {
        Write-Host "Removing existing $DriveLetter drive mapping..."
        Remove-PSDrive -Name $DriveLetter -Force
    }
}

# Function to map a network drive
function Map-NetworkDrive {
    param (
        [string]$DriveLetter,
        [string]$NetworkPath
    )
    Write-Host "Mapping $DriveLetter to $NetworkPath..."
    New-PSDrive -Name $DriveLetter -PSProvider FileSystem -Root $NetworkPath -Persist
}

# Function to copy files using robocopy
function Copy-FilesWithRobocopy {
    param (
        [string]$SourcePath,
        [string]$DestinationPath
    )
    Write-Host "Copying files from $SourcePath to $DestinationPath..."
    robocopy $SourcePath $DestinationPath /E /MOVE
}

# Main script execution
try {
    Write-Host "Checking current network connections..."
    net use

    Write-Host "`nDisconnecting from NEXUS shares..."
    # First disconnect from specific NEXUS shares
    net use \\NEXUS\amazon /delete /y 2>$null
    net use \\NEXUS\flamenco /delete /y 2>$null
    # Then disconnect any mapped drives
    net use A: /delete /y 2>$null
    net use F: /delete /y 2>$null
    Write-Host "Existing NEXUS connections cleared."

    Write-Host "`nMapping network drives..."
    Write-Host "Connecting drives to NEXUS..."

    # First connect to the share without a drive letter
    Write-Host "Establishing initial NEXUS connection..."
    net use \\NEXUS\amazon /user:Nathan /persistent:yes
    if ($LASTEXITCODE -eq 0) {
        Write-Host "Initial NEXUS connection established."
        Start-Sleep -Seconds 2

        Write-Host "`nMapping A: drive..."
        net use A: \\NEXUS\amazon /persistent:yes
        if ($LASTEXITCODE -eq 0) {
            Write-Host "A: drive connected successfully."
            
            Write-Host "`nMapping F: drive..."
            net use F: \\NEXUS\flamenco /persistent:yes
            if ($LASTEXITCODE -eq 0) {
                Write-Host "F: drive connected successfully."
                
                # Copy files from C:\nexus\amazon to F:\renders
                Copy-FilesWithRobocopy -SourcePath "C:\nexus\amazon" -DestinationPath "F:\renders"
                
                Write-Host "Operation completed successfully!"
            } else {
                Write-Host "Failed to connect F: drive" -ForegroundColor Red
            }
        } else {
            Write-Host "Failed to connect A: drive" -ForegroundColor Red
        }
    } else {
        Write-Host "Failed to establish initial NEXUS connection" -ForegroundColor Red
    }

    Write-Host "`nVerifying connections..."
    net use

} catch {
    Write-Error "An error occurred: $_"
    exit 1
} 