#!/bin/bash

set -e

INSTALL_DIR="/usr/local/bin"
BINARY_NAME="pib"
REPO_URL="https://github.com/yourusername/proxmox-cloud-image"

echo "=========================================="
echo "Proxmox Cloud Image Tool Installer"
echo "=========================================="
echo ""

if [ "$EUID" -ne 0 ]; then 
    echo "Error: This script must be run as root (use sudo)"
    exit 1
fi

echo "Checking dependencies..."
if ! command -v qemu-img &> /dev/null; then
    echo "Installing qemu-utils..."
    apt-get update
    apt-get install -y qemu-utils
fi

if ! command -v virt-customize &> /dev/null; then
    echo "Installing libguestfs-tools..."
    apt-get install -y libguestfs-tools
fi

if ! command -v go &> /dev/null; then
    echo "Error: Go is not installed. Please install Go 1.19+ first."
    echo "Visit: https://golang.org/doc/install"
    exit 1
fi

echo ""
echo "Building binary..."
go build -o $BINARY_NAME

echo "Installing to $INSTALL_DIR..."
cp $BINARY_NAME $INSTALL_DIR/
chmod +x $INSTALL_DIR/$BINARY_NAME

echo ""
echo "=========================================="
echo "Installation completed successfully!"
echo "=========================================="
echo ""
echo "You can now use: $BINARY_NAME"
echo ""
echo "Examples:"
echo "  $BINARY_NAME -h"
echo "  $BINARY_NAME -config config.yaml"
echo "  $BINARY_NAME -list-storage -proxmox-host 192.168.1.100"
echo ""
