#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CRATE_MANIFEST="$ROOT_DIR/traffic_core/Cargo.toml" TARGET_BIN="$ROOT_DIR/traffic_core/target/release/awg2-harness" MIN_RUSTC="1.85.0" AUTO_INSTALL=false SKIP_FMT=false for arg in "$@"; do case "$arg" in --install) AUTO_INSTALL=true ;; --skip-fmt) SKIP_FMT=true ;; *) echo "Unknown arg: $arg"; exit 1 ;; esac done require_cmd() { command -v "$1" >/dev/null 2>&1 || { echo "Missing command: $1"; exit 1; } } version_ge() { local have="$1" local need="$2" [[ "$(printf '%s\n' "$need" "$have" | sort -V | head -n1)" == "$need" ]] } if [[ -f /etc/os-release ]]; then # shellcheck disable=SC1091 source /etc/os-release case "${ID:-}" in debian|ubuntu) ;; *) echo "Unsupported OS: ${ID:-unknown}. Only Debian/Ubuntu are supported."; exit 1 ;; esac fi ARCH="$(uname -m)" if [[ "$ARCH" != "x86_64" && "$ARCH" != "amd64" ]]; then echo "Unsupported arch: $ARCH. Only amd64/x86_64 is supported." exit 1 fi if ! command -v cargo >/dev/null 2>&1; then echo "cargo not found. Install rustup first: https://rustup.rs" exit 1 fi require_cmd rustc RUSTC_VERSION="$(rustc --version | awk '{print $2}')" if ! version_ge "$RUSTC_VERSION" "$MIN_RUSTC"; then echo "rustc $RUSTC_VERSION is too old. Require >= $MIN_RUSTC" exit 1 fi for cmd in gcc pkg-config; do require_cmd "$cmd" done if [[ "$SKIP_FMT" != true ]]; then cargo fmt --manifest-path "$CRATE_MANIFEST" --all --check fi cargo clippy --locked --manifest-path "$CRATE_MANIFEST" --all-targets -- -D warnings cargo test --locked --manifest-path "$CRATE_MANIFEST" --release cargo build --locked --manifest-path "$CRATE_MANIFEST" --release require_cmd file require_cmd ldd file "$TARGET_BIN" ldd "$TARGET_BIN" || true "$TARGET_BIN" --help >/dev/null sha256sum "$TARGET_BIN" ls -lh "$TARGET_BIN" if [[ "$AUTO_INSTALL" == true ]]; then require_cmd install RUN_AS_ROOT=( ) if [[ "$(id -u)" -eq 0 ]]; then RUN_AS_ROOT=( ) else require_cmd sudo RUN_AS_ROOT=(sudo) fi "${RUN_AS_ROOT[@]}" install -m 0755 "$TARGET_BIN" /usr/local/bin/awg2-harness "${RUN_AS_ROOT[@]}" mkdir -p /etc/awg2-harness if [[ ! -f /etc/awg2-harness/config.toml ]]; then echo "Missing /etc/awg2-harness/config.toml" echo "Refusing to install example config as production config." echo "Create config manually from $ROOT_DIR/config.example.toml and re-run with --install." exit 1 fi "${RUN_AS_ROOT[@]}" install -m 0644 "$ROOT_DIR/awg2-harness.service" /etc/systemd/system/awg2-harness.service if command -v systemd-analyze >/dev/null 2>&1; then "${RUN_AS_ROOT[@]}" systemd-analyze verify /etc/systemd/system/awg2-harness.service fi "${RUN_AS_ROOT[@]}" systemctl daemon-reload fi echo "Build completed: $TARGET_BIN"