Update Script

Purpose: This script was purpose-built for the homelab Minecraft servers in my homelab. It may need to be ported based on your own needs.

clear

# #################################################
# # BUNNY LAB - MINECRAFT UPGRADE SCRIPT          #
# #################################################

# Function to display the banner
function Show-Banner {
    Write-Host "#################################################" -ForegroundColor Cyan
    Write-Host "# BUNNY LAB - MINECRAFT UPGRADE SCRIPT          #" -ForegroundColor Cyan
    Write-Host "#################################################" -ForegroundColor Cyan
}

# Function to get user input for the zip file
function Get-ZipFileName {
    Write-Host "Step 1: Getting the zip file name from user input..." -ForegroundColor Yellow
    Write-Host "Please enter the name of the newest Minecraft ZIP file in the downloads folder (e.g., 'Server-Files-x.xx.zip'): " -ForegroundColor Yellow
    $zipFileName = Read-Host
    $zipFilePath = "C:\Users\nicole.rappe\Downloads\$zipFileName"

    # Check if the zip file exists in the Downloads folder
    Write-Host "Verifying if the specified ZIP file exists at: $zipFilePath" -ForegroundColor Yellow
    if (-not (Test-Path $zipFilePath)) {
        Write-Host "File not found! Please check the file name and try again." -ForegroundColor Red
        exit
    }

    Write-Host "ZIP file found: $zipFilePath" -ForegroundColor Green
    return $zipFilePath
}

# Function to unzip the file without nesting
function Unzip-ServerFiles {
    param (
        [string]$zipFilePath,
        [string]$destinationFolder
    )

    Write-Host "Step 2: Unzipping the server files to: $destinationFolder" -ForegroundColor Yellow

    # Create a temporary folder for extraction
    $tempFolder = "$env:TEMP\MinecraftTemp"
    Write-Host "Creating temporary folder for extraction at: $tempFolder" -ForegroundColor Yellow

    # Remove the temporary folder if it exists, then recreate it
    if (Test-Path $tempFolder) {
        Write-Host "Temporary folder exists. Removing existing folder..." -ForegroundColor Yellow
        Remove-Item -Recurse -Force $tempFolder
    }
    New-Item -ItemType Directory -Path $tempFolder

    Write-Host "Unzipping new server files..." -ForegroundColor Green
    Expand-Archive -Path $zipFilePath -DestinationPath $tempFolder -Force

    Write-Host "Moving unzipped files to destination folder: $destinationFolder" -ForegroundColor Green
    # Move the contents of the temporary folder to the destination
    Get-ChildItem -Path $tempFolder -Recurse | Move-Item -Destination $destinationFolder -Force

    # Clean up the temporary folder
    Write-Host "Cleaning up temporary folder..." -ForegroundColor Yellow
    Remove-Item -Recurse -Force $tempFolder
}

# Function to copy specific files/folders from the old deployment
function Copy-ServerData {
    param (
        [string]$sourceFolder,
        [string]$destinationFolder
    )

    Write-Host "Step 3: Copying server data from: $sourceFolder to: $destinationFolder" -ForegroundColor Yellow

    # Files to copy
    Write-Host "Copying essential files..." -ForegroundColor Yellow
    Copy-Item "$sourceFolder\eula.txt" "$destinationFolder\eula.txt" -Force
    Copy-Item "$sourceFolder\user_jvm_args.txt" "$destinationFolder\user_jvm_args.txt" -Force
    Copy-Item "$sourceFolder\ops.json" "$destinationFolder\ops.json" -Force
    Copy-Item "$sourceFolder\server.properties" "$destinationFolder\server.properties" -Force
#    Copy-Item "$sourceFolder\mods\ftbbackups2-neoforge-1.21-1.0.28.jar" "$destinationFolder\mods\ftbbackups2-neoforge-1.21-1.0.28.jar" -Force
    Copy-Item "$sourceFolder\config\ftbbackups2.json" "$destinationFolder\config\ftbbackups2.json" -Force

    Write-Host "Copying world data and backups folder..." -ForegroundColor Yellow
    # Folder to copy (recursively)
    Copy-Item "$sourceFolder\world" "$destinationFolder\world" -Recurse -Force
#    New-Item -ItemType SymbolicLink -Path "\backups" -Target "Z:\"
}

# Function to rename the old folder with the current date
function Rename-OldServer {
    param (
        [string]$oldFolderPath
    )

    $currentDate = Get-Date -Format "MM-dd-yyyy"
    $backupFolderPath = "$oldFolderPath.backup.$currentDate"

    Write-Host "Step 4: Renaming old server folder to: $backupFolderPath" -ForegroundColor Yellow
    Rename-Item -Path $oldFolderPath -NewName $backupFolderPath
    Write-Host "Old server folder renamed to: $backupFolderPath" -ForegroundColor Green
}

# Function to rename the new deployment to 'ATM10'
function Rename-NewServer {
    param (
        [string]$newDeploymentPath,
        [string]$finalServerPath
    )

    Write-Host "Step 5: Renaming new deployment folder to 'ATM10' at: $finalServerPath" -ForegroundColor Yellow
    Rename-Item -Path $newDeploymentPath -NewName $finalServerPath
    Write-Host "New server folder renamed to 'ATM10' at: $finalServerPath" -ForegroundColor Green
}

# Main Script Logic

# Show banner
Show-Banner

# Variables for folder paths
$oldServerFolder = "C:\Users\nicole.rappe\Desktop\Minecraft_Server\ATM10"
$newDeploymentFolder = "C:\Users\nicole.rappe\Desktop\Minecraft_Server\ATM10_NewDeployment"
$finalServerFolder = "C:\Users\nicole.rappe\Desktop\Minecraft_Server\ATM10"

# Step 1: Get the zip file name from the user
$zipFilePath = Get-ZipFileName

# Step 2: Unzip the file to the new deployment folder without nesting
Unzip-ServerFiles -zipFilePath $zipFilePath -destinationFolder $newDeploymentFolder

# Step 3: Copy necessary files/folders from the old server
Copy-ServerData -sourceFolder $oldServerFolder -destinationFolder $newDeploymentFolder

# Step 4: Rename the old server folder with the current date
Rename-OldServer -oldFolderPath $oldServerFolder

# Step 5: Rename the new deployment folder to 'ATM10'
Rename-NewServer -newDeploymentPath $newDeploymentFolder -finalServerPath $finalServerFolder

# Step 6. Create Symbolic Link to Backup Drive
Write-Host "Step 6: Create Symbolic Link to Backup Drive" -ForegroundColor Cyan
cd "C:\Users\nicole.rappe\Desktop\Minecraft_Server\ATM10"
cmd.exe /c mklink /D backups Z:\

# Step 7: Notify the user that the server is ready to launch
Write-Host "Step 7: Server Ready to Launch!" -ForegroundColor Cyan

Write-Host "Press any key to exit the script"
[System.Console]::ReadKey($true)  # Waits for a key press and doesn't display the pressed key

clear