#!/bin/bash

# 设置要收集依赖的二进制文件路径
EXECUTABLE=debian/kaiming/opt/kaiming-tools/bin/kaiming
DEST_DIR=debian/kaiming/opt/kaiming-tools/lib

# 获取二进制文件的所有依赖库路径
for lib in $(ldd $EXECUTABLE | grep -o '/.*' | sort -u); do
    # 拷贝动态库到指定目录
    if [ -f "$lib" ]; then
        cp $lib $DEST_DIR  
    fi
done

if [ ! -e debian/kaiming/opt/kaiming-tools/bin/crun ]; then
	cp -p /usr/bin/crun debian/kaiming/opt/kaiming-tools/bin/crun1.14.1
fi

ARCH=$(uname -m)

case "$ARCH" in 
    x86_64)
    if [ ! -e debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2 ]; then
        cp -p /lib/x86_64-linux-gnu/libyajl.so.2 debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2
    fi	
    ;;
    aarch64)
    if [ ! -e debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2 ]; then
        cp -p /lib/aarch64-linux-gnu/libyajl.so.2 debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2
    fi	
    ;;
    loongarch64)
    if [ ! -e debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2 ]; then
        cp -p /lib/loongarch64-linux-gnu/libyajl.so.2 debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2
    fi	
    ;;
    sw_64)
    if [ ! -e debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2 ]; then
        cp -p /lib/sw_64-linux-gnu/libyajl.so.2 debian/kaiming/opt/kaiming-tools/lib/libyajl.so.2
    fi	
    ;;
esac