$parent = Split-Path -Parent $MyInvocation.MyCommand.Definition;

[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8

$dbapi_home = $parent + "\..\";
$lib_dir = $dbapi_home + "lib\";
$extra_lib_dir = $dbapi_home + "extlib\";
$conf_dir = $dbapi_home + "conf";

if (-Not (Test-Path -Path $extra_lib_dir)) {
    New-Item -Path $extra_lib_dir -ItemType Directory
}

$serviceFile = Get-ChildItem -Path $lib_dir -Filter "dbapi-service*" | Select-Object -First 1
$serviceFilePath = $serviceFile.FullName

$java_library_path=Join-Path $dbapi_home "bin"

$exclude_jars = @("spring-boot-starter-webflux", "spring-webflux", "spring-cloud-gateway-server", "spring-cloud-starter-gateway");

$jarList = Get-ChildItem $lib_dir -Recurse | Select-Object -ExpandProperty FullName

$filteredJarList = $jarList | Where-Object {
    $fileName = [System.IO.Path]::GetFileName($_)
    -not ($exclude_jars | Where-Object { $fileName.StartsWith($_) })
}

$extraJarList = Get-ChildItem $extra_lib_dir -Recurse | Select-Object -ExpandProperty FullName

$joinedJarList = ($filteredJarList + $extraJarList) -join ";"


$command = "java --% -server -Djava.library.path=""$java_library_path"" -XX:+IgnoreUnrecognizedVMOptions -XX:+DisableAttachMechanism -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xss512k -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:+PrintGCDateStamps -javaagent:""$serviceFilePath"" -cp ""$conf_dir;$joinedJarList"" com.gitee.freakchicken.dbapi.CIjAcBwM"

cd $dbapi_home;

iex $command;


Read-Host -Prompt "The above error occurred. Press Enter to exit.";