#!/bin/bash

# Script to update the UTM mirror site
# This script helps update the index.html file when new UTM images are added to the mirror

# Function to add a new VM to the index.html file
add_vm() {
    local name="$1"
    local arch="$2"
    local filename="$3"
    local size="$4"
    local username="$5"
    local password="$6"
    local root_password="$7"
    local note="$8"
    
    # Create the HTML for the new VM
    local vm_html="<div class=\"gallery-item\">
                    <h3>$name</h3>
                    <h4><i class=\"fa fa-microchip\"></i> $arch</h4>
                    <ul class=\"specs\">
                        <li><i class=\"fa fa-hdd\"></i> Size: $size</li>
                    </ul>
                    <div class=\"credentials\">"
    
    # Add credentials if provided
    if [ -n "$username" ] && [ -n "$password" ]; then
        vm_html="$vm_html
                        <p><i class=\"fa fa-user\"></i> Username: <code>$username</code></p>
                        <p><i class=\"fa fa-key\"></i> Password: <code>$password</code></p>"
        
        if [ -n "$root_password" ]; then
            vm_html="$vm_html
                        <p><i class=\"fa fa-key\"></i> Root password: <code>$root_password</code></p>"
        fi
    elif [ -n "$note" ]; then
        vm_html="$vm_html
                        <p><i class=\"fa fa-info-circle\"></i> $note</p>"
    else
        vm_html="$vm_html
                        <p><i class=\"fa fa-user\"></i> No login information available</p>"
    fi
    
    vm_html="$vm_html
                    </div>
                    <div class=\"button\">
                        <a href=\"https://mirror.bgp.rodeo/utm/$filename\">Download</a>
                    </div>
                </div>"
    
    # Insert the new VM before the closing div of the gallery
    sed -i.bak "/<\/div>.*<div class=\"info-section\">/i \\
                $vm_html
                " index.html
    
    # Remove backup file
    rm -f index.html.bak
    
    echo "Added $name to the gallery"
}

# Function to update the README.md file
update_readme() {
    echo "Updating README.md with current date..."
    sed -i.bak "s/This is a mirror site for UTM virtual machine images, hosted at/This is a mirror site for UTM virtual machine images (last updated: $(date '+%Y-%m-%d')), hosted at/" README.md
    rm -f README.md.bak
}

# Function to update the sitemap.xml file
update_sitemap() {
    echo "Updating sitemap.xml with current date..."
    sed -i.bak "s/<lastmod>.*<\/lastmod>/<lastmod>$(date '+%Y-%m-%d')<\/lastmod>/" sitemap.xml
    rm -f sitemap.xml.bak
}

# Main menu
echo "UTM Mirror Site Updater"
echo "======================="
echo
echo "1. Add a new VM to the gallery"
echo "2. Update README.md and sitemap.xml with current date"
echo "3. Exit"
echo
read -p "Enter your choice: " choice

case $choice in
    1)
        echo
        read -p "Enter VM name (e.g., 'Debian 12'): " name
        read -p "Enter architecture (e.g., 'ARM64'): " arch
        read -p "Enter filename (e.g., 'debian-12-arm64-utm.zip'): " filename
        read -p "Enter size (e.g., '2.5 GiB'): " size
        read -p "Enter username (leave empty if not applicable): " username
        read -p "Enter password (leave empty if not applicable): " password
        read -p "Enter root password (leave empty if not applicable): " root_password
        read -p "Enter special note (optional): " note
        add_vm "$name" "$arch" "$filename" "$size" "$username" "$password" "$root_password" "$note"
        ;;
    2)
        update_readme
        update_sitemap
        echo "Updated README.md and sitemap.xml with current date"
        ;;
    3)
        echo "Exiting..."
        exit 0
        ;;
    *)
        echo "Invalid choice"
        exit 1
        ;;
esac

echo "Done!" 