#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-imagemagick
ARCH=alpha
BUILD=1
# This is the base version number, which is needed to cd into the source tree
BASEVER=6.3.1
# This is the version number used in the source tarball filename
FILEVER=6.3.1-5
# This is the version number used in the package, where a version number cannot
# contain a '-'
PKGVER=6.3.1_5
TARGET=$ARCH-alphaslack-linux

rm -rf $PKG
mkdir -p $PKG
( cd $TMP
rm -rf ImageMagick-$BASEVER
tar xjf $CWD/ImageMagick-$FILEVER.tar.bz2
cd ImageMagick-$BASEVER
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
./configure \
 --prefix=/usr \
 --program-prefix= \
 --enable-16bit-pixel \
 --with-x \
 --mandir=/usr/man \
 --with-frozenpaths=no \
 --enable-static=no \
 --enable-shared \
 --with-perl \
 $TARGET || exit
make || exit
make install DESTDIR=$PKG || exit
# Lately ImageMagick has been smoking crack:
rm -f $PKG/usr/lib/libltdl.*
( cd $PKG
  # Nothing but a perl upgrade should replace this (and maybe not even that)
  find . -name perllocal.pod | xargs rm -f
)
# DESTDIR is still broken about this, but works well enough otherwise:
chmod 644 $PKG/usr/share/man/man3/*
mv $PKG/usr/share/man/man3 $PKG/usr/man
rmdir $PKG/usr/share/man
( cd $PKG/usr/lib/perl5
  # Ditch empty dirs:
  rmdir */* 2> /dev/null
  rmdir * 2> /dev/null
)
( 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
)
chown -R root.bin $PKG/usr/bin
gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/usr
mv $PKG/usr/share/doc $PKG/usr
cp -a AUTHORS LICENSE NEWS NOTICE Platforms.txt QuickStart.txt README.txt $PKG/usr/doc/Imag*
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cd $PKG
makepkg -l y -c n ../imagemagick-$PKGVER-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/imagemagick.build.log
