#!/bin/bash
# Hermes Node Protocol
# Copyright (c) 2026 Stefy (nextime) Lanza <stefy@nexlab.net>
# All rights reserved.
# 
# This software is released under the MIT License with a copyleft clause.
# See the LICENSE file for full terms.
set -e

echo "=== Building Linux Distribution ==="

# Create dist directory
mkdir -p dist

# Create temporary build directory
BUILD_DIR=$(mktemp -d)
PACKAGE_DIR="$BUILD_DIR/hermes-node-agent"
mkdir -p "$PACKAGE_DIR"

echo "Copying files to $PACKAGE_DIR..."

# Copy core files
cp hermes_node_agent.py "$PACKAGE_DIR/"
cp browser_controller.py "$PACKAGE_DIR/"
cp requirements.txt "$PACKAGE_DIR/"
cp install.sh "$PACKAGE_DIR/"
cp hermes-node-agent.init.d "$PACKAGE_DIR/"
cp hermes-node-agent.service "$PACKAGE_DIR/"

# Copy documentation
cp README.md "$PACKAGE_DIR/"
cp LICENSE "$PACKAGE_DIR/"
cp DEPLOYMENT.md "$PACKAGE_DIR/"
cp PROTOCOL.md "$PACKAGE_DIR/"
cp BROWSER_PROTOCOL.md "$PACKAGE_DIR/"

# Create tarball
cd "$BUILD_DIR"
tar czf hermes-node-agent-linux.tar.gz hermes-node-agent/
mv hermes-node-agent-linux.tar.gz "$OLDPWD/dist/"
cd "$OLDPWD"

# Cleanup
rm -rf "$BUILD_DIR"

echo "✅ Linux package created: dist/hermes-node-agent-linux.tar.gz"
echo ""
echo "To deploy:"
echo "  scp dist/hermes-node-agent-linux.tar.gz user@node:~/"
echo "  ssh user@node"
echo "  tar xzf hermes-node-agent-linux.tar.gz"
echo "  cd hermes-node-agent"
echo "  sudo ./install.sh"
