socktop/scripts/install-with-man.sh
jasonwitty f82a5903b8
Some checks failed
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
WIP: Man pages generation with clap_mangen
2025-11-20 23:35:29 -08:00

201 lines
5.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install socktop binaries and man pages
# This script builds the binaries, generates man pages, and installs everything
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
echo -e "${BLUE}==>${NC} ${1}"
}
print_success() {
echo -e "${GREEN}${NC} ${1}"
}
print_error() {
echo -e "${RED}${NC} ${1}"
}
print_warning() {
echo -e "${YELLOW}!${NC} ${1}"
}
# Parse arguments
SYSTEM_INSTALL=false
MAN_ONLY=false
while [ $# -gt 0 ]; do
case "$1" in
--system)
SYSTEM_INSTALL=true
shift
;;
--man-only)
MAN_ONLY=true
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Build and install socktop binaries and man pages"
echo ""
echo "Options:"
echo " --system Install system-wide (requires sudo)"
echo " --man-only Only install man pages (skip binary build)"
echo " --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Build and install for current user"
echo " sudo $0 --system # Build and install system-wide"
echo " $0 --man-only # Only install man pages"
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Run '$0 --help' for usage information"
exit 1
;;
esac
done
cd "$PROJECT_ROOT"
# Step 1: Build binaries (unless --man-only)
if [ "$MAN_ONLY" = false ]; then
print_header "Building binaries..."
cargo build --release
print_success "Binaries built successfully"
fi
# Step 2: Extract generated man pages from OUT_DIR
print_header "Extracting generated man pages..."
# Find the build output directory
SOCKTOP_OUT_DIR=$(find target/release/build/socktop-*/out -type d -name "man" 2>/dev/null | head -1)
AGENT_OUT_DIR=$(find target/release/build/socktop_agent-*/out -type d -name "man" 2>/dev/null | head -1)
if [ -z "$SOCKTOP_OUT_DIR" ] || [ -z "$AGENT_OUT_DIR" ]; then
print_error "Generated man pages not found. Building to generate them..."
cargo build --release
SOCKTOP_OUT_DIR=$(find target/release/build/socktop-*/out -type d -name "man" 2>/dev/null | head -1)
AGENT_OUT_DIR=$(find target/release/build/socktop_agent-*/out -type d -name "man" 2>/dev/null | head -1)
fi
if [ -z "$SOCKTOP_OUT_DIR" ] || [ ! -f "$SOCKTOP_OUT_DIR/socktop.1" ]; then
print_error "Failed to find generated socktop.1 man page"
exit 1
fi
if [ -z "$AGENT_OUT_DIR" ] || [ ! -f "$AGENT_OUT_DIR/socktop_agent.1" ]; then
print_error "Failed to find generated socktop_agent.1 man page"
exit 1
fi
print_success "Found generated man pages"
# Step 3: Determine installation directories
if [ "$SYSTEM_INSTALL" = true ]; then
if [ "$EUID" -ne 0 ]; then
print_error "System-wide installation requires root privileges"
echo "Please run with sudo: sudo $0 --system"
exit 1
fi
BIN_DIR="/usr/local/bin"
MAN_DIR="/usr/local/share/man/man1"
else
BIN_DIR="$HOME/.cargo/bin"
MAN_DIR="$HOME/.local/share/man/man1"
fi
# Step 4: Install binaries (unless --man-only)
if [ "$MAN_ONLY" = false ]; then
print_header "Installing binaries to $BIN_DIR..."
if [ "$SYSTEM_INSTALL" = true ]; then
install -m 755 target/release/socktop "$BIN_DIR/socktop"
install -m 755 target/release/socktop_agent "$BIN_DIR/socktop_agent"
else
# For user install, cargo already puts binaries in ~/.cargo/bin
# But we can copy from release if needed
if [ ! -f "$BIN_DIR/socktop" ]; then
cp target/release/socktop "$BIN_DIR/"
chmod 755 "$BIN_DIR/socktop"
fi
if [ ! -f "$BIN_DIR/socktop_agent" ]; then
cp target/release/socktop_agent "$BIN_DIR/"
chmod 755 "$BIN_DIR/socktop_agent"
fi
fi
print_success "Binaries installed to $BIN_DIR"
fi
# Step 5: Install man pages
print_header "Installing man pages to $MAN_DIR..."
mkdir -p "$MAN_DIR"
if [ "$SYSTEM_INSTALL" = true ]; then
install -m 644 "$SOCKTOP_OUT_DIR/socktop.1" "$MAN_DIR/socktop.1"
install -m 644 "$AGENT_OUT_DIR/socktop_agent.1" "$MAN_DIR/socktop_agent.1"
else
cp "$SOCKTOP_OUT_DIR/socktop.1" "$MAN_DIR/socktop.1"
cp "$AGENT_OUT_DIR/socktop_agent.1" "$MAN_DIR/socktop_agent.1"
chmod 644 "$MAN_DIR/socktop.1"
chmod 644 "$MAN_DIR/socktop_agent.1"
fi
print_success "Man pages installed to $MAN_DIR"
# Update man database if available
if [ "$SYSTEM_INSTALL" = true ]; then
if command -v mandb &>/dev/null; then
print_header "Updating man database..."
mandb 2>/dev/null || true
fi
fi
# Final summary
echo ""
print_success "Installation complete!"
echo ""
if [ "$MAN_ONLY" = false ]; then
echo "Binaries installed:"
echo " socktop -> $BIN_DIR/socktop"
echo " socktop_agent -> $BIN_DIR/socktop_agent"
echo ""
fi
echo "Man pages installed:"
echo " socktop(1) -> $MAN_DIR/socktop.1"
echo " socktop_agent(1) -> $MAN_DIR/socktop_agent.1"
echo ""
echo "Try it out:"
if [ "$MAN_ONLY" = false ]; then
echo " socktop --help"
echo " socktop_agent --help"
fi
echo " man socktop"
echo " man socktop_agent"
echo ""
# Check if MANPATH needs updating for user install
if [ "$SYSTEM_INSTALL" = false ]; then
if ! man -w socktop &>/dev/null 2>&1; then
print_warning "If 'man socktop' doesn't work, add to your shell rc file:"
echo " export MANPATH=\"\$HOME/.local/share/man:\$MANPATH\""
fi
fi