#!/bin/bash
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8

# 检查脚本参数
if [ "$#" -ne 4 ]; then
    echo "Usage: $0  <build_type> <source_directory> <build_directory> <install_directory>"
    exit 1
fi


BOOST_SOURCE_DIR=$1
BOOST_BUILD_DIR=$2
BOOST_INSTALL_DIR=$3
BOOST_BUILD_TYPE=$4

BOOST_URL="https://archives.boost.io/release/1.83.0/source/boost_1_83_0.tar.gz"
proxy_URL="http://127.0.0.1:9081"

export http_proxy="$proxy_URL"
export https_proxy="$proxy_URL"
export ftp_proxy="$proxy_URL"


echo "BOOST_SOURCE_DIR $BOOST_SOURCE_DIR"
echo "BOOST_BUILD_DIR $BOOST_BUILD_DIR"
echo "BOOST_INSTALL_DIR $BOOST_INSTALL_DIR"
echo "BOOST_BUILD_TYPE $BOOST_BUILD_TYPE"

# 如果安装目录不存在
if [ ! -d "$BOOST_INSTALL_DIR" ]; then
    if [ ! -d "$BOOST_BUILD_DIR" ]; then
        mkdir -p "$BOOST_BUILD_DIR"
    fi

    # 如果源目录不存在，创建它
    mkdir -p "$BOOST_SOURCE_DIR"
    cd "$BOOST_SOURCE_DIR" || exit

    # 下载Boost源码包
    if [ ! -f "$BOOST_SOURCE_DIR/boost.tar.gz" ]; then
        wget $BOOST_URL -O "$BOOST_SOURCE_DIR/boost.tar.gz"
    fi

    # 解压Boost源码包
    if [ ! -d "$BOOST_SOURCE_DIR/boost" ]; then
        tar --transform 's/^boost_/boost/' -xzf "$BOOST_SOURCE_DIR/boost.tar.gz" -C "$BOOST_SOURCE_DIR"
    fi

    # 执行Bootstrap和构建
    cd "$BOOST_SOURCE_DIR/boost" || exit
    ./bootstrap.sh --prefix="$BOOST_INSTALL_DIR"


    ./b2 variant=$BOOST_BUILD_TYPE  link=shared --build-dir="$BOOST_BUILD_DIR" --prefix="$BOOST_INSTALL_DIR" install
else
    echo "安装目录已存在，跳过安装！"
fi
