#!/bin/bash
###
# @Author       : lqm283
# @Date         : 2024-08-26 08:41:42
 # @LastEditTime : 2025-03-17 16:46:00
 # @LastEditors  : lqm283
# --------------------------------------------------------------------------------<
# @Description  : Mansion_T1G注射泵的工程初始化脚本
# --------------------------------------------------------------------------------<
###
ROOT=$(pwd)
script_file=$0
local_server=192.168.10.10
cloud_server=script.veryark.cn

url=ssh://git@gitlab.veryark.cn:3399/developer/pump/mansion_t1g/manifest.git
version=main
manifest=develop

ui_sdk_version="6.1-mickledore"
server_sdk_version="go1.24.1"

if [ -n "$1" ]; then
    manifest=$1
fi

if [ -n "$2" ] && [ "$manifest" == "release" ]; then
    version=$2
fi

function __exit() {
    cd $ROOT
    rm $ROOT/$script_file
}

function check_ip_status() {
    ping -c 3 -i 0.2 -W 3 $1 &>/dev/null
    if [ $? -eq 0 ]; then
        return 0
    else
        return -1
    fi
}

function install_front_ui_sdk() {
    echo "install front ui sdk"
    local sdk_install_path='~/.local/bin'
    local sdk_source="environment-setup-armv8a-poky-linux"
    local sdk_file_name="mansion_t1g-frontui-toolchain-${ui_sdk_version}.sh"
    local sdk_local_url="http://$local_server/${sdk_file_name}"
    local sdk_remote_url="https://$veryark_server/sdk/${sdk_file_name}"

    # 检查本地是否有SDK，如果有则跳过SDK的安装
    local expanded_path=$(eval echo ${sdk_install_path}/${ui_sdk_version}/${sdk_source})
    if [ -f "$expanded_path" ]; then
        echo "Find local SDK: $ui_sdk_version at $expanded_path"
        return
    fi

    # 下载SDK
    echo "download front ui sdk"
    local url=""
    check_ip_status $local_server
    if [ $? == 0 ]; then
        url=$sdk_local_url
    else
        url=$sdk_remote_url
    fi
    echo "url = ${url}"
    curl --progress-bar -k $url > $sdk_file_name

    # 安装SDK
    echo "install front ui sdk to ${sdk_install_path}/${ui_sdk_version}"
    printf "${sdk_install_path}/${ui_sdk_version}\n\n" | bash $sdk_file_name

    # 清理SDK安装包
    rm $sdk_file_name
}

function install_front_server_sdk(){
    echo "install front server sdk"
    local sdk_install_path='~/.local/bin'
    local sdk_file_name="$server_sdk_version.linux-amd64.tar.gz"
    local sdk_local_url="http://$local_server/${sdk_file_name}"
    local sdk_remote_url="https://$cloud_server/sdk/${sdk_file_name}"
    local expanded_path=$(eval echo ${sdk_install_path})
    local version_file="${expanded_path}/go/VERSION"

    # Check if the VERSION file exists and contains the correct version
    if [ -f "$version_file" ]; then
        local installed_version=$(head -n 1 "$version_file")
        if [[ "$installed_version" == "$server_sdk_version" ]]; then
            echo "Found matching Go SDK: $server_sdk_version"
            return
        else
            echo "Existing Go SDK version ($installed_version) doesn't match required version ($server_sdk_version). Will reinstall."
        fi
    fi

    # 下载SDK
    echo "download front server sdk"
    local url=""
    check_ip_status $local_server
    if [ $? == 0 ]; then
        url=$sdk_local_url
    else
        url=$sdk_remote_url
    fi
    echo "url = ${url}"
    curl --progress-bar -k $url > $sdk_file_name

    # 安装SDK
    echo "install front server sdk to $sdk_install_path"
    rm -rf ${expanded_path}/go
    mkdir -p ${expanded_path}
    pv $sdk_file_name | tar xzf - -C ${expanded_path}
    if [[ $(cat ~/.bashrc | grep $sdk_install_path/go/bin) == "" ]]; then
        echo "export PATH=\$PATH:$sdk_install_path/go/bin" >> ~/.bashrc
    fi
    rm $sdk_file_name
}

trap __exit EXIT

# 1. 安装源码仓库
wget https://script.veryark.cn/project_init.sh --no-check-certificate
bash project_init.sh -u $url -b $version -m $manifest.xml

# 2. 安装前端SDK

if [[ $manifest == "release" ]]; then
    install_front_ui_sdk
    install_front_server_sdk
fi

echo "Project $projectname init all done!!"
echo 'Now please use command "source ~/.bashrc" to reload the bash configuration.'



