# Set initial variables:
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-gimp
rm -rf $PKG
mkdir -p $PKG

VERSION=2.4.7
ARCH=${ARCH:-i486}
BUILD=1

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

cd $TMP
rm -rf gimp-$VERSION
tar xjvf $CWD/gimp-$VERSION.tar.bz2 || exit 1
cd gimp-$VERSION

## Brute force ugliness because some macro didn't substitute correctly:
#zcat $CWD/gimp.desktop.in.in.diff.gz | patch -p1 --verbose || exit 1

chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
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 {} \;

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var/lib \
  --mandir=/usr/man \
  --disable-static \
  --build=$ARCH-slackware-linux
make -j3 || exit 1
mkdir -p $PKG/usr/share/applications $PKG/usr/share/application-registry
make install DESTDIR=$PKG

#if [ ! -e $PKG/usr/share/gimp/2.0/images/wilber-icon.png ]; then
#  echo "Icon missing, failed"
#  exit 1
#fi

gzip -9 $PKG/usr/man/man?/*.?
rm -f $PKG/usr/man/man?/*.?
( cd $PKG/usr/man/man1
  ln -sf gimptool-2.?.1.gz gimptool.1.gz
  ln -sf gimp-remote-2.?.1.gz gimp-remote.1.gz
  ln -sf gimp-2.?.1.gz gimp.1.gz
  cd $PKG/usr/man/man5
  ln -sf gimprc-2.?.5.gz gimprc.5.gz
)

## Bail on broken links:
#if find $PKG -type l | xargs file | grep broken ; then
#  exit 1
#fi

mkdir -p $PKG/usr/doc/gimp-$VERSION
cp -a \
  AUTHORS COPYING HACKING INSTALL LICENSE NEWS README* \
  docs/Wilber* docs/keybindings.txt docs/quick_reference.ps \
  $PKG/usr/doc/gimp-$VERSION

( 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
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
makepkg -l y -c n ../gimp-$VERSION-$ARCH-$BUILD.tgz