#!/bin/bash
# packager          : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage			: http://www.graphicsmagick.org/
# source location	: http://freefr.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.7/GraphicsMagick-1.3.7.tar.bz2
# dependency        : 
# last revised      : 26.10.2009

softname='GraphicsMagick'
softversion='1.3.7'
packageversion='1fb'

arch='i486'
cpu='i686'

prefix=/usr

# log
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.bz2"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://freefr.dl.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.7/$softname-$softversion.tar.bz2

# prepare the build result directory
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/$prefix/bin
mkdir -p $dest/usr/doc
mkdir -p $dest/usr/src/$softname-$softversion

# Slac-desc
cat <<EOF > $dest/install/slack-desc
              |-----handy-ruler------------------------------------------------------|
GraphicsMagick: GraphicsMagick (A comprehensive image processing rendering system)
GraphicsMagick:
GraphicsMagick: GraphicsMagick is a robust collection of tools and
GraphicsMagick: libraries which support reading, writing, and manipulating
GraphicsMagick: an image in over 88 major formats including popular 
GraphicsMagick: formats like TIFF, JPEG, JPEG-2000, PNG, PDF, PhotoCD,
GraphicsMagick: SVG, and GIF.
GraphicsMagick:
GraphicsMagick:
GraphicsMagick:
GraphicsMagick:
EOF

# check if source is present
if [ -f $softname-$softversion.tar.bz2 ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

# extract the source code
cd $buildir
tar xvf $srcpkg
cd $src

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --bindir=/usr/bin \
    --libdir=/usr/lib \
    --includedir=/usr/include \
    --mandir=/usr/man \
    --program-prefix="" \
    --program-suffix="" \
    --enable-static="no" \
	--enable-shared="yes" \
    --build=$arch-slackware-linux || exit

make
make install DESTDIR=$dest
    
# add 'default' files
mv -v $dest/usr/share/doc/$softname $dest/usr/doc/$softname-$softversion
rm -rf $dest/usr/share/doc

# strip
( cd $dest
  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
)

# compress man pages
( cd $dest/$prefix/man
  find . -name "*.?" | xargs gzip -9
)

# SalixBuild
cp $buildir/build-$softname.sh $dest/$prefix/src/$softname-$softversion

# set target permissions
chown -R root:root $dest

cd $dest
find . -perm 664 -exec chmod 644 {} \;
find . -perm 640 -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 {} \;
find . -type d -exec chmod 755 {} \;

# real packaging work
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
echo -e "bzip2,e2fsprogs,freetype,gcc,jasper,lcms,libICE,\
libSM,libX11,libXau,libXdmcp,libXext,libjpeg,libpng,\
libtiff,libxcb,libxml2,zlib" > $package.dep

# .src
rm -rf $src
rm -rf $dest

# erase
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/ap/GraphicsMagick/source/build-GraphicsMagick.sh > $package.src
echo $source >>$package.src

# log
) 2>&1 | tee "$softname-$softversion-build.log"
