#!/bin/sh
#
# Heavily based on the Slackware 12.1 SlackBuild
# 
# http://www.rootkit.nl/projects/rootkit_hunter.html
# 
# Packager neongen (neongen at libero.it)
# http://www.slacky.it

CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

NAME=rkhunter
VERSION=1.3.2
ARCH=${ARCH:-noarch}
BUILD=1ng
PKG=$TMP/package-$NAME
SOURCE=http://surfnet.dl.sourceforge.net/sourceforge/rkhunter/$NAME-$VERSION.tar.gz

if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG
fi


echo "+=============================================================================="
echo "| $NAME-$VERSION"
echo "+=============================================================================="



if [ ! -e $NAME-$VERSION.tar.gz ]; then
echo "Downloading source"
wget -c $SOURCE
fi


cd $TMP

echo "Unpacking $NAME..."
tar -xzf $CWD/$NAME-$VERSION.tar.gz

cd $NAME-$VERSION

find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
#cd $NAME

mkdir -p $PKG/usr

sh installer.sh --layout custom $PKG/usr/ \
     --striproot $PKG --install

mkdir -p $PKG/usr/doc/$NAME-$VERSION
mv $PKG/usr/share/doc/* $PKG/usr/doc/
mv $PKG/usr/share/man $PKG/usr/
gzip -9 $PKG/usr/man/*/*
rm -rf $PKG/usr/share/
mv $PKG/usr/etc/rkhunter.conf $PKG/usr/etc/rkhunter.conf.new

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

chown root:root -R $PKG
cd $PKG
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/../$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$NAME-$VERSION
  rm -rf $PKG
  rm -rf $TMP/$NAME-$VERSION.tar.gz*
fi
