#!/bin/bash

# aaa_libraries.SlackBuild
# by Stuart Winter <mozes@slackware.com> for Slackware ARM.
# 28-Aug-2006
#
# Copyright 2006-2019  Stuart Winter, Surrey, England, UK.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################
#
# TODO:
# 2 - modify 'find' script to take 32bit vs 64bit as an argument and make it output
#     to different files.
# 3 - modify this script to automatically pick the proper file based on $ARCH
#################################################################################

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete and re-create temporary directories

###
# This'd need a little bit of checking/maybe some extra support for aarch64/lib64
# find_aaaelflibpkgs would also need fixing - just a bit of conditional code would suffice
# and that way we can use the x86_64 port as upstream for both.
###

# The basic package framework:
mkdir -vpm755 $PKG/{lib${LIBDIRSUFFIX},usr/lib${LIBDIRSUFFIX}}

# Unpack the previous bundle of ELF libraries.
# This allows us to keep old copies of libraries which some apps may still
# be linked against (either in Slackware ARM itself or 3rd parties) which
# would break if the library package was upgraded and had a new so number.
# The new libraries and symlinks are created by extracting the *latest* packages
# over the top of the old.
echo "Unpacking archive store of ELF libraries..."
#tar xf $PORTCWD/elflibs-archive.tar.?z -C $PKG ## To unpack into the NEW package - we probably want to manually choose libs rather than have all of them.
# If we don't want to drag around old stuff forever in the package but still want
# to keep some bits, we will unpack it here.
mkdir -vpm755 $TMPBUILD/library-archive
tar xf $PORTCWD/library-archive/$PKGARCH.tar.?z -C $TMPBUILD/library-archive/ ## To archive the old AND new (new is copied later in this script)

# The scripts find that libraries from glibc are 'only present' within aaa_libraries
# but this is because they're within /lib<Suffix>/incoming within the txz package
# Let's install the aaa_glibc-solibs package into the library-archive so that
# the required libraries are made available to this script.
tar -C $TMPBUILD/library-archive/ -xvvf $PKGSTORE/a/*glibc*solibs*.t?z lib${LIBDIRSUFFIX}/incoming
mv -fv $TMPBUILD/library-archive/lib${LIBDIRSUFFIX}/incoming/* $TMPBUILD/library-archive/lib${LIBDIRSUFFIX}/

# Add in the libraries that only aaa_libraries hosts (i.e. they are no longer present in
# any Slackware packages).  These are usually required for OS upgrades and package
# transitions where we can't recompile everything for a package that links against a
# previous version of a library using the old API, for example.
if [ -s $PORTCWD/x86_slackware_aaa_libraries_onlyhost_list.txt ]; then
   # We obtain our lists from the Slackware x86_64 package, so we convert
   # lib64 to /lib (or whatever's set in the slackkit package config).
   sed 's?lib64/?lib'"${LIBDIRSUFFIX}"/'?g' $PORTCWD/x86_slackware_aaa_libraries_onlyhost_list.txt | while read lib ; do
    if [ -s $TMPBUILD/library-archive/$lib ]; then
       echo "Copying aaa_libraries-only library: $lib"
       cp -fav $TMPBUILD/library-archive/$lib $PKG/$( dirname $lib )/
    fi
  done
fi

# Populate the packages list with the file created by 'find_aaaelflibpkgs'
PACKAGES="$( awk -F: '{print $1}' < $PORTCWD/x86_slackware_aaa_libraries_pkg_list.txt | rev | cut -d- -f4- | rev | uniq )"

shopt -s extglob # needed to find package names

# Iterate through the list and extract them into the temporary dir:
for i in ${PACKAGES}; do
  if [ -s $PKGSTORE/$i-+([^-])-+([^-])-+([^-]).t?z ]; then
     echo "Extracting package $PKGSTORE/$i"
     tar xvvf $PKGSTORE/$i-+([^-])-+([^-])-+([^-]).t?z lib lib64 usr/lib usr/lib64 install 2> /dev/null
     echo -n "Running the package's install script..."
     # Significant speedup for ARM when doing symlink creation:
     # we only want to create symlinks in 'lib' directories though
     ( egrep "cd .*lib" install/doinst.sh | sed -e's?^( cd \([^;]*\);\(.*\) )$?pushd \1 \&\> /dev/null ; \2 ; popd \&\> /dev/null?g ' | bash )  > /dev/null 2>&1
     echo " ... done"
   else
     echo "** WARNING: $PKGSTORE/$i not found **"
     sleep 5
 fi
done

# Grab the .so's and symlinks.
# We don't want to copy *everything* out of the packages - only
# a few select things.
# Make the so names generic so we don't need to concern ourselves
# with version numbers:
sed 's?lib64/?lib'"${LIBDIRSUFFIX}"/'?g' $PORTCWD/x86_slackware_aaa_libraries_pkg_list.txt | rev | \
    awk -F: '{print $1}' | rev | \
    # This has the side effect that whilst it allows us to bypass issues where Slackware upstream has
    # different versions of SOs (older or newer), it does mean that in cases such as package '/l/pcre2',
    # that has multiple versioned library files, we end up with all of them in our aaa_libraries where
    # as Slackware upstream has just one.
    # I think the pro's of this simple approach outweigh the cons, and it's rare and really doesn't matter
    # enough presently to fix it.
    sed -e 's?-[0-9].*so\(.*\)?*so* ?g' -e 's?so.[0-9]\(.*\)?*so*?g' | sort | uniq > $TMPBUILD/sos-sorted
    # Copy libs from /lib:
    grep "^lib${LIBDIRSUFFIX}/" $TMPBUILD/sos-sorted | while read SOLIB ; do
      cp -fav $SOLIB $PKG/lib${LIBDIRSUFFIX}/
    done
    # Copy libs from /usr/lib:
    grep "^usr/lib${LIBDIRSUFFIX}/" $TMPBUILD/sos-sorted | while read SOLIB ; do
      cp -fav $SOLIB $PKG/usr/lib${LIBDIRSUFFIX}/
    done

# For Oracle 10g RAC support:
( cd $PKG/lib${LIBDIRSUFFIX}
  for i in ../usr/lib${LIBDIRSUFFIX}/libgcc* ; do
        ln -vvfs $i .
  done )


# Remove any black listed libraries:
if [ -s $PORTCWD/library_blacklist ]; then
   echo "Removing black listed libraries..."
   egrep -v '^#|^$' $PORTCWD/library_blacklist | while read blacklib ; do
     rm -fv $PKG/$blacklib
   done
fi

# Slackware packaging policies:
cd $PKG
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links

# Recreate the elflibs archive which will now contain the old stuff *and*
# the new packages.  So eventually this thing will be pretty big and will
# need trimming down manually.
echo "Archiving the ELF libs..."
cd $TMPBUILD/library-archive
cp -fa $PKG/* .
# Now repack the archive so that we have a copy of the previous stuff AND the
# new ones generated by this run of the script
tar -Ixz -cf $PORTCWD/library-archive/$PKGARCH.tar.xz .

# Determine if the ARM package has any libraries missing that are only present
# in the aaa_libraries package on x86/upstream.
# Some times the libraries are added to x86 to support 3rd party builds that built
# against previous versions - e.g. libreadline.  In order for this older version to
# make it automatically in to the ARM package, we need to already be including the library
# in aaa_libraries, and it'll be picked up out of the "elf libs archive" that is maintained
# within this source directory and by this build script.
# However, if something that isn't normally included in aaa_libraries, but is added temporarily
# e.g. "icu", we need to manually grab the old version and insert it in to the archive.
#
# Note: this file has usr/lib64 filtered out of it.
# I'd need to fix this for aarch64
if [ -s $PORTCWD/x86_slackware_aaa_libraries_onlyhost_list.txt ]; then
   echo "Determining if ths package is missing any aaa_libraries-only libraries (compared to upstream)"
   sed 's?lib64/?lib'"${LIBDIRSUFFIX}"/'?g' $PORTCWD/x86_slackware_aaa_libraries_onlyhost_list.txt | while read lib; do
     if [ ! -f $lib ]; then
        if ! egrep -q "^$lib" $PORTCWD/library_blacklist ; then
          echo "NOT FOUND: $lib"
        fi
     fi
   done
fi

echo "Now run a pkgdiff against Slackware x86's & ARM's package"
