#!/bin/bash 

# aaa_elflibs.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for ARMedslack
# 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}

# 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 )"

# Iterate through the list and extract them into the temporary dir:
for i in ${PACKAGES}; do
  if [ -s $PKGSTORE/$i-[0-9]*.tgz ]; then
     echo "Extracting package $PKGSTORE/$i"
     tar xvvf $PKGSTORE/$i-[0-9]*.tgz lib usr/lib install 2> /dev/null
     echo -n "Please wait - running 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

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