#!/bin/bash

# aaa_libraries.SlackBuild
# by Stuart Winter <mozes@slackware.com> for Slackware ARM.
# 28-Aug-2006
#
# Copyright 2006-2022  Stuart Winter, Donostia, Spain.
# 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.
#
#################################################################################

# Note: This package is only to be built on the master build host.
#
# How to use (this is automated within the Slackware ARM build system)
# 1. Save previous versions of library prior to building new packages:
#    Sud helper determines the current list of libraries within the upstream
#    (x86) aaa_library packages and copies them to the staging directory
#    within this package's source dir.
# 2. Batch of packages is built.
# 3. aaa_libraries.SlackBuild is run on the final pass which once again takes the list
#    of packages from the x86 txz, copies from the staging directory
#    then from the filesystem of the build machine.

# 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

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

# Unpack the previous bundle of shared libraries.
echo "Unpacking archive store of shared libraries..."
mkdir -vpm755 $TMPBUILD/library-archive
tar xf $PORTCWD/library-archive/$PKGARCH.tar.?z -C $TMPBUILD/library-archive/

# Walk the x86/upstream package, copying first from the
# archive then from the build host's filesystem:
cd $TMPBUILD/library-archive
tar tvf $SLACKSOURCE/../slackware*/a/aaa_libraries-*-*-*.t?z 2>&1 | awk '{print $6}' | grep -E '.*/.*so.*' | while read lib ; do
  # Copy a saved version.  This captures any libraries that are no longer
  # available on the host OS:
  cp -vfH --parents  ./${lib} $PKG/
  # Overwrite from the version on the host OS, if available.
  # This enables using the freshest versions:
  cp -vfH --parents /${lib} $PKG/
done

# 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.
# We used to retain all of the previous versions of libraries but this proved
# unnecessary, so we'll just archive the contents of the package.
echo "Archiving the shared libraries..."
cd $PKG
tar -Ixz -cf $PORTCWD/library-archive/$PKGARCH.tar.xz .

#####################################################################
# Taken from aaa_libraries.SlackBuild.orig for the changes in x86:
# Mon Apr 29 21:32:37 UTC 2024
# when ncurses was changed.
#
# Time to ask for trouble. We should add a few compatibility symlinks for the
# non-wide ncurses libraries, and for the now built-in tic/tinfo:
( cd $PKG/lib${LIBDIRSUFFIX}
  ln -vsf libformw.so.6 libform.so.6
  ln -vsf libmenuw.so.6 libmenu.so.6
  ln -vsf libncurses++w.so.6 libncurses++.so.6
  ln -vsf libncursesw.so.6 libncurses.so.6
  ln -vsf libpanelw.so.6 libpanel.so.6
)
#####################################################################

# 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
