#!/usr/bin/python3

import os
import sys
import pathlib
import subprocess

if len(sys.argv) != 2:
   print("Usage %s <folder_to_check>" % sys.argv[0])
   sys.exit(1)

def run_ldd(path):
    output = subprocess.check_output(["ldd", path])
    not_found = [line.strip() for line in output.splitlines() if b"not found" in line]
    if not not_found:
        return True
    for entry in not_found:
        print("ERROR: File %s has ldd error: %s" % (path, entry.decode("utf-8")))
    return False

error = False
path_to_check = sys.argv[1]
for so_file in pathlib.Path(path_to_check).glob("**/*.so"):
    success = run_ldd(str(so_file))
    if not success:
        error = True

if error:
    print("ERROR: Some .so files have not found dependencies")
    sys.exit(2)
