#!/usr/bin/env php
<?php

include $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

use Jiwei\EasyHttpSdk\Generation\SdkEndpointBuilder;

$params = getopt("d:e:p:v:h", [
    "dist:",
    "endpoints:",
    "package:",
    "version:",
    "help"
]);

if (isset($params['h']) || isset($params['help'])) {
    echo "SDK生成工具🧸ver:0.1\n
-v --version
输出SDK Endpoint 类的PHP语法版本，默认兼容 7.2，目前最高支持 8.1
-d --dist
输出SDK Endpoint 类的目录 默认为 ./src/Endpoint/
-e --endpoints
获取endpoins toml 描述文件的目录 ./endpoints/
-p--package
SDK Endpoint 类所使用的命名空间名称 " . PHP_EOL;
    exit;
}


$packageName = $params['p'] ?? $params['package'] ?? "";
$version = $params['v'] ?? $params['version'] ?? "70000";
$distPath = $params['d'] ?? $params['dist'] ?? $_composer_bin_dir . "/../../src/Endpoint/";
$endpointPath = $params['e'] ?? $params['endpoints'] ?? $_composer_bin_dir . "/../../endpoints/";

if ("" === $packageName) {
    echo "请输入的包名" . PHP_EOL;
    exit;
}

if (substr($distPath, -1) != "/") {
    $distPath .= "/";
}

if (substr($endpointPath, -1) != "/") {
    $endpointPath .= "/";
}

echo "正在创建Jiwei\\{$packageName}\\Endpoint\\" . PHP_EOL;
echo "目标目录{$distPath}" . PHP_EOL;
echo "配置来源{$endpointPath}" . PHP_EOL;

if(substr($version, 0, 1) == "8"){
    $version = 80000;
}

$builder = new SdkEndpointBuilder(
    $distPath,
    $endpointPath,
    __DIR__ . '/../template/',
    $packageName,
    $version
);

$count = $builder->build();

echo "成功生成{$count}个 Endpoint Class" . PHP_EOL;
