#!/usr/bin/env bash
# Installation script for Hyperledger Fabric in LXC container
# Fabric is a permissioned blockchain framework

set -euo pipefail

source /dev/stdin <<<"$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func 2>/dev/null || echo '')"

FABRIC_VERSION="${FABRIC_VERSION:-2.5}"
FABRIC_CA_VERSION="${FABRIC_CA_VERSION:-1.5}"
FABRIC_USER="${FABRIC_USER:-fabric}"
FABRIC_GROUP="${FABRIC_GROUP:-fabric}"
FABRIC_HOME="/opt/fabric"
FABRIC_DATA="/var/lib/fabric"

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

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

log_info "Updating system packages..."
export LC_ALL=C
export LANG=C
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get upgrade -y -qq
apt-get install -y -qq \
    docker.io \
    docker-compose \
    curl \
    wget \
    jq \
    ca-certificates \
    gnupg \
    lsb-release \
    git \
    make \
    gcc \
    g++ \
    python3 \
    python3-pip

log_success "System packages updated"

# Install Docker Compose v2
log_info "Installing Docker Compose..."
apt-get install -y -qq docker-compose-plugin
systemctl enable docker
systemctl start docker

log_info "Creating fabric user..."
if ! id -u "$FABRIC_USER" &>/dev/null; then
    useradd -r -s /bin/bash -d "$FABRIC_HOME" -m "$FABRIC_USER"
    usermod -aG docker "$FABRIC_USER"
    log_success "Created fabric user"
else
    log_info "Fabric user already exists"
    usermod -aG docker "$FABRIC_USER"
fi

# Install Fabric binaries
log_info "Installing Hyperledger Fabric binaries..."
cd /tmp
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh | bash -s -- binary

# Move binaries to system location
mkdir -p /usr/local/bin/fabric
mv fabric-samples/bin/* /usr/local/bin/fabric/
chmod +x /usr/local/bin/fabric/*
ln -sf /usr/local/bin/fabric/* /usr/local/bin/

log_success "Fabric binaries installed"

# Create directories
log_info "Creating directories..."
mkdir -p "$FABRIC_HOME" "$FABRIC_DATA"
chown -R "$FABRIC_USER:$FABRIC_GROUP" "$FABRIC_HOME" "$FABRIC_DATA"

# Download Fabric samples (optional, for reference)
log_info "Setting up Fabric environment..."
cd "$FABRIC_HOME"

# Create configuration directory
mkdir -p config network scripts

log_info "Creating basic network configuration..."
cat > config/configtx.yaml.template <<'EOF'
# Configtx template for Fabric network
# This is a template - customize for your network
Organizations:
  - &OrdererOrg
    Name: OrdererOrg
    ID: OrdererMSP
    MSPDir: crypto-config/ordererOrganizations/example.com/msp

  - &Org1
    Name: Org1MSP
    ID: Org1MSP
    MSPDir: crypto-config/peerOrganizations/org1.example.com/msp

Profiles:
  TestNetwork:
    Orderer:
      <<: *OrdererOrg
    Consortiums:
      SampleConsortium:
        Organizations:
          - <<: *Org1
EOF

chown -R "$FABRIC_USER:$FABRIC_GROUP" "$FABRIC_HOME"

log_success "Fabric installation completed!"
log_info "Next steps:"
log_info "1. Configure Fabric network in $FABRIC_HOME/config/"
log_info "2. Generate crypto materials using cryptogen or CA"
log_info "3. Create channel configuration"
log_info "4. Start Fabric network using docker-compose"

