#!/bin/sh -e
test -z ${CCP4} && CCP4=ccp4-8.0
test -d ${CCP4} || CCP4=/opt/xtal/ccp4-8.0
echo
if ! test -d ${CCP4}; then
  echo '#' Set up CCP4 and rerun this script.
  echo
  echo '#' If you use sh, bash, zsh, ksh, ash, etc.:
  echo source /path/to/ccp4-8.0/bin/ccp4.setup-sh
  echo
  echo '#' If you use csh, tcsh, etc.:
  echo source /path/to/ccp4-8.0/bin/ccp4.setup-csh
  echo
  exit
fi
echo Searching for ELF-files in:
echo ${CCP4}

if test _${1} = _quick; then
  xx="""
    ${CCP4}/QtPlugins/xcbglintegrations/libqxcb-glx-integration.so
    ${CCP4}/QtPlugins/xcbglintegrations/libqxcb-egl-integration.so
    ${CCP4}/QtPlugins/platforms/libqxcb.so
    ${CCP4}/coot_py2/lib/libguilereadline-v-17.so.17.0.3
    ${CCP4}/lib/libQt5XcbQpa.so.5.15.2
    ${CCP4}/bin/ipmosflm
    ${CCP4}/coot_py2/libexec/coot-bin
    ${CCP4}/libexec/qtpisa
    ${CCP4}/bin/xplot84driver
    ${CCP4}/bin/bltsh
    ${CCP4}/lib/libopengl.so
  """
  xx=$(echo "${xx}" | sed -nE 's/^\s*(\S.*)$/\1/p')
else
  xx=$(find ${CCP4} \( -name '*\.so\.*' -or -name '*\.so' -or -perm -100 \) -type f)
fi
xx=$(echo "${xx}" | sort -u)
echo found $(echo "${xx}" | wc -l) executable and library files
xx=$(file ${xx} | sed -nE 's/^([^:]+):\s*ELF\s.*$/\1/p')
echo found $(echo "${xx}" | wc -l) ELF-files
echo
sleep 2

ee='''
  /webkit2gtk-4.0/
  /lib-dynload/
  /shiboken2_generator/shiboken2
'''
ee1='''
  /coot_py2/lib/libgfortran.so.3.0.0
  /coot_py2/lib/libopenblas-r0.3.3.so
  /lib/python3.7/site-packages/PySide2/designer
  /lib/python3.7/site-packages/PySide2/pyside2-lupdate
  /lib/python3.7/site-packages/PySide2/uic
  /lib/python3.7/site-packages/ccp4mg/PdbView.so
  /lib/python3.7/site-packages/ccp4mg/cpptable.so
  /lib/python3.7/site-packages/ccp4mg/mgglframebufferobject.so
  /lib/python3.7/site-packages/ccp4mg/mgradialgradient.so
'''
ee=$ee$ee1
unset ee1

ee=$(echo "${ee}" | sed -nE 's/^\s*(\S.*)$/\1/p' | sort -u)
echo Excluding:
echo "${ee}" | paste /dev/null -
xx=$(echo "${xx}" | egrep -v $(echo ${ee} | tr " " "|"))
unset ee
echo

echo Running ldd on $(echo "${xx}" | wc -l) ELF-files
echo
unset yy
for x in ${xx}; do
  if y=$(ldd ${x} 2>/dev/null | grep 'not found'); then
    echo ${x}:
    echo "${y}"
    y=$(echo "${y}" | sed -E 's/ => not found//')
    yy="${yy}
${y}"
  fi
done

yy=$(echo "${yy}" | sort -u)
if test -z "${yy}"; then
  echo No missing libraries detected
  echo
  exit 0
else
  echo
  printf "not found:" && echo "${yy}"
  echo
  if zz=$(echo "${xx}" | egrep "$(echo ${yy} | sed 's, ,|/,g')"); then
    echo run patch_rpaths.sh to fix these:
    echo "${zz}" | paste /dev/null -
    echo
  fi
  unset zz
fi
unset xx

which apt > /dev/null 2>&1 && i=apt
which yum > /dev/null 2>&1 && i=yum
which dnf > /dev/null 2>&1 && i=dnf
case ${i} in
  (apt)
    if which apt-file > /dev/null 2>&1; then
      echo Running apt-file -l on missing libaries, this may take a while
      zz=$(for y in ${yy}; do apt-file -l find ${y} || :; done | sort -u)
      echo
    else
      echo To identify packages that provide missing libraries:
      echo 1. install apt-file
      printf '\t%s\n' 'sudo apt install apt-file'
      printf '\t%s\n' 'sudo apt-file update'
      echo 2. rerun this script
      echo 
      exit 0
    fi
  ;;
  (yum|dnf)
    echo Running ${i} provides on missing libaries, this may take a while
    zz=$(${i} provides ${yy} | sed -nE 's/^([^0-9]+)-[0-9].*:.*$/\1/p' | sort -u)
    echo
  ;;
  (*)
    echo None of apt, dnf or yum is found on the system,
    echo please contact ccp4@stfc.ac.uk
    echo 
    exit 0
  ;;
esac

if test -z "${zz}"; then
  echo
  echo See:
  echo https://www.ccp4.ac.uk/download/doc/known_issues_linux.html
else
  echo required packages:
  echo "${zz}" | paste /dev/null -
  echo 
  sleep 2
  echo Next:
  echo 1. install reqired packages
  printf '\t'
  echo sudo ${i} install ${zz}
  echo 2. rerun this script to check for remaining issues
fi
unset zz
echo

notes='''
debian-10:
sudo apt install libncurses5 libopengl0 libtinfo5 libxcb-xinerama0 libxcb-xinput0 libxkbcommon-x11-0
not found: libxcb-util.so.1
ubuntu-18:
sudo apt install libopengl0 libxcb-xinerama0 libxcb-xinput0
fedora-35:
sudo dnf install libXaw libnsl mesa-libGLU ncurses-compat-libs
centos-8:
sudo dnf install libXScrnSaver libXaw libnsl mesa-libGLU ncurses-compat-libs xcb-util-image xcb-util-keysyms xcb-util-renderutil xcb-util-wm
ubuntu-20:
sudo apt install libncurses5 libopengl0 libtinfo5 libxcb-xinerama0 libxcb-xinput0
debian-11:
sudo apt install libncurses5 libtinfo5 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-render-util0 libxcb-xinerama0 libxcb-xinput0 libxcb-xkb1 libxkbcommon-x11-0
ubuntu-22:
sudo apt install libncurses5 libtinfo5 libxcb-xinerama0 libxcb-xinput0
'''

