#!/bin/sh
#141112 created. code taken out of 3builddistro
#180606 narrow search.

if [ -f WOOFMERGEVARS ];then
. ./WOOFMERGEVARS #has variables WOOF_HOSTARCH, WOOF_TARGETARCH, WOOF_COMPATDISTRO, WOOF_COMPATVERSION
else
 echo 'File WOOFMERGEVARS does not exist. This is created by script
merge2out. Your setup is wrong, quiting.'
 exit
fi

CHKOK='bad'
[ "$WOOF_HOSTARCH" == "$WOOF_TARGETARCH" ] && CHKOK='good'
HOSTARCH="$(uname -m)"
[ "$HOSTARCH" == "$WOOF_TARGETARCH" ] && CHKOK='good'
[ "$HOSTARCH" == "x86_64" ] && xHOSTARCH='amd64'
[ "$xHOSTARCH" == "$WOOF_TARGETARCH" ] && CHKOK='good'

if [ "$CHKOK" != "good" ];then
 echo "host=${HOSTARCH}, target=${WOOF_TARGETARCH}"
 echo "these must be the same to be able to run this script."
 exit
fi

[ ! -d sandbox3/rootfs-complete ] && exit
WKGDIR="`pwd`"

. ./DISTRO_SPECS      #this has DISTRO_VERSION, DISTRO_FILE_PREFIX

#now do dependency check...
dependcheck() {
 echo -n "" > /tmp/missinglibs.txt
 echo -n "" > /tmp/notmissinglibs0.txt
 echo -n "" > /tmp/notusedlibs.txt
 find $WKGDIR/sandbox3/rootfs-complete -type f | #110516
 while read ONEFILE #110516
 do
  echo -n '.'
  ISANEXEC="`file --brief $ONEFILE | grep -E "LSB executable|shared object"`"
  if [ ! "$ISANEXEC" = "" ];then
   #need to chroot into rootfs-complete...
    NUMBYTESALL="`echo -n "$ONEFILE" | wc -c | tr -s " " | cut -f 2 -d " "`"
    NUMBYTESHD=`echo -n "$WKGDIR/sandbox3/rootfs-complete" | wc -c | tr -s " " | cut -f 2 -d " "`
    NUMBYTESHD="`expr $NUMBYTESHD + 1`"
    NEWROOTFILE="`echo -n "$ONEFILE" | cut -b $NUMBYTESHD-$NUMBYTESALL`"
   LDDRESULT="`chroot ${WKGDIR}/sandbox3/rootfs-complete ldd ${NEWROOTFILE}`"

   MISSINGLIBS="`echo "$LDDRESULT" | grep "not found" | cut -f 2 | cut -f 1 -d " " | tr "\n" " "`"
   if [ ! "$MISSINGLIBS" = "" ];then
    echo "File $ONEFILE has these missing library files:" >> /tmp/missinglibs.txt
    echo " $MISSINGLIBS" >> /tmp/missinglibs.txt
    #echo "The missing libs are in these packages:" >> /tmp/missinglibs.txt
    #for ONEMISSING in $MISSINGLIBS
    #do
    # #find ${WKGDIR}/packages-${DISTRO_FILE_PREFIX} -name $ONEMISSING >> /tmp/missinglibs.txt
    # find ${WKGDIR}/packages-${DISTRO_FILE_PREFIX} -name $ONEMISSING >> /tmp/missinglibs.txt
    #done
    echo "" >> /tmp/missinglibs.txt
   fi
   #find all libs not missing...
   NOTMISSING="`file --brief $ONEFILE | grep -v "not found" | grep "shared object"`"
   if [ "$NOTMISSING" != "" ];then
    basename "$ONEFILE" >> /tmp/notmissinglibs0.txt
   fi
  fi
 done
 sync
 sort -u /tmp/notmissinglibs0.txt > /tmp/notmissinglibs.txt
 #now go thru rootfs-complete and see if any unused libs...
 find $WKGDIR/sandbox3/rootfs-complete -type f | grep '/lib' | #110516 180606
 while read ONEFILE #110516
 do
  ISANLIB="`file --brief $ONEFILE | grep "shared object"`"
  if [ ! "$ISANLIB" = "" ];then
   FILEBASE='/'"`basename $ONEFILE`"'$'
   FNDMATCH="`cat /tmp/notmissinglibs.txt | grep "$FILEBASE"`"
   if [ "$FNDMATCH" = "" ];then
    echo "File $ONEFILE is in Puppy but is not used." >> /tmp/notusedlibs.txt
   fi
  fi
 done
}

dependcheck
if [ -s /tmp/missinglibs.txt ];then #true if file not zero size.
 echo
 echo "UNFORTUNATELY, these libs are missing:"
 cat /tmp/missinglibs.txt
 echo
fi
if [ -s /tmp/notusedlibs.txt ];then
 echo
 echo "INTERESTING, the following libs are in Puppy but apparently not used:"
 cat /tmp/notusedlibs.txt
 echo
fi

if [ -s /tmp/missinglibs.txt ];then #true if file not zero size.
 echo
 echo "Please examine file /tmp/missinglibs.txt"
fi
if [ -s /tmp/notusedlibs.txt ];then
 echo "Please examine file /tmp/notusedlibs.txt"
fi

