#!/bin/bash
KnownBrokenBins="nfnl_osf|snmpd|ntpsnmpd|snmptrapd|xtables-nft-multi|gxditview|windmc|agentxtrap|ranlib|dlltool|windres|wpa_gui|ar|dbus-launch|xtotroff"
FindPath="$(sed 's/:/ /g' <<<$PATH)"

#looking for any broken binary that is not on the known broken list

NewBrokenList="$(
  find $FindPath -type f -executable 2>/dev/null | while read line
  do
    file $line |grep -qE "\bELF\b|dynamically linked" || continue
    grep -qwE "$KnownBrokenBins" <<< $line && continue
    ldd $line 2>/dev/null |grep -q "not found" && echo $line
  done
)"

[ "$NewBrokenList" = "" ] && exit 0
#create a detailed report for each new broken bin in tmp/missing
( for line in $NewBrokenList
  do
    Filetype="$(file $line |sed 's/, interpreter .*$//')"
    grep -qE "\bELF\b|dynamically linked" <<< $Filetype || continue
    OwningPackage="$(grep -l "${line#/}" /var/log/packages/*)"
    Missing="$(ldd $line 2>/dev/null | grep "not found" |sed 's/^[ |\t]*//g; s/ .*$//' | sort -u)"
    [ "$Missing" != "" ] && echo -e "${OwningPackage} \n${Filetype} \n${Missing} \n"
  done
) | tee /tmp/missing

