#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-gqview

VERSION=2.0.1
ARCH=${ARCH:-i486}
BUILD=2

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf gqview-$VERSION
tar xzvf $CWD/gqview-$VERSION.tar.gz
cd gqview-$VERSION
chown -R root.root .
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc || exit 1
make || exit 1
make install DESTDIR=$PKG

gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/usr/doc/
mv $PKG/usr/share/doc/gqview-$VERSION/ $PKG/usr/doc/
cp -a AUTHORS $PKG/usr/doc/gqview-$VERSION/
chmod 644 $PKG/usr/doc/gqview-$VERSION/AUTHORS
rm -rf $PKG/usr/share/doc/

chown -R root.root $PKG/
chgrp -R bin $PKG/usr/bin/

( 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

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/gqview-$VERSION-$ARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/gqview-$VERSION
  rm -rf $PKG
fi
