#!/bin/bash
# Split an upstream tarball into -dfsg, and -unrarplugin.
if test $# -ne 2; then
    echo -e "Usage: $0 <PATH> <VERSION>\n\t<PATH> - directory that contains clamav-<VERSION>.tar.gz";
    exit 1;
fi

test -d $1 || { echo "Directory $1 doesn't exist"; exit 2; }
TARBALL=`realpath "$1/clamav-$2.tar.gz"`
test -f $TARBALL || { echo "Tarball $TARBALL doesn't exist"; exit 3; }

TEMP=`mktemp -d __splitXXXXXX` || { echo "Cannot create temporary directory"; exit 2; }
echo "Temporary directory is $TEMP"
cd $TEMP || exit 3;
echo "Extracting $TARBALL";
tar -xzf $TARBALL || { echo "Failed to extract $TARBALL"; exit 4; }

UNRARPKG=clamav-$2-unrar.tar.gz
DFSGPKG=clamav-$2-dfsg.tar.gz
UNRARDIR="clamav-$2-unrar"
MAKEFLAGS=-j4

mkdir testpfx || { echo "Failed to create testpfx"; exit 5; }
TESTPFX=`realpath testpfx`
mv clamav-$2 clamav-$2-dfsg &&
mkdir $UNRARDIR &&
UNRARDIR=`realpath "$UNRARDIR"` &&
echo "Preparing dfsg package" &&
cd clamav-$2-dfsg &&
cp -R libclamunrar_iface $UNRARDIR &&
mv libclamunrar $UNRARDIR &&
cp -R m4/ $UNRARDIR &&
cp -R config/ $UNRARDIR &&
cp configure.in $UNRARDIR &&
cd ../ &&
tar -czf $DFSGPKG clamav-$2-dfsg/ &&
cd $UNRARDIR &&
echo "Preparing unrar package" &&
sed -ie '/AC_OUTPUT/,/])/ {
/^AC_OUTPUT/p
s/^libclamav\/Makefile/libclamunrar_iface\/Makefile/p
/^Makefile/p
/^])/p
d
}
/LTDL/d
/ltdl/d
s/clamscan\/clamscan.c/libclamunrar_iface\/unrar_iface.c/
' configure.in &&
cat <<EOF >Makefile.am &&
ACLOCAL_AMFLAGS=-I m4
DISTCLEANFILES = target.h
SUBDIRS = libclamunrar_iface
EOF
autoreconf &&
cd .. &&
tar -czf $UNRARPKG clamav-$2-unrar/ &&
mkdir buildtest && cd buildtest &&
echo "Running build-test for $DFSGPKG" &&
tar -xzf ../$DFSGPKG && cd clamav-$2-dfsg &&
echo "Configuring" &&
./configure --disable-clamav --disable-unrar --enable-milter --prefix=$TESTPFX >makelog &&
echo "Building" &&
make $MAKEFLAGS >>makelog&&
echo "Checking" &&
make $MAKEFLAGS check >>makelog 2>&1&&
make $MAKEFLAGS install >>makelog&&
make $MAKFELAGS installcheck >>makelog&&
echo "OK" &&
cd .. &&
echo "Running build-test for $UNRARPKG" &&
tar -xzf ../$UNRARPKG && cd clamav-$2-unrar &&
echo "Configuring" &&
./configure --disable-clamav --prefix=$TESTPFX >makelog &&
echo "Building" &&
make $MAKEFLAGS >>makelog &&
make $MAKEFLAGS install >>makelog &&
make $MAKEFLAGS installcheck >>makelog &&
echo "OK" &&
cd ../.. &&
echo "Testing whether unrar functionality works" &&
cat <<EOF >test.hdb
aa15bcf478d165efd2065190eb473bcb:544:ClamAV-Test-File
EOF

if test $? -ne 0; then
    tail makelog
    echo
    echo "Failed"
    exit 50;
fi
$TESTPFX/bin/clamscan buildtest/clamav-$2-dfsg/test/clam-v*.rar -dtest.hdb >clamscanlog
if test $? -ne 1; then
    echo "Test failed";
    cat clamscanlog
    exit 10;
fi
NDET=`grep FOUND clamscanlog | wc -l`
if test "0$NDET" -eq "2"; then
    echo "All testfiles detected"
    echo "Copying tarballs to current directory"
    mv $UNRARPKG ../ &&
    mv $DFSGPKG ../ &&
    echo "Ready: $UNRARPKG $DFSGPKG" &&
    rm -rf $TEMP &&
    echo "Removed temporary directory $TEMP" &&
    exit 0
    exit 30
fi
echo "Test failed"
cat clamscanlog
exit 100