#!/bin/bash

# Test Container VM script for Debian Atomic Desktop Bootc Installer
# This runs our installer container in a VM-like environment with VNC

set -e

# Configuration
CONTAINER_NAME="debian-atomic-installer-vm"
VNC_PORT="5901"
VNC_DISPLAY=":1"

# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

# Check if installer image exists
check_installer() {
    log_info "Checking installer image..."
    if ! podman image exists debian-atomic-installer:latest; then
        log_warning "Installer image not found. Building it first..."
        just build-installer
    fi
    log_success "Installer image ready."
}

# Start the installer container with VNC
start_container_vm() {
    log_info "Starting installer container with VNC access..."
    log_info "VNC server will be available at: vnc://localhost:$VNC_PORT"
    log_info "Use a VNC client to connect to: localhost:$VNC_PORT"
    
    # Stop any existing container
    podman stop "$CONTAINER_NAME" 2>/dev/null || true
    podman rm "$CONTAINER_NAME" 2>/dev/null || true
    
    # Start the container with systemd and VNC
    podman run -d \
        --name "$CONTAINER_NAME" \
        --privileged \
        --systemd=always \
        -v /sys/fs/cgroup:/sys/fs/cgroup:rw \
        -p "$VNC_PORT:5900" \
        -p 2222:22 \
        -e DISPLAY="$VNC_DISPLAY" \
        debian-atomic-installer:latest
    
    log_success "Container started. Connect to VNC at localhost:$VNC_PORT"
    log_info "To access the container shell: podman exec -it $CONTAINER_NAME bash"
    log_info "To stop the container: podman stop $CONTAINER_NAME"
}

# Install VNC server in the container
setup_vnc() {
    log_info "Setting up VNC server in the container..."
    
    podman exec "$CONTAINER_NAME" bash -c "
        # Install VNC server and desktop environment
        apt-get update && apt-get install -y \
            tightvncserver \
            xfce4 \
            xfce4-goodies \
            dbus-x11 \
            && rm -rf /var/lib/apt/lists/*
        
        # Create VNC password
        mkdir -p /root/.vnc
        echo 'password' | vncpasswd -f > /root/.vnc/passwd
        chmod 600 /root/.vnc/passwd
        
        # Create VNC startup script
        cat > /root/.vnc/xstartup << 'EOF'
        #!/bin/bash
        xrdb \$HOME/.Xresources
        startxfce4 &
        EOF
        chmod +x /root/.vnc/xstartup
        
        # Start VNC server
        vncserver :1 -geometry 1024x768 -depth 24
    "
    
    log_success "VNC server setup completed"
}

# Show container status
show_status() {
    log_info "Container status:"
    podman ps -a --filter name="$CONTAINER_NAME"
    
    echo
    log_info "VNC Access:"
    echo "  Connect to: vnc://localhost:$VNC_PORT"
    echo "  Password: password"
    echo
    log_info "SSH Access:"
    echo "  Connect to: ssh installer@localhost -p 2222"
    echo "  Password: installer"
    echo
    log_info "Container Shell:"
    echo "  podman exec -it $CONTAINER_NAME bash"
}

# Stop the container
stop_container() {
    log_info "Stopping installer container..."
    podman stop "$CONTAINER_NAME" 2>/dev/null || true
    podman rm "$CONTAINER_NAME" 2>/dev/null || true
    log_success "Container stopped and removed"
}

# Show help
show_help() {
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  start       - Start installer container with VNC"
    echo "  setup-vnc   - Setup VNC server in the container"
    echo "  status      - Show container status and access info"
    echo "  stop        - Stop and remove the container"
    echo "  test        - Full test (start container, setup VNC)"
    echo "  shell       - Access container shell"
    echo "  help        - Show this help"
    echo ""
    echo "VNC Access:"
    echo "  After starting, connect to: vnc://localhost:$VNC_PORT"
    echo "  Password: password"
    echo ""
    echo "SSH Access:"
    echo "  ssh installer@localhost -p 2222"
    echo "  Password: installer"
}

# Main execution
case "${1:-help}" in
    "start")
        check_installer
        start_container_vm
        ;;
    "setup-vnc")
        setup_vnc
        ;;
    "status")
        show_status
        ;;
    "stop")
        stop_container
        ;;
    "test")
        check_installer
        start_container_vm
        sleep 5
        setup_vnc
        show_status
        ;;
    "shell")
        podman exec -it "$CONTAINER_NAME" bash
        ;;
    "help"|*)
        show_help
        ;;
esac 