#!/bin/bash

# Script to generate detail pages for all VMs
# This script helps create detail pages for all VMs based on the template

# Function to create a detail page
create_detail_page() {
    local vm_name="$1"
    local vm_file="$2"
    local vm_title="$3"
    local vm_arch="$4"
    local vm_memory="$5"
    local vm_disk="$6"
    local vm_display="$7"
    local vm_size="$8"
    local vm_username="$9"
    local vm_password="${10}"
    local vm_root_password="${11}"
    local vm_screenshot="${12}"
    local vm_notes="${13}"
    
    # Create the file path
    local file_path="vms/${vm_file}.html"
    
    # Copy the template
    cp vm_detail_template.html "$file_path"
    
    # Replace placeholders
    sed -i.bak "s/VM_NAME/$vm_title/g" "$file_path"
    sed -i.bak "s/VM_ARCH/$vm_arch/g" "$file_path"
    sed -i.bak "s/VM_MEMORY/$vm_memory/g" "$file_path"
    sed -i.bak "s/VM_DISK/$vm_disk/g" "$file_path"
    sed -i.bak "s/VM_DISPLAY/$vm_display/g" "$file_path"
    sed -i.bak "s/VM_SIZE/$vm_size/g" "$file_path"
    sed -i.bak "s/VM_USERNAME/$vm_username/g" "$file_path"
    sed -i.bak "s/VM_PASSWORD/$vm_password/g" "$file_path"
    sed -i.bak "s/VM_SCREENSHOT/$vm_screenshot/g" "$file_path"
    
    # Handle root password
    if [ -n "$vm_root_password" ]; then
        sed -i.bak "s|VM_ROOT_PASSWORD|<p><i class=\"fas fa-key\"></i> Root Password: <code>$vm_root_password</code></p>|g" "$file_path"
    else
        sed -i.bak "s|VM_ROOT_PASSWORD||g" "$file_path"
    fi
    
    # Handle notes
    if [ -n "$vm_notes" ]; then
        sed -i.bak "s|VM_NOTES|$vm_notes|g" "$file_path"
    else
        sed -i.bak "s|VM_NOTES|<p>No specific notes for this VM.</p>|g" "$file_path"
    fi
    
    # Set download URL
    sed -i.bak "s|VM_DOWNLOAD_URL|https://mirror.bgp.rodeo/utm/${vm_name}|g" "$file_path"
    
    # Remove backup files
    rm -f "${file_path}.bak"
    
    echo "Created detail page for $vm_title at $file_path"
}

# Main script
echo "Generating VM detail pages..."

# Create detail pages for each VM
# Parameters: vm_name vm_file vm_title vm_arch vm_memory vm_disk vm_display vm_size vm_username vm_password vm_root_password vm_screenshot vm_notes

# Debian and derivatives
create_detail_page "archlinux-arm64-utm4.zip" "archlinux-arm" "ArchLinux ARM" "ARM64" "2 GiB" "32 GiB" "VGA+Console" "532.0 MiB" "root" "root" "" "archlinux-logo.png" "<ul><li>This is a minimal installation of ArchLinux ARM.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

create_detail_page "debian-10.4-i3-arm64-utm.zip" "debian-10-4-i3" "Debian 10.4 (Custom i3)" "ARM64" "1024 MiB" "20 GiB" "VGA+Console" "592.4 MiB" "debian" "debian" "" "debian-10.4-i3-arm64.png" "<ul><li>This is a custom installation of Debian 10.4 (Buster) with the i3 window manager.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

create_detail_page "debian-11.5-ldxe-arm64-utm4.zip" "debian-11-ldxe" "Debian 11 (LXDE)" "ARM64" "1024 MiB" "20 GiB" "VGA+Console" "1023.5 MiB" "debian" "debian" "" "debian-11-ldxe-arm64.png" "<ul><li>This is a lightweight installation of Debian 11 (Bullseye) with the LXDE desktop environment.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li><li>Due to a bug, auto resolution does not work. Run <code>xrandr --output Virtual-1 --auto</code> to resize.</li></ul>"

create_detail_page "debian-11.5-xfce-arm64-utm4.zip" "debian-11-xfce" "Debian 11 (Xfce)" "ARM64" "1024 MiB" "20 GiB" "VGA+Console" "882.8 MiB" "debian" "debian" "" "debian-11-xfce-arm64.png" "<ul><li>This is a lightweight installation of Debian 11 (Bullseye) with the Xfce desktop environment.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

create_detail_page "debian-12-arm64-utm.zip" "debian-12" "Debian 12" "ARM64" "4 GiB" "64 GiB" "GPU Accelerated" "2.5 GiB" "debian" "debian" "" "debian-12-arm64.png" "<ul><li>This is a clean installation of Debian 12 (Bookworm) with GPU acceleration enabled.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

create_detail_page "debian-12-rosetta-arm64-utm.zip" "debian-12-rosetta" "Debian 12 (Rosetta)" "ARM64" "4 GiB" "64 GiB" "GPU Accelerated" "2.5 GiB" "debian" "debian" "" "debian-12-arm64.png" "<ul><li>This is a Debian 12 installation with Rosetta translation enabled for running x86 applications.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

# Fedora and derivatives
create_detail_page "fedora-38-arm64-utm.zip" "fedora-38" "Fedora 38" "ARM64" "4 GiB" "64 GiB" "GPU Accelerated" "3.2 GiB" "fedora" "fedora" "" "fedora-38-arm64.png" "<ul><li>This is a clean installation of Fedora 38 with GPU acceleration enabled.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

# Security distributions
create_detail_page "kali-linux-2023-arm64-utm.zip" "kali-2023" "Kali Linux" "ARM64" "4 GiB" "80 GiB" "GPU Accelerated" "4.6 GiB" "kali" "kali" "" "kali-2023-arm64.png" "<ul><li>This is a Kali Linux installation with security tools pre-installed.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

# Classic OS
create_detail_page "mac-os-9.2.1-utm.zip" "mac-os-9" "Mac OS 9.2.1" "PowerPC" "512 MiB" "4 GiB" "VGA" "134.9 MiB" "" "" "" "placeholder.png" "<ul><li>This is a classic Mac OS 9.2.1 installation.</li><li>No login is required to use this system.</li><li>Some applications may not work correctly due to emulation limitations.</li></ul>"

# Windows-like OS
create_detail_page "reactos-0.4.14-x64-utm.zip" "reactos-0-4-14" "ReactOS 0.4.14" "x64" "1 GiB" "8 GiB" "VGA" "317.6 MiB" "Administrator" "" "" "reactos-0.4.14-x64.png" "<ul><li>ReactOS is an open-source operating system compatible with Windows applications and drivers.</li><li>No password is required for the Administrator account.</li><li>This is an experimental OS and may have stability issues.</li></ul>"

# Unix-like OS
create_detail_page "sun-solaris-9-utm.zip" "sun-solaris-9" "Sun Solaris 9" "SPARC" "1 GiB" "8 GiB" "VGA" "436.3 MiB" "root" "root" "" "sun-solaris-9-sparc.png" "<ul><li>This is a classic Sun Solaris 9 installation for SPARC architecture.</li><li>Login with username <code>root</code> and password <code>root</code>.</li><li>This is a historical OS primarily for educational purposes.</li></ul>"

# Ubuntu
create_detail_page "ubuntu-14.04-x64-utm.zip" "ubuntu-14-04" "Ubuntu 14.04" "x64" "2 GiB" "20 GiB" "VGA" "1.9 GiB" "ubuntu" "ubuntu" "" "ubuntu-14.04-x64.png" "<ul><li>This is an Ubuntu 14.04 LTS installation with Unity desktop.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

create_detail_page "ubuntu-20.04-arm64-utm.zip" "ubuntu-20-04" "Ubuntu 20.04" "ARM64" "4 GiB" "64 GiB" "GPU Accelerated" "3.6 GiB" "ubuntu" "ubuntu" "" "ubuntu-20.04-arm64.png" "<ul><li>This is an Ubuntu 20.04 LTS installation with GNOME desktop.</li><li>SPICE tools are pre-installed for better integration with UTM.</li><li>For security reasons, it's recommended to change the default passwords after first login.</li></ul>"

# Windows (placeholder files)
create_detail_page "windows-7-x64-utm.zip" "windows-7" "Windows 7" "x64" "4 GiB" "64 GiB" "VGA" "1.7 KiB" "" "" "" "windows-7-x64.png" "<ul><li>This is a placeholder file for Windows 7.</li><li>You need to provide your own Windows 7 ISO.</li><li>Follow the instructions in the included README file to set up Windows 7.</li></ul>"

create_detail_page "windows-xp-x64-utm.zip" "windows-xp" "Windows XP" "x64" "2 GiB" "32 GiB" "VGA" "1.7 KiB" "" "" "" "windows-xp-x86.png" "<ul><li>This is a placeholder file for Windows XP.</li><li>You need to provide your own Windows XP ISO.</li><li>Follow the instructions in the included README file to set up Windows XP.</li></ul>"

echo "Detail page generation complete!" 