#!/bin/bash
# Helper script to install packages on r630-01 remotely
# Usage: ./run-package-install-remote.sh [password]

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_SCRIPT="$SCRIPT_DIR/install-r630-packages.sh"
PROXMOX_HOST="192.168.11.11"
PROXMOX_USER="root"

# Get password from argument or environment
if [ -n "$1" ]; then
    PASSWORD="$1"
elif [ -n "$PROXMOX_PASSWORD" ]; then
    PASSWORD="$PROXMOX_PASSWORD"
fi

# Check if sshpass is installed
if ! command -v sshpass &>/dev/null; then
    echo "Error: sshpass not installed. Install with: sudo apt-get install sshpass"
    exit 1
fi

if [ -z "$PASSWORD" ]; then
    echo "Usage: $0 <password>"
    echo "Or: PROXMOX_PASSWORD=yourpass $0"
    exit 1
fi

echo "=========================================="
echo "Installing Packages on R630-01"
echo "=========================================="
echo ""

# Copy script to remote server
echo "Copying installation script to $PROXMOX_USER@$PROXMOX_HOST..."
sshpass -p "$PASSWORD" scp -o StrictHostKeyChecking=no "$INSTALL_SCRIPT" "$PROXMOX_USER@$PROXMOX_HOST:/tmp/install-r630-packages.sh" || {
    echo "Error: Failed to copy script"
    exit 1
}

# Make it executable
sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no "$PROXMOX_USER@$PROXMOX_HOST" "chmod +x /tmp/install-r630-packages.sh"

# Run the script
echo ""
echo "Running installation script..."
echo ""

sshpass -p "$PASSWORD" ssh -o StrictHostKeyChecking=no -t "$PROXMOX_USER@$PROXMOX_HOST" "/tmp/install-r630-packages.sh"

echo ""
echo "=========================================="
echo "Package installation complete!"
echo "=========================================="

