#!/bin/bash
CUR_DIR=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)
OS_NAME=$(sed -nr '/NAME/s/^NAME="(.*)"$/\1/gp' /etc/os-release)
VERSION_ID=$(sed -nr '/VERSION_ID/s/^VERSION_ID="(.*)"$/\1/gp' /etc/os-release)
PLATFROM=$(arch)
echo "OS_NAME:$OS_NAME"
echo "PLATFROM:$PLATFROM"
echo "VERSION_ID:$VERSION_ID"

download() {
    url="$1"
    softName=${url##*/}
    echo -e "\n\nBegin to download ${softName}"

    times=0
    while true
    do
        curl -k -L -O ${url}
        if [ $(ls -l ${softName}|awk '{print $5}') -ge 50000 ]; then
            echo "${softName} download complete"
            break
        else
            times=$[${times}+1]
            if [ ${times} -gt 3 ]; then
                echo "package ${softName} download failed,pls check"
                exit 1
            fi
            echo "package ${softName} download failed, retry $times in 3 seconds......"
            sleep 3
        fi
    done
}

if [ "${PLATFROM}" == "x86_64" ];then
    if [ "$OS_NAME" == "Ubuntu" ];then
        if [ "$VERSION_ID" == "20.04" ];then
            download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/glog_0.6.0_ubuntu.tar.gz
            download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/opencv_4.5.5_ubuntu.tar.gz
        elif [ "$VERSION_ID" == "22.04" ];then
            download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/opencv_4.2.0_ubuntu.tar.gz
        fi
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/obssdk_3.22.3_ubuntu.tar.gz
    elif [ "$OS_NAME" == "openEuler" ];then
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/cpprestsdk_2.10.15.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/duktape_2.6.0.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/ffmpeg_4.4.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/opencv_4.2.0.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive/obssdk_3.22.3.tar.gz
    fi
elif [ "${PLATFROM}" == "aarch64" ];then
    if [ "$OS_NAME" == "Ubuntu" ];then
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/glog_0.6.0_ubuntu.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/opencv_4.5.5_ubuntu.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/obssdk_3.22.3_ubuntu.tar.gz
    elif [ "$OS_NAME" == "openEuler" ];then
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/cpprestsdk_2.10.18.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/duktape_2.6.0.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/ffmpeg_4.4.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/opencv_4.2.0.tar.gz
        download https://github.com/modelbox-ai/modelbox-binary/releases/download/BinaryArchive_aarch64/obssdk_3.22.3.tar.gz
    fi
else
    echo "build error"
    exit 1
fi

ls -lh *.tar.gz
ls release|egrep 'devel|document|solution|demo'|xargs -i rm -f release/{}
ls -lh release
