#!/bin/bash
##############################################################################
# Program: find_aaa_libraries_libpkgs
# Purpose: Find contents of Slackware's a/aaa_libraries package
# Author : Stuart Winter <mozes@slackware.com> with the Perl one-liner
#          by Jim Hawkins
# Date...: 28-Aug-2006
# Version: 1.00
##############################################################################
# You can run this natively on Slackware ARM but beware that it'll take
# the best part of an hour! (when compiling KDE
# simultaneously, anyway ;-) )
# Run it on an x86, and then run the build script on Slackware ARM.
# That's why this is a separate script.
#
# Usage: ./find_aaaelflibpkgs
#
# This script will output a file in the $CWD named:
#   'x86_slackware_aaa_libraries_pkg_list.txt'
# which is used by aaa_libraries.SlackBuild.
#
##############################################################################

CWD=$( pwd )

# Load in the Slackware ARM build kit: we use the config in here to
# find out where our real x86 Slackware tree is:
if [ ! -f /usr/share/slackdev/buildkit.sh ]; then
   echo "You need to install and configure the slackkit package."
   exit 1
 else
   source /usr/share/slackdev/buildkit.sh
fi

# The Slackware x86 trees are at the same level as the Slackware ARM directories:
SLACKSOURCE=$PWD/../../../../slackware64-current/

echo "Slackware source tree: $SLACKSOURCE"

# Make a temporary extraction directory in which we'll explode Slackware's
# aaa_libraries package:
TMP=/tmp/aaa-elfdingy
rm -rf $TMP
mkdir -pm755 $TMP

# Unpack the Slackware manifest file - helps speedup the search:
MANIFEST=$TMP/MANIFEST
bzcat $SLACKSOURCE/slackware*/MANIFEST.bz2 > $MANIFEST

# Extract the Slackware aaa_libraries package so that we can determine its contents:
cd $TMP
tar xvvf $SLACKSOURCE/slackware*/a/aaa_libraries*.t?z
# We want know about the symlinks too -- most are versionless symlinks
# but there may also be some manually added ones -- we'll check these
# later when we manually check over our port's resulting .t?z package:
# In Slackware >=15.0, aaa_libraries does not contain symlinks -- instead
# installpkg runs ldconfig, which subsequently creates them.
[ -f install/doinst.sh ] && sh install/doinst.sh

# Find all executable files:
( find . -name '*.so*' -type f -printf "%P\n" | xargs -i file '{}' | egrep '(ELF.*shared.)' | awk -F: '{print $1}' > $TMP/execfiles

# Store the symlinks for later perusal:
# Don't need this -- we take the symlinks from the port's packages install/doinst.sh
#  find . -type l -printf "%h/%l --> %P\n" > $CWD/x86_slackware_aaa_libraries_symlinks.txt

# Determine which library files belong to which packages:
# HACK: switch lib64 to /lib -- will need to be modified for aarch64 port.
( cat $TMP/execfiles | while read lib; do
      cat $MANIFEST | \
      perl -e'while (<STDIN>){$p=$1 if/Package: (.*\.t.z)/; print "$p:$ARGV[0]\n" if/\Q$ARGV[0]\E/}' $lib #| sed 's?lib64?lib?'
    done ) | sort | uniq
) > $TMP/lib_to_pkglist

# Determine which packages contain each library - excluding aaa_libraries since we are building
# that package, obviously we can't look in there for the libraries!
# We'll also ignore the -solibs packages and we'll choose the primary package:
egrep -v -- "aaa_libraries|-solibs-" $TMP/lib_to_pkglist > $CWD/x86_slackware_aaa_libraries_pkg_list.txt

# Determine which library files are *only* provided by aaa_libraries and no longer exist
# within packages (one of the purposes of aaa_libraries is to host older copies of libraries to
# aid in upgrades).  These are the ones we'll need to pull from our archive stash.
cut -d: -f2- $TMP/lib_to_pkglist | sort | uniq -u > $CWD/x86_slackware_aaa_libraries_onlyhost_list.txt

# Now we know which libs are _in_ aaa_libraries, we need to know which are *only* in aaa_libraries
#{ awk -F: '{print $2}' x86_slackware_aaa_libraries_pkg_list.txt ; cat x86_slackware_aaa_libraries_onlyhost_list.txt ;} | sort | uniq

#
# Filter out ones that we know are missing from Slackware ARM (List is provided by aaa_libraries.SlackBuild)
#
# This is because there wasn't such a version (e.g. the hard float port was created long after particular
# versions of libraries were in use), or it was decided not to temporarily add an older version that
# isn't normally containd within aaa_libraries (therefore the older version wasn't automatically archived -- we'd
# need to have gone and pulled it from the archive of the previous package and it was deemed not worth it).
#
# List updated: 05-Apr-2019
#
for gonelib in \
 lib/libncurses.so.5.9 \
 lib/libncursesw.so.5.9 \
 lib/libtermcap.so.2.0.8 \
 usr/lib/libform.so.5.9 \
 usr/lib/libformw.so.5.9 \
 usr/lib/libhistory.so.6.3 \
 usr/lib/libicudata.so.63.1 \
 usr/lib/libicui18n.so.63.1 \
 usr/lib/libicuio.so.63.1 \
 usr/lib/libicutest.so.63.1 \
 usr/lib/libicutu.so.63.1 \
 usr/lib/libicuuc.so.63.1 \
 usr/lib/libmenu.so.5.9 \
 usr/lib/libmenuw.so.5.9 \
 usr/lib/libpanel.so.5.9 \
 usr/lib/libpanelw.so.5.9 \
 usr/lib/libpng14.so.14.22.0 \
 usr/lib/libreadline.so.6.3 \
 usr/lib/libstdc++.so.5.0.7 ; do
  sed -i '\?'"${gonelib}"'?d' $CWD/x86_slackware_aaa_libraries_onlyhost_list.txt
done

# Clean up:
#rm -rf $TMP
