#!/bin/sh

set -e
set -x

# pass different package names if you wish, for later versions.
SIGNAL_PKG="${SIGNAL_PKG:-signal-desktop-8.11.0-x86_64-1_SBo.tgz}"
GLIBC_PKG="${GLIBC_PKG:-glibc-2.42-x86_64-1_slack15.0.txz}"

FTP_SITE="ftp://ftp.slackware.com/pub/slackware/slackware64-15.0"
FTP_PATH="/testing/packages/binutils-gcc-glibc"

apply () {
    # install the signal package,
    # download & install latest glibc
    upgradepkg --reinstall --install-new "$SIGNAL_PKG"
    lftp -c "open ${FTP_SITE}${FTP_PATH}; get -c $GLIBC_PKG;"
    installpkg --root /opt/glibc "$GLIBC_PKG"

    # the actual patching happens here.
    find /opt/Signal/ -type f -executable            \
         -exec patchelf --set-interpreter            \
         /opt/glibc/lib64/ld-linux-x86-64.so.2 {} \; \
         -exec patchelf --force-rpath --set-rpath    \
         \$ORIGIN:/opt/glibc/lib64 {} \;

    # The `$ORIGIN'  path is required, as  a literal
    # string, with  $ escaped from the  shell; it is
    # not a shell variable.  This literal string has
    # a special  meaning to the linker  to allow the
    # executable, in this case Signal, to search its
    # own directory for libraries.
}

revert () {
    # this is all we need to revert the hack.
    upgradepkg --reinstall "$SIGNAL_PKG"
    rm -rf /opt/glibc
}

if   [ "$1" =  apply ]; then  apply;
elif [ "$1" = revert ]; then revert;
else echo "Usage: $0 [apply|revert]"; fi
