#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# Set initial variables:
NAME=clamassassin
VERSION=1.2.4
ARCH=${ARCH:-noarch}
BUILD=${BUILD:-3fv}
TMP=/tmp/tgz
PKG=$TMP/package-$NAME

CWD=`pwd`

SOURCE=http://jameslick.com/$NAME/$NAME-$VERSION.tar.gz			
if [ ! -e $CWD/$NAME-$VERSION.tar.gz ]; then
 wget -c $SOURCE
fi

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

cd $TMP
rm -rf $NAME-$VERSION
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION
chown -R root:root .

./configure --prefix=/usr \
--enable-subject-rewrite=***VIRUS*** \
--enable-clamdscan
#--disable-signature-version -> (...read below)
make install

# The Install simply copies the "clamassassin" script file into /usr/bin, then changes its attributes to 555.
# L'Installazione si limita a copiare lo script-file "clamassassin" in /usr/bin ed a cambiarne i permessi in 555. 

# --disable-signature-version: if enabled we must change the "/usr/share/clamav" permissions from 770 to 771 and
# the ones of the "virus definitions files" included in order to give them read access to "others". 
# --disable-signature-version: se abilitata implica il cambiamento dei permessi della dir. "/usr/share/clamav" da
# 770 a 771 e quello dei files di definizione virus in essa contenuti per permetterne la lettura ad "others".

mkdir -p $PKG/usr/bin
mv /usr/bin/clamassassin $PKG/usr/bin
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a CHANGELOG LICENSE README $PKG/usr/doc/$NAME-$VERSION
cp $CWD/clamassassin.SlackBuild $CWD/slack-desc $PKG/usr/doc/$NAME-$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 -f1 -d : | xargs strip --strip-unneeded 2> /dev/null && \
find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

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

# Build the package:
cd $PKG
ADD=$(ls /var/log/packages/ | sed -ne 's/\(clamav\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2,/p' \
-e 's/\(procmail\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2/p') \
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz

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