#!/bin/sh
# script to install Bill Landry's <bill at inetmsg dot com> scripts
# for maintaining Sanesecurity signatures for clamav
# Copyright (C) 2009-2010 Eric Shubert <ejs@shubes.net>
#                         Jake Vickers <jake@qmailtoaster.com>
# NOTE - This script should be replaced by an rpm package at some point
########################################################################
#  7/31/12 shubes - modified to get script from sourceforge
#  1/07/10 shubes - added -s flag to qmail-clam to get rid of cron emails
#  9/25/09 shubes - Totally refactored to clean things up
#  3/22/09 Jake   - Originally Written
#
WEB_LOC=http://sourceforge.net/projects/unofficial-sigs/files
PKG_NAME=clamav-unofficial-sigs
PKG_VER=3.7.1
PKG_TGZ=$PKG_NAME-$PKG_VER.tar.gz
QTP_DIR=/opt/qmailtoaster-plus

########################################################################
# initialization processing
#
a1_initialization(){

# Make sure we're root
if [ "$UID" != "0" ]; then
  echo "Error: You are not logged in as root, please su -"
  exit 1
fi

# remove files from previous version
rm -rf       /etc/unofficial-clamav-sigs.conf \
 /etc/cron.hourly/unofficial-clamav-sigs.sh \
 /usr/unofficial-dbs

# set a temp directory for the tar file. $$ is the current shell ID.
TEMP_DIR=$(tempfile 2>/dev/null) || TEMP_DIR=/tmp/$me.$$

# make sure the temp directory is deleted when we're done
trap "rm -rf $TEMP_DIR" 0 1 2 5 15
}

###################################################################
## download and unpack current package
#
a3_download_unpack(){

rm -rf $TEMP_DIR
mkdir -p $TEMP_DIR
echo "$me $myver - getting version $PKG_VER of $PKG_NAME ..."

wget --tries=3 -O $TEMP_DIR/$PKG_TGZ $WEB_LOC/$PKG_TGZ/download

if [ "$?" != "0" ]; then
  echo "$me - unable to reach $WEB_LOC/$PKG_TGZ/download, please try again later."
  exit 1
fi

tar -xz -C $TEMP_DIR -f $TEMP_DIR/$PKG_TGZ
}

###################################################################
## install the package files
#
a5_install_files(){

# install the shell script
cp $TEMP_DIR/$PKG_NAME-*/$PKG_NAME.sh $QTP_DIR/bin/.
chmod 755 $QTP_DIR/bin/$PKG_NAME.sh
ln -sf ../..$QTP_DIR/bin/$PKG_NAME.sh /usr/sbin/.

# install the configuration file

cat $TEMP_DIR/$PKG_NAME-*/$PKG_NAME.conf | sed \
    -e "s|/var/lib/clamav|/usr/share/clamav|" \
    -e "/#reload_opt=/areload_opt=\"qmail-clam -s restart\"" \
    -e "s|reload_dbs=\"no\"|reload_dbs=\"yes\"|" \
    -e "s|/usr/unofficial-dbs|/usr/share/clamav/unofficial-dbs|" \
    -e "s|_silence=\"no\"|_silence=\"yes\"|" \
    -e "s|enable_logging=\"no\"|enable_logging=\"yes\"|" \
    -e "s|log_file_path=\"/var/log\"|log_file_path=\"/var/log/clamav\"|" \
    -e "s|log_file_name=\"clamav-unofficial|log_file_name=\"unofficial|" \
    -e "s|configuration_complete=\"no\"|configuration_complete=\"yes\"|" \
    > $QTP_DIR/etc/$PKG_NAME.conf
ln -sf ../..$QTP_DIR/etc/$PKG_NAME.conf /etc/.

# install the man page
gzip $TEMP_DIR/$PKG_NAME-*/$PKG_NAME.8
cp $TEMP_DIR/$PKG_NAME-*/$PKG_NAME.8.gz $QTP_DIR/man/.
ln -sf ../../../..$QTP_DIR/man/$PKG_NAME.8.gz /usr/share/man/man8/.

# install the cron job (cannot symlink cron jobs)
grep "^#" $TEMP_DIR/$PKG_NAME-*/$PKG_NAME-cron \
    > /etc/cron.d/$PKG_NAME-cron
echo -e "\n25 * * * * root /usr/sbin/$PKG_NAME.sh -c /etc/$PKG_NAME.conf" \
    >>/etc/cron.d/$PKG_NAME-cron

# install the logrotate spec
cat $TEMP_DIR/$PKG_NAME-*/$PKG_NAME-logrotate | sed \
    -e "s|clamav-unofficial|clamav/unofficial|" \
    > $QTP_DIR/etc/$PKG_NAME-logrotate
ln -sf ../..$QTP_DIR/etc/$PKG_NAME-logrotate /etc/logrotate.d/.

# install documentation files
mkdir -p $QTP_DIR/doc/$PKG_NAME
mv $TEMP_DIR/$PKG_NAME-*/CHANGELOG $QTP_DIR/doc/$PKG_NAME/.
mv $TEMP_DIR/$PKG_NAME-*/INSTALL   $QTP_DIR/doc/$PKG_NAME/.
mv $TEMP_DIR/$PKG_NAME-*/LICENSE   $QTP_DIR/doc/$PKG_NAME/.
mv $TEMP_DIR/$PKG_NAME-*/README    $QTP_DIR/doc/$PKG_NAME/.
}

###################################################################
## main script execution begins here
#
me=${0##*/}
myver=v0.3.2

a1_initialization

a3_download_unpack

a5_install_files

echo "$PKG_NAME installed successfully"
echo "clamav database files provided by Sanesecurity will be updated within an hour,"
echo " and continuously after that."
exit 0
