#!/usr/bin/env bash # 首次环境:装编译依赖、cmake 编译、建库建表并启动 # 用法: ./init.sh [config.ini] set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$ROOT" CONFIG="${1:-config/config.ini}" BUILD_TYPE="${BUILD_TYPE:-Release}" JOBS="$(nproc 2>/dev/null || echo 4)" if [[ ! -f "$CONFIG" ]]; then echo "config not found: $CONFIG" >&2 exit 1 fi APT_PACKAGES=( build-essential cmake libcurl4-openssl-dev libssl-dev libmysqlcppconn-dev zlib1g-dev ) missing=() for pkg in "${APT_PACKAGES[@]}"; do if ! dpkg -s "$pkg" >/dev/null 2>&1; then missing+=("$pkg") fi done if (( ${#missing[@]} > 0 )); then echo "install packages: ${missing[*]}" if [[ "$(id -u)" -eq 0 ]]; then apt-get update apt-get install -y "${missing[@]}" else sudo apt-get update sudo apt-get install -y "${missing[@]}" fi fi if [[ ! -e /usr/local/include/util/json.h && ! -e /usr/local/include/ylib/util/json.h ]]; then echo "ylib headers not found under /usr/local/include" >&2 exit 1 fi if [[ ! -e /usr/local/lib/libylib.a && ! -e /usr/local/lib/libylib.so && ! -e /usr/local/lib/libylib_d.a ]]; then echo "ylib library not found under /usr/local/lib" >&2 exit 1 fi if [[ ! -e /usr/local/lib/libhpsocket.a && ! -e /usr/local/lib/libhpsocket.so ]]; then echo "hpsocket library not found under /usr/local/lib" >&2 exit 1 fi cmake -S . -B build -DCMAKE_BUILD_TYPE="$BUILD_TYPE" cmake --build build -j"$JOBS" if [[ ! -x ./build/server ]]; then echo "build/server not found" >&2 exit 1 fi exec ./build/server --init-db "$CONFIG"