#!/bin/bash

# libwmf.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 25-Jul-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
PKG2=$TMP/package-$PKGNAM-docs
rm -rf $PKG2 && mkdir -p $PKG2

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
esac

# Create framework for packages:
mkdir -p $PKG2/{install,usr/doc}
mkdir -p $PKG/{install,usr/doc/$PKGNAM-$VERSION}

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

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --enable-static=no \
   --build=$ARCH-slackware-linux || failconfig

# Build:
make -j3 || failmake

# Install into package framework:
make install DESTDIR=$PKG

# Install docs:
mv $PKG/usr/share/doc/$PKGNAM $PKG2/usr/doc/$PKGNAM-$VERSION
rm -rf $PKG/usr/share/doc
cp -a AUTHORS BUILDING COPYING CREDITS NEWS README TODO \
      $PKG/usr/doc/$PKGNAM-$VERSION

# Install package descriptions:
install -m644 $CWD/slack-desc $PKG/install
install -m644 $CWD/slack-desc-docs $PKG2/install/slack-desc

# Start fakeroot server if required:
start_fakeroot

# Set generic Slackware packaging policies:
for i in $PKG $PKG2 ; do
    cd $i
    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
    slackhlinks     # search for any hard links
done

# Build the packages:
if [ $PORTARCH = arm ]; then
   ( cd $PKG  && slackmp )
   ( cd $PKG2 && PKGNAM=$PKGNAM-docs ARCH=noarch slackmp )
 else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PKGNAM-$VERSION-$ARCH-$BUILD.tgz
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PKGNAM-docs-$VERSION-noarch-$BUILD.tgz
fi
