#!/bin/bash
##############################################################################
# Program: find_aaaelflibpkgs
# Purpose: Find contents of Slackware's a/aaa_elflibs package
# Author : Stuart Winter <stuart@armedslack.org> with the Perl one-liner
#          by Jim Hawkins
# Date...: 28-Aug-2006
# Version: 1.00
##############################################################################
# You can run this natively on ARMedslack 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 ARMedslack.
# That's why this is a separate script.
#
# Usage: ./find_aaaelflibpkgs
# 
# This script will output a file in the $CWD named:
#   'x86_slackware_aaa_elflibs_pkg_list.txt'
# which is used by aaa_elflibs.SlackBuild.
#
##############################################################################

CWD=$( pwd )

# Drag in the ARMedslack build kit: we use the config in here to 
# find out where our real x86 Slackware tree is:
source /usr/share/slackdev/buildkit.sh

# Make a temporary extraction directory in which we'll explode Slackware's
# aaa_elflibs 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_elflibs package so that we can determine its contents:
cd $TMP
tar xvvf $SLACKSOURCE/../slackware/a/aaa_elflibs*.tgz
# 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 .tgz package:
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_elflibs_symlinks.txt

# Determine which library files belong to which package:
( cat $TMP/execfiles | while read lib; do 
      cat $MANIFEST | \
      perl -e'while (<STDIN>){$p=$1 if/Package: (.*\.tgz)/; print "$p:$ARGV[0]\n" if/\Q$ARGV[0]\E/}' $lib | \
      egrep -v -- "aaa_elflibs|-solibs-" 
    done ) | sort | uniq 
) > $CWD/x86_slackware_aaa_elflibs_pkg_list.txt

# Clean up:
#rm -rf $TMP
