#!/bin/sh
# Heavily based on the Slackware 13.37 SlackBuild
#
# Written by Corrado Franco (http://www.conraid.net)
# Last build from Luca De Pandis aka atomix600 ( http://www.slacky.eu )
# Slackware build script for Abook (abook.sf.net)
#
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e


PKGNAME=${PKGNAME:-abook}
VERSION=${VERSION:-0.6.0pre2}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}
TAG=${TAG:-sl}
PKGTYPE=${PKGTYPE:-txz}
CWD=$(pwd)
TMP=${TMP:-/tmp/buildpkgs/$PKGNAME}
SOURCE=http://abook.sourceforge.net/devel/abook-0.6.0pre2.tar.gz
PATCH=http://abook.sourceforge.net/patches/abook_vcard_import.patch
PKG=${PKG:-$TMP/package-$PKGNAME}
DOCS="$PKG/usr/doc/$PKGNAME-$VERSION"
DOCFILES="ABOUT-NLS ANNOUNCE AUTHORS BUGS COPYING ChangeLog FAQ NEWS README RELEASE_NOTES THANKS TODO"	

case "$ARCH" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"; CHOST="i486"
             ;;
  i686)      SLKCFLAGS="-O2 -march=i686 -pipe -fomit-frame-pointer"; CHOST="i486"
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC -pipe -fomit-frame-pointer"; CHOST="x86_64"
             ;;
esac

if ! [ -e $(basename $SOURCE) ]; then
    wget $SOURCE
fi

if ! [ -e $(basename $PATCH) ]; then
    wget $PATCH
fi

rm -rf $PKG
mkdir -p $TMP $PKG
cd $TMP
rm -rf $PKGNAME-$VERSION
tar xzvf $CWD/$PKGNAME-$VERSION.tar.gz
cd $PKGNAME-$VERSION
cat $CWD/$(basename $PATCH) | patch -p1 --verbose --backup --suffix=.orig

chown -R root:root .
chmod -R u+w,go+r-w,a-s .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --infodir=/usr/info \
  --host=$CHOST-slackware-linux \
  --build=$CHOST-slackware-linux



make
make DESTDIR=$PKG install

mkdir -p ${DOCS}
for i in ${DOCFILES}; do
    if [ -e ${i} ]; then
        if ! [ -L ${i} ]; then
	    cp -a ${i} ${DOCS}
	else
	    cp -LRp ${i} ${DOCS}
        fi
    fi;
done

if [ -d $PKG/usr/man ]; then
( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)
fi

cat $CWD/$PKGNAME.SlackBuild > $DOCS/$PKGNAME.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
requiredbuilder -v -y -s $CWD $PKG
cd $PKG
/sbin/makepkg -l y -c n $CWD/$PKGNAME-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE

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