#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# Requirements: perl-Tk
# http://afick.sourceforge.net/
# http://www.slacky.eu

NAME=afick
VERSION=2.13
ARCH=${ARCH:-noarch}
BUILD=1bj

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
rm -rf $PKG
mkdir -p $PKG/usr/share/applications
SOURCE=http://surfnet.dl.sourceforge.net/sourceforge/$NAME/$NAME-$VERSION-1.tgz

if [ ! -e $NAME-$VERSION-1.tgz ]; then
 wget -c $SOURCE
fi

cd $TMP
rm -rf $NAME-$VERSION-1
tar xzf $CWD/$NAME-$VERSION-1.tgz || exit 1
cd $NAME-$VERSION-1
echo "Building $NAME-$VERSION ..."
chown -R root:root .

# Build
perl Makefile.pl || exit 1

# Install
make all \
   DESTDIR=$PKG \
   TARGET_DOC=$PKG/usr/doc/$NAME-$VERSION \
   TARGET_MAN=$PKG/usr/man || exit 1

rm -fr $PKG/etc/afick.conf.sav
mv $PKG/etc/afick.conf $PKG/etc/afick.conf.new
(cd $PKG && touch var/lib/afick/afick.dir var/lib/afick/afick.pag var/lib/afick/afick.ctr var/lib/afick/history )

rm -fr $PKG/usr/share/{applnk,icons,gnome}
# kde menu entry
cat > $PKG/usr/share/applications/$NAME.desktop <<EOF
[KDE Desktop Entry]
Name=$NAME
GenericName=A quick and portable tripwires clone
Exec=$NAME-tk
Type=Application
Icon=/usr/share/pixmaps/$NAME.png
Terminal=false
X-KDE-StartupNotify=true
X-KDE-SubstituteUID=true
Categories=Qt;KDE;System;
EOF

# Gzip the man pages:
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Build the package
cd $PKG
mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
chown -R root:root .
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

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