#!/bin/bash

# pango.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 25-Aug-2004

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

# 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 & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Fix for x86_64 (does not hurt the other archs but note that we will start
# using /etc/pango/$host instead of /etc/pango/ now):
zcat $CWD/pango.etc.host.location.diff.gz | patch -p1 --verbose || exit 1

# Autoconf changes linux to linux-gnu.
# Our host is $ARCH-slackware-linux not $ARCH-slackware-linux-gnueabi:
sed -i -e 's#linux|linux-gnu|#linux|linux-gnueabi|#' config.sub

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# This is where our fontconfig headers live:
#sed -i 's?^FONTCONFIG_CFLAGS.*=\(.*\)$?FONTCONFIG_CFLAGS = -I/usr/X11/include/fontconfig \1?' Makefile

# Build:
make $NUMJOBS || failmake

# Check the host value that is passed to the compile to the one in this script:
host="$ARCH-slackware-linux-gnueabi"
compile_host=$(grep 'host_triplet =' pango/Makefile | sed "s/.* = //")
if [ "x$compile_host" != "x$host" ]; then
  echo "Host mismatch: compile='$compile_host', SlackBuild='$host'" && exit 1
fi

# Install into package pseudo root:
make install DESTDIR=$PKG || failinstall

# Install wrapper for pango-querymodules:
cp $CWD/update-pango-querymodules $PKG/usr/bin/update-pango-querymodules
chmod 0755 $PKG/usr/bin/update-pango-querymodules

# Do not remove pangox.aliases
rm -f $PKG/etc/pango/pango.modules
mkdir -p $PKG/etc/pango/$host
touch $PKG/etc/pango/$host/pango.modules

# We need to have separate 32-bit and 64-bit binaries
# for places where we have two copies of the GTK+ package installed.
# (we might have x86_64 and i486 packages on the same system, for example.)
case "$host" in
  s390x*|x86_64*)
   mv $PKG/usr/bin/pango-querymodules $PKG/usr/bin/pango-querymodules-64
   ( cd $PKG/usr/bin ; ln -sf pango-querymodules-64 pango-querymodules )
   ;;
  *)
   mv $PKG/usr/bin/pango-querymodules $PKG/usr/bin/pango-querymodules-32
   ( cd $PKG/usr/bin ; ln -sf pango-querymodules-32 pango-querymodules )
   ;;
esac

mkdir -p $PKG/install
cat <<EOF > $PKG/install/doinst.sh
# Updating the pango.modules file:
chroot . /sbin/ldconfig 2> /dev/null
if [ -x /usr/bin/update-pango-querymodules ]; then
  /usr/bin/update-pango-querymodules
fi
EOF

mkdir -p $PKG/usr/doc/pango-$VERSION
cp -a \
  AUTHORS COPYING* NEWS README TODO \
  $PKG/usr/doc/pango-$VERSION
ln -s /usr/share/gtk-doc/html/pango $PKG/usr/doc/pango-$VERSION/html

# 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, 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
