#!/bin/bash

set -e

path_cur=$(cd `dirname $0`; pwd)
build_type="Release"
# 生成目录
app_path="app"
# 可执行程序名
appname="VCarContainer"

# 拷贝执行文件依赖库
function cpoyLibs() {
    # 利用 ldd 提取依赖库的具体路径
    liblist=$(ldd ./$app_path/$appname | awk '{ if (match($3,"/")){ printf("%s "), $3 } }')
    # 拷贝库文件和可执行程序到目标文件夹
    cp $liblist ./$app_path/lib
}

function prepare_path() {
    [ -n "$1" ] && rm -rf $1
    mkdir -p $1
    cd $1
}

function prepare_path_new() {
	if [ ! -d $1 ];then
	   mkdir -p $1
	fi
	cd $1
}


#NX
function build(){
    local path_build=$path_cur/build
    prepare_path $path_build

    if [ "$1" == $app_path ]; then
      cmake -DCMAKE_BUILD_TYPE="Release" ..
    else
      cmake -DCMAKE_BUILD_TYPE="Release" ..
    fi

    make -j6
    local ret=$?
    cd ..
    return ${ret}
}

# build with different according to the parameter, default is A300
if [ "$1" == $app_path ]; then
    echo "---------正式模式----------"
    if [ ! -d $1 ];then
      mkdir -p $1
      mkdir -p $app_path/config $app_path/lib $app_path/model $app_path/collect
      mkdir -p $app_path/result $app_path/tmp $app_path/videos
    fi
    build $1
    echo "开始拷贝软件库文件..."
    cpoyLibs
else
    echo "---------调试模式----------"
    if [ ! -d $1 ];then
      mkdir -p $1
      mkdir -p $app_path/config $app_path/lib $app_path/model
      mkdir -p $app_path/result $app_path/tmp $app_path/videos
    fi
    build
fi

if [ $? -ne 0 ]; then
    exit 1
fi

echo "开始拷贝配置文件..."
# 拷贝yaml文件和xml文件
cp -f ./config/*.yaml ./$app_path/config
echo "-----------successfully--------------"

exit 0
