#!/bin/sh
# This script incorporates Spamassassin with qscanq-psa.
#
# Copyright by Ales Djordjevic, 2005.
# We can be reached at projects@hostbird.com
# http://www.hostbird.com
#####################################################################

# this is the directory into which the script will backup the original
# qmail-queue binary. Change it if you need to (NO TRAILING /):
SCRIPTDIR="/usr/local/src/antivir"
# if you rename this script, please change the name here:
SCRIPTNAME="qscanq-psa-spam.sh"

# YOU DON'T NEED TO EDIT ANYTHING BELOW THIS POINT

#####################################################################
#TODO:	should we modify the qscanq wrap and unwrap...? probabaly!
#	write the fixqmail part
#####################################################################
# script version, please don't change
SCRIPTVER="0.9.1"
# ok, let's go
install() {
# check if /var/qmail directory exists
if [ -d /var/qmail ]
then
echo ""
echo "Ok, your qmail directory exists... Let's proceed..."
echo ""
else
echo ""
echo "The /var/qmail directory doesn't exist. That's BAD unless you have an unsupported OS..."
echo "We have to end this install now. Bye."
echo ""
exit
fi
# check if qscanq-psa is installed
if [ -s /usr/local/qscanq/command/qscanq ]
then
echo ""
echo "Qscanq-psa binary exists, that's a good sign..."
echo ""
else
echo ""
echo "Qscanq-psa binary cannot be found! Sorry, we need to stop right now..."
echo "Is qscanq-psa installed at all? Fix the problem and try again."
echo ""
exit
fi
# check to see if qscanq-psa is active
QUEUESYMLINK=`ls -la /var/qmail/bin/qmail-queue | awk '{print$11}'`
if [ $QUEUESYMLINK = /usr/local/qscanq/command/qscanq ]
then
echo ""
echo "Ok, your current qmail-queue is a symlink to qscanq binary, as it should be..."
echo ""
else
echo ""
echo "Your qmail-queue isn't a symlink to the qscanq binary. Sorry, we need to stop right now..."
echo "Is qscanq-psa active? Fix the problem and try again."
echo ""
exit
fi
# check if spamc exists!
if [ -s /usr/bin/spamc ]
then
echo ""
echo "Spamassassin's spamc binary exists, ok..."
echo ""
else
echo ""
echo "Spamassassin's spamc binary cannot be found in /usr/bin! Sorry, we need to stop right now..."
echo "Is Spamassassin installed at all? Fix the problem and try again."
echo ""
exit
fi
# check if spamd is running
SPAMDRUNNING=`service spamassassin status | awk '{print$NF}'`
if [ $SPAMDRUNNING = running... ]
then
echo ""
echo "Spamassassin's spamd daemon seems to be running, let's proceed..."
echo ""
else
echo ""
echo "Spamassassin's daemon doesn't seem to be running. Let's try to start it..."
echo ""
service spamassassin start
    sleep 5s
    SPAMDRUNNING2=`service spamassassin status | awk '{print$NF}'`
    if [ $SPAMDRUNNING2 = running... ]
    then
    echo ""
    echo "Ok, Spamassassin's spamd daemon seems to be running now, let's proceed..."
    echo ""
    else
    echo ""
    echo "Spamassassin's daemon failed to start. Fix the problem and try again."
    echo ""
    exit
    fi
fi
# configure spamd to start at boot
echo ""
echo "Checking to see if spamassassin is set up to start at boot"
echo ""
SPAMDSTARTUP=`chkconfig --list spamassassin | awk '{print$5}'`
if [ $SPAMDSTARTUP = 3:on ]
then
echo ""
echo "Ok, Spamassassin is already set up to start at boot..."
echo ""
else
chkconfig spamassassin on
echo ""
echo "Ok, Spamassassin has been set up to start at boot now..."
echo ""
fi
# create a working directory for Spamassassin
if [ -d /var/qmail/.spamassassin ]
then
echo ""
echo "Spamassassin working directory already exists... skipping creation"
echo ""
else
mkdir /var/qmail/.spamassassin
echo ""
echo "Spamassassin working directory created"
echo ""
fi
chmod 777 /var/qmail/.spamassassin
echo ""
echo "Spamassassin working directory permissons set to 777"
echo ""
# check to see if qscanq-psa spamassassin addon isn't perhaps already installed
ALREADYINSTALLED=`tail -n 1 /var/qmail/bin/qscanq/qmail-queue | awk '{print$NF}'`
if [ $ALREADYINSTALLED = /var/qmail/bin/qmail-queue.orig ]
then
echo ""
echo "It seems like qscanq-psa spamassassin addon has already been instaled!"
echo "You really shouldn't install this twice... Are you perhaps trying to uninstall it?"
echo "Try executing '$SCRIPTNAME help' to see the instructions."
echo ""
exit
else
echo ""
echo "The qscanq-psa spamassassin addon doesn't seem to be installed yet, so..."
echo ""
fi
# create a shell script to pipe e-mail trough spamassassin
echo ""
echo "Here comes the tricky part. We are creating a shell script to"
echo "pipe e-mail trough spamassassin..."
echo ""
echo "#!/bin/sh" > /var/qmail/bin/qscanq/qmail-queue.new
echo "/usr/bin/spamc | /var/qmail/bin/qmail-queue.orig" >> /var/qmail/bin/qscanq/qmail-queue.new
chown -R qmailq:qmail /var/qmail/bin/qscanq/qmail-queue.new
chmod 755 /var/qmail/bin/qscanq/qmail-queue.new
# check if qmail-queue backups already exist and rename them (with a date stamp?)
# than backup the real qmail-queue (twice, just in case)
if [ -s $SCRIPTDIR/qmail-queue.hb.bkp ]
then
cp -p $SCRIPTDIR/qmail-queue.hb.bkp $SCRIPTDIR/qmail-queue.hb.bkp.`date '+%Y-%m-%d_%T'`
else 
echo ""
fi
echo ""
echo "Let's back up the real qmail-queue..."
echo ""
cp -p /var/qmail/bin/qscanq/qmail-queue $SCRIPTDIR/qmail-queue.hb.bkp
cp -p /var/qmail/bin/qscanq/qmail-queue /var/qmail/bin/qmail-queue.hb.bkp
# move the real qmail-queue to it's new location
echo ""
echo "Now let's move the real qmail-queue to it's new location..."
echo ""
mv /var/qmail/bin/qscanq/qmail-queue /var/qmail/bin/qmail-queue.orig
# move the shell script in it's place, so that qscanq-psa can deliver mail to it
echo ""
echo "And finally, let's move the shell script in it's place."
echo ""
mv /var/qmail/bin/qscanq/qmail-queue.new /var/qmail/bin/qscanq/qmail-queue
echo ""
echo "That's it! Please check in your logs if mail is received like it should be"
echo "and if spamassassin is working!"
echo ""
}

uninstall() {
#TODO:
# check to see if fixqmail is needed!
####
# check to see if qscanq-psa is active
QUEUESYMLINK=`ls -la /var/qmail/bin/qmail-queue | awk '{print$11}'`
if [ $QUEUESYMLINK = /usr/local/qscanq/command/qscanq ]
then
echo ""
echo "Ok, your current qmail-queue is a symlink to qscanq binary, as it should be..."
echo ""
else
echo ""
echo "Your qmail-queue isn't a symlink to the qscanq binary. Sorry, we need to stop right now..."
echo "Is qscanq-psa active? Fix the problem and try again."
echo ""
exit
fi
# check to see if qscanq-psa-spam is installed
ALREADYINSTALLED=`tail -n 1 /var/qmail/bin/qscanq/qmail-queue | awk '{print$NF}'`
if [ $ALREADYINSTALLED = /var/qmail/bin/qmail-queue.orig ]
then
echo ""
echo "The qscanq-psa spamassassin addon seems to be installed, so we can try and uninstall it..."
echo ""

#rm /var/qmail/.spamassassin/*
#rmdir /var/qmail/.spamassassin
cp /var/qmail/bin/qscanq/qmail-queue /var/qmail/bin/qscanq/qmail-queue.new
mv /var/qmail/bin/qmail-queue.orig /var/qmail/bin/qscanq/qmail-queue
echo "Done!"
echo ""

exit
else
echo ""
echo "Error! The qscanq-psa spamassassin addon doesn't seem to be installed!"
echo ""
exit
fi
}

fixqmail() {
#TODO:
# check to see if qscanq-psa is installed and active, if not, check if qmail-fix is needed
# check to see if qscanq-psa-spam is installed
# check if qmail-queue.bkp exists, if it doesn't, ask user to provide it and copy it in place
echo ""
echo "NOT IMPLEMENTED YET!"
echo ""
}

help() {
echo ""
echo ""
echo "This script installs additional Spamassassin support to qscanq-psa."
echo ""
echo ""
echo "NOTE: The script is still in beta stage, please use it with care."
echo "It assumes it hasn't been renamed and that you're running it from"
echo "the directory $SCRIPTDIR"
echo "If this isn't correct, you'll need to edit it and change the two"
echo "variables at the top. You don't need to change anything else."
echo ""
echo "REQUIREMENTS: a working qscanq-psa install in ACTIVE use; a working"
echo "Spamassassin install with spamd running and with spamc in /usr/bin."
echo ""
echo "Version: $SCRIPTVER"
echo ""
}

version() {
echo ""
echo "$SCRIPTVER"
echo ""
}

case "$1" in
install)
  install
  ;;
uninstall)
  uninstall
  ;;
fixqmail)
  fixqmail
  ;;
help)
  help
  ;;
version)
  version
  ;;
*)
echo $"Usage: $0 {install|uninstall|fixqmail|help|version}"
exit 1
esac
