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

VERSION=1.17
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1ga}

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf lcms-$VERSION
tar xjvf $CWD/lcms-$VERSION.tar.bz2 || exit 1
cd lcms-$VERSION
chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
CFLAGS=-O3 \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --enable-static=no \
  --with-python
make -j4 || exit 1
make install DESTDIR=$PKG

# delete all stuff provided by lcms package
rm -r $PKG/usr/bin
rm -r $PKG/usr/include
rm -r $PKG/usr/man
rm -r $PKG/usr/lib/{pkgconfig,liblcms.la,liblcms.so*}

( cd $PKG
  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
)

gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/usr/doc/python-lcms-$VERSION
cp -a \
  AUTHORS COPYING INSTALL NEWS README* doc \
  $PKG/usr/doc/python-lcms-$VERSION
find $PKG/usr/doc/python-lcms-$VERSION -type f -exec chmod 644 {} \;
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/python-lcms-$VERSION/slack-desc
cat $CWD/python-lcms.SlackBuild > $PKG/usr/doc/python-lcms-$VERSION/python-lcms.SlackBuild

cd $PKG
ADD="python >= 2.5.2-i486-1" \
requiredbuilder -v -y -s $CWD $PKG
if [ -x /sbin/makepkg7 ]; then
 makepkg7 -l y -c n $CWD/python-lcms-$VERSION-$ARCH-$BUILD.tgz
else
 makepkg -l y -c n $CWD/python-lcms-$VERSION-$ARCH-$BUILD.tgz
fi

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
