$PSConsoleOutputEncoding
$OutputEncoding = [System.Text.Encoding]::UTF8
$PSConsoleOutputEncoding = [System.Text.Encoding]::UTF8


#!/usr/bin/env pwsh

# 设置环境变量
$env:LANG = "en_US.UTF-8"
$env:LC_ALL = "en_US.UTF-8"

$BOOST_SOURCE_DIR = $args[0]
$BOOST_BUILD_DIR = $args[1]
$BOOST_INSTALL_DIR = $args[2]
$version=$args[3]
$other_option=$args[4]

$formattedVersion = $version -replace "\.", "_"
# $version = "1.83.0"
$BOOST_URL = "https://archives.boost.io/release/$version/source/boost_$formattedVersion.tar.gz"
$proxy_URL = "http://127.0.0.1:9081"
$name = "boost_$formattedVersion"


# 设置代理
$env:http_proxy = $proxy_URL
$env:https_proxy = $proxy_URL
$env: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 "version $version"
echo "other_option $other_option"


if (-not (Test-Path $BOOST_INSTALL_DIR)) {
    if (-not (Test-Path $BOOST_BUILD_DIR)) {
        mkdir -p $BOOST_BUILD_DIR
    }
    if (-not (Test-Path $BOOST_SOURCE_DIR)) {
        mkdir -p $BOOST_SOURCE_DIR
    }
    Set-Location -Path $BOOST_SOURCE_DIR

    # 下载Boost源码包
    if (-not (Test-Path "$BOOST_SOURCE_DIR\boost.tar.gz")) {
        Invoke-WebRequest -Uri $BOOST_URL -OutFile "$BOOST_SOURCE_DIR\boost.tar.gz"
    }

    # 解压Boost源码包
    if (-not (Test-Path "$BOOST_SOURCE_DIR\$name")) {
        echo "tar..."
        tar -xzf "$BOOST_SOURCE_DIR\boost.tar.gz" -C "$BOOST_SOURCE_DIR"
    }
    Set-Location -Path "$BOOST_SOURCE_DIR\$name"
    $bootstrap_cmd = "./bootstrap.bat --prefix=`"$BOOST_INSTALL_DIR`""
    echo "Invoke-Expression bootstrap_cmd: $bootstrap_cmd"
    Invoke-Expression $bootstrap_cmd
    $b2_cmd = "./b2 $other_option --build-dir=`"$BOOST_BUILD_DIR`" --prefix=`"$BOOST_INSTALL_DIR`" install"
    echo "Invoke-Expression b2_cmd: $b2_cmd"
    Invoke-Expression $b2_cmd
} else {
    echo "intall dir already exisit skip all!"

    exit 0
}


exit 0
