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

if which patchelf > /dev/null 2>&1; then
  x=python3.7/site-packages/PySide2/libpyside2.cpython-37m-x86_64-linux-gnu.so.5.15
    (cd ${CCP4}/lib; test -e $(basename ${x}) || ln -s ${x})
  x=python3.7/site-packages/shiboken2/libshiboken2.cpython-37m-x86_64-linux-gnu.so.5.15
    (cd ${CCP4}/lib; test -e $(basename ${x}) || ln -s ${x})

  patchelf --set-rpath '$ORIGIN/../../..' \
    ${CCP4}/lib/python3.7/site-packages/PySide2/uic \
    ${CCP4}/lib/python3.7/site-packages/PySide2/designer \
    ${CCP4}/lib/python3.7/site-packages/PySide2/pyside2-lupdate \
    ${CCP4}/lib/python3.7/site-packages/shiboken2_generator/shiboken2

  patchelf --set-rpath '$ORIGIN' \
    ${CCP4}/coot_py2/lib/libgfortran.so.3 \
    ${CCP4}/coot_py2/lib/libopenblas-r0.3.3.so

  echo done
else
  echo
  echo To fix incorrect rpaths:
  echo 1. install patchelf
  which apt > /dev/null 2>&1 && i=apt
  which yum > /dev/null 2>&1 && i=yum
  which dnf > /dev/null 2>&1 && i=dnf
  test -z ${i} || printf '\tsudo %s install patchelf\n' ${i}
  echo 2. rerun this script
  echo
fi

