#!/bin/bash
# Script: collect_so_files.sh

BINARY="AppImage/usr/bin/r"
LIB_DIR="AppImage/usr/lib"
mkdir -p "$LIB_DIR"

# Function to copy a library and its dependencies
copy_with_deps() {
    local lib="$1"
    if [ -f "$lib" ] && [ ! -f "$LIB_DIR/$(basename "$lib")" ]; then
        cp "$lib" "$LIB_DIR/"
        echo "Copied: $lib"
        # Recursively check dependencies of this library
        ldd "$lib" | grep -o '/[^ ]\+' | while read -r dep; do
            if [ -f "$dep" ]; then
                copy_with_deps "$dep"
            fi
        done
    fi
}

# Start with the binary’s dependencies
ldd "$BINARY" | grep -o '/[^ ]\+' | while read -r lib; do
    copy_with_deps "$lib"
done

