#!/bin/bash
# Set password for VMID 5000 without using Web UI console
# This script works even if console UI is not accessible

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"
VMID=5000
PASSWORD="L@kers2010"

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

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }

echo "════════════════════════════════════════"
echo "Set Password Without Console"
echo "════════════════════════════════════════"
echo ""

# Find container node
log_info "Finding container location..."
CONTAINER_NODE=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "for node in ml110 pve pve2; do \
        if pvesh get /nodes/\$node/lxc/$VMID/status/current 2>/dev/null | grep -q status; then \
            echo \$node; break; \
        fi; \
    done" 2>/dev/null || echo "")

if [ -z "$CONTAINER_NODE" ]; then
    log_warn "Container not found. Exiting."
    exit 1
fi

log_success "Container found on node: $CONTAINER_NODE"
echo ""

# Check container status
log_info "Checking container status..."
STATUS=$(ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "pvesh get /nodes/$CONTAINER_NODE/lxc/$VMID/status/current --output-format json-pretty 2>/dev/null | grep -o '\"status\":\"[^\"]*\"' | cut -d'\"' -f4" || echo "unknown")

if [ "$STATUS" != "running" ]; then
    log_warn "Container is not running (status: $STATUS)"
    log_info "Starting container..."
    ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
        "pvesh create /nodes/$CONTAINER_NODE/lxc/$VMID/status/start 2>/dev/null" && \
    log_info "Waiting 10 seconds for container to start..." && \
    sleep 10
else
    log_success "Container is running"
fi
echo ""

# Set password using chpasswd
log_info "Setting root password via command line (no console needed)..."
if ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "ssh $CONTAINER_NODE 'pct exec $VMID -- bash -c \"echo root:$PASSWORD | chpasswd\"' 2>&1"; then
    log_success "Password set successfully!"
else
    log_warn "Command may have failed. Trying alternative method..."
    
    # Try via Proxmox API exec (if available)
    log_info "Attempting alternative method..."
    ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
        "pvesh create /nodes/$CONTAINER_NODE/lxc/$VMID/exec --command 'bash' --arg '-c' --arg 'echo root:$PASSWORD | chpasswd' 2>&1" || \
    log_warn "Please set password manually via pct enter method"
fi
echo ""

# Verify
log_info "Verifying password was set..."
if ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" \
    "ssh $CONTAINER_NODE 'pct exec $VMID -- bash -c \"su - root -c echo Password check\"' 2>&1" | grep -q "Password check\|root"; then
    log_success "Password verification successful"
else
    log_warn "Verification inconclusive. Password should be set, but please verify manually."
fi
echo ""

echo "════════════════════════════════════════"
echo "Password Setting Complete"
echo "════════════════════════════════════════"
echo ""
echo "You can now SSH to the container:"
echo "  ssh root@192.168.11.140"
echo "  Password: $PASSWORD"
echo ""
echo "If SSH doesn't work, try:"
echo "  ssh root@$PROXMOX_HOST"
echo "  ssh $CONTAINER_NODE"
echo "  pct enter $VMID"
echo "  # Then set password: passwd root"
echo ""

