#!/bin/bash 

# aaa_elflibs.SlackBuild
# by Stuart Winter <mozes@slackware.com> for Slackware ARM.
# 28-Aug-2006

# Wow! This build script is cool - some times I even surprise myself! ;-)

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

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

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

# 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 elflibs..."
#tar xf $PORTCWD/elflibs-archive.tar.xz -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/elf-archive
tar xf $PORTCWD/elflibs-archive.tar.xz -C $TMPBUILD/elf-archive/ ## To archive the old AND new (new is copied later in this script)

# Populate the packages list with the file created by 'find_aaaelflibpkgs'
PACKAGES="$( awk -F: '{print $1}' < $PORTCWD/x86_slackware_aaa_elflibs_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 usr/lib 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 .sos 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:
cat $PORTCWD/x86_slackware_aaa_elflibs_pkg_list.txt | rev | \
    awk -F: '{print $1}' | rev | \
    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/" $TMPBUILD/sos-sorted | while read SOLIB ; do
      cp -fav $SOLIB $PKG/lib/
    done
    # Copy libs from /usr/lib:
    grep "^usr/lib/" $TMPBUILD/sos-sorted | while read SOLIB ; do
      cp -fav $SOLIB $PKG/usr/lib/
    done

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

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Slackware 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/elf-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/elflibs-archive.tar.xz .

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