#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-expat32

VERSION=2.0.0
ARCH=x86_64
BUILD=2
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf expat-$VERSION
tar xzvf $CWD/expat-$VERSION.tar.gz
cd expat-$VERSION
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
CC="gcc -m32" \
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib
make -j6
make install DESTDIR=$PKG
( cd $PKG
	# Get rid of stuff we'd prefer to use the 64-bit versions of
	rm -rf usr/{include,man}
	(cd usr/bin; mkdir 32; mv * 32)	
	# Strip binaries
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

cd $PKG
mkdir install
cat $CWD/slack-desc > install/slack-desc
makepkg -l y -c n ../expat32-$VERSION-$PKGARCH-$BUILD.tgz

