#!/bin/bash
# NIP Release Build Script
set -e

echo "🚀 Building NIP v0.1.0 for Release"
echo "======================================"
echo ""

# Check if Nim is available
if ! command -v nim &> /dev/null; then
    echo "❌ Error: Nim compiler not found"
    echo "   Please install Nim: https://nim-lang.org/install.html"
    exit 1
fi

# Show Nim version
echo "📋 Nim version: $(nim --version | head -1)"
echo ""

# Clean previous builds
echo "🧹 Cleaning previous builds..."
rm -f nip nip_release
echo ""

# Build optimized release binary
echo "🔨 Building optimized release binary..."
nim c \
    --opt:speed \
    --define:release \
    --define:danger \
    --passC:-flto \
    --passL:-flto \
    --passL:-s \
    --hints:off \
    --warnings:off \
    --out:nip_release \
    nip.nim

if [ ! -f "nip_release" ]; then
    echo "❌ Build failed!"
    exit 1
fi

# Show binary info
echo ""
echo "✅ Build successful!"
echo ""
echo "📊 Binary Information:"
ls -lh nip_release
echo ""

# Test the binary
echo "🧪 Testing binary..."
if ./nip_release --version > /dev/null 2>&1; then
    echo "✅ Binary test passed"
else
    echo "⚠️  Binary test failed (but this is expected without root)"
fi
echo ""

# Create release directory
RELEASE_DIR="nip-v0.1.0-$(uname -s)-$(uname -m)"
echo "📦 Creating release package: $RELEASE_DIR"
mkdir -p "$RELEASE_DIR"

# Copy files to release directory
cp nip_release "$RELEASE_DIR/nip"
cp README.md "$RELEASE_DIR/" 2>/dev/null || echo "# NIP v0.1.0" > "$RELEASE_DIR/README.md"

# Create installation script
cat > "$RELEASE_DIR/install.sh" << 'EOF'
#!/bin/bash
# NIP Installation Script
set -e

echo "🌱 Installing NIP v0.1.0"
echo "============================"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "⚠️  This installer requires root privileges"
    echo "   Please run: sudo ./install.sh"
    exit 1
fi

# Install binary
echo "📦 Installing NIP binary..."
cp nip /usr/local/bin/nip
chmod +x /usr/local/bin/nip
echo "   Installed to: /usr/local/bin/nip"
echo ""

# Create directories
echo "📁 Creating directories..."
mkdir -p /Programs
mkdir -p /System/Links/{Executables,Libraries,Headers,Shared}
mkdir -p /var/nip/{cache,db}
mkdir -p /etc/nip
echo "   Created system directories"
echo ""

# Setup system integration
echo "🔧 Setting up system integration..."
if /usr/local/bin/nip setup; then
    echo "   System integration complete"
else
    echo "   System integration partially complete"
fi
echo ""

echo "✅ NIP installation complete!"
echo ""
echo "🎉 You can now use NIP:"
echo "   nip --help              # Show help"
echo "   nip config init         # Initialize user config"
echo "   nip graft nix:hello     # Graft a package"
echo "   nip status              # Show system status"
echo ""
echo "📚 For more information:"
echo "   https://git.maiwald.work/Nexus/NexusToolKit"
EOF

chmod +x "$RELEASE_DIR/install.sh"

# Create uninstall script
cat > "$RELEASE_DIR/uninstall.sh" << 'EOF'
#!/bin/bash
# NIP Uninstallation Script
set -e

echo "🗑️  Uninstalling NIP"
echo "======================"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "⚠️  This uninstaller requires root privileges"
    echo "   Please run: sudo ./uninstall.sh"
    exit 1
fi

# Remove binary
echo "📦 Removing NIP binary..."
rm -f /usr/local/bin/nip
echo "   Removed: /usr/local/bin/nip"
echo ""

# Ask about data removal
echo "❓ Remove NIP data directories? [y/N]"
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
    echo "🗑️  Removing data directories..."
    rm -rf /Programs
    rm -rf /System/Links
    rm -rf /var/nip
    rm -rf /etc/nip
    rm -f /etc/profile.d/nip.sh
    rm -f /etc/ld.so.conf.d/nip.conf
    echo "   Removed all NIP data"
else
    echo "   Kept NIP data directories"
fi
echo ""

echo "✅ NIP uninstallation complete!"
EOF

chmod +x "$RELEASE_DIR/uninstall.sh"

# Create tarball
echo "📦 Creating release tarball..."
tar -czf "$RELEASE_DIR.tar.gz" "$RELEASE_DIR"
echo "   Created: $RELEASE_DIR.tar.gz"
echo ""

echo "🎉 Release build complete!"
echo ""
echo "📋 Release artifacts:"
echo "   Binary: nip_release"
echo "   Package: $RELEASE_DIR/"
echo "   Tarball: $RELEASE_DIR.tar.gz"
echo ""
echo "🚀 Ready for distribution!"
