#!/bin/bash

# rpm.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script by Patrick Volkerding.
# 09-Jun-2004

# Record toolchain & other info for the build log:
slackbuildinfo

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Install the docs before mangling them with our filters:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a ABOUT-NLS COPYING CREDITS GROUPS INSTALL README README.amiga \
      RPM-GPG-KEY RPM-PGP-KEY TODO doc/manual \
      $PKG/usr/doc/$PACKAGE-$VERSION
rm -f $PKG/usr/doc/$PACKAGE-$VERSION/manual/Makefile*

# Change the /usr/src/redhat path to /usr/src/rpm:
find . -name "*.orig" | xargs rm --verbose
fgrep -lr -- '{_usrsrc}/redhat' . | xargs sed -i 's?{_usrsrc}/redhat?{_usrsrc}/rpm?g'
fgrep -lr -- 'src/redhat' .       | xargs sed -i 's?/src/redhat?src/rpm?g'

# Using "rpm" for the vendor name cures the /usr/src bug.
#CPPFLAGS="-I/usr/include/beecrypt" \
./configure \
   --prefix=/usr \
   --with-pic \
   --without-selinux \
   --without-python \
   --without-apidocs \
   --without-dmalloc \
   --without-efence \
   --without-javaglue \
   --program-prefix="" \
   --program-suffix="" \
   --build=$ARCH-rpm-linux \
   --host=$ARCH-rpm-linux \
   --target=$ARCH-rpm-linux || failconfig

# Do a plain build, and then strip things by hand.
# Use -i since debugedit won't compile without a lot more cruft, and we don't need it anyway.
make -i || failmake
make -i install DESTDIR=$PKG

# Filter all .la files (thanks much to Mark Post for the sed script):
( cd $PKG && find . -type f -name "*.la" | xargs sed -i 's%-L/tmp/[[:graph:]]* % %g' )

# Since rpm linked statically is generally segfaulting after each glibc
# upgrade, these static libraries are very likely equally useless.
rm -f $PKG/usr/lib/*.a

# Here's a bug that's been around forever:
( cd $PKG/usr/lib/rpm && rm -rf rpmpopt ; ln -sf rpmpopt-$VERSION rpmpopt )

mkdir -p $PKG/var/lib/rpm/tmp

# Install default rpm package list:
zcat $CWD/Packages.gz > $PKG/var/lib/rpm/tmp/Packages

# We ship popt separately:
rm -f $PKG/usr/include/popt.h
rm -f $PKG/usr/man/man3/popt.3*
rm -f $PKG/usr/lib/libpopt.*
rm -f $PKG/usr/share/locale/*/LC_MESSAGES/popt.mo

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

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
