#!/bin/sh
#

#Pipe into bash if it's present

if [ "`bash --version 2>&1|grep 'GNU bash'`" != "" -a "$BASH_VERSION" = "" ]; then
    exec bash $0 $*
    exit
fi

umask 007

OLD_LANG="$LANG" 
LANG=C  
export LANG OLD_LANG

QS_VERSION=`grep 'my $VERSION' qmail-scanner-queue.template|cut -d= -f2|sed -e 's/\"//g' -e 's/\;//g'`

export QS_VERSION

echo "Building Qmail-Scanner $QS_VERSION..."

if [ "`id |grep root`" = "" ]; then
    cat<<EOF

Sorry, but this must be run as root, as several operations require - well 
root access :-)


EOF
    exit 1
fi

if [ "$SPAMC_HOST" != "" ]; then 
  SA_HN="-d $SPAMC_HOST" 
fi

VERSION=`grep '^# Version: ' qmail-scanner-queue.template`
VERSION=`echo $VERSION|awk '{print $NF}'`

SUPPORTED_SCANNERS="clamscan,clamdscan,sweep,sophie,vscan,trophie,uvscan,csav,antivir,kavscanner,AvpLinux,kavdaemon,AvpDaemonClient,fsav,fprot,inocucmd,vexira,verbose_spamassassin,fast_spamassassin"

SILENT_VIRUSES='klez,bugbear,hybris,yaha,braid,nimda,tanatos,sobig,winevar,palyh,fizzer,gibe,cailont,lovelorn,swen,dumaru,sober,hawawi,holar-i,mimail,poffer,bagle,worm.galil,mydoom,worm.sco,tanx,novarg,\@mm'

PWD=${PWD:-`pwd`}
TMPDIR=${TMPDIR:-/tmp}

QSLANG=${OLD_LANG:-en_GB}

if [ "`echo $QSLANG|egrep -i '^en_|^C$'`" != "" ]; then
    #reset all English to en_GB
    QSLANG="en_GB"
fi

if [ ! -d "./locale/$QSLANG" ]; then
    echo "

                ***** NOTE ******

Qmail-Scanner doesn't have language translations for $QSLANG, 
- so defaulting to english...

[Hit <RETURN> to continue]
"
    read ans
    QSLANG="en_GB"
fi

export QSLANG

#Let's grab the list of languages supported

LANGUAGES="`ls  -d locale/[a-z]*|sed 's?^locale\/??g'`"
LANGUAGES=`echo $LANGUAGES`

AS_QQ="/var/spool/qmailscan"
QMAILDIR="/var/qmail"
BINDIR="$QMAILDIR/bin"
#QMAILQUEUE_BIN="$BINDIR/qmail-queue"
ARCHIVEIT="0"
ARCHIVEDIR="archives"
REDUNDANT="no"
FIX_MIME="2"
DISABLE_EOL_CHECK="0"
DEBUG_LEVEL="1"
FORCE_UNZIP="0"
QUARANTINE_PASSWORD_PROTECTED="0"
DESCRIPTIVE_HEADERS="0"
NOTIFY_ADDRESSES="psender,nmlvadm"
USERNAME="$USER"
MANUAL_INSTALL="0"
INSTALLIT="0"
DONOTCONFIRM="0"
LOG_DETAILS="0"
LOG_CRYPTO="0"
SKIP_TEXT_MSGS="1"
QS_USER="qscand"
SKIP_SETUID_TEST=""

while [ -n "$1" ]
do
    case $1 in
        --qs-user) if [ "$2" != "" ] ; then  shift ; fi ; QS_USER="$1" ;; 
        --spooldir) if [ "$2" != "" ] ; then  shift ; fi ; AS_QQ="$1" ;;
	--qmaildir) if [ "$2" != "" ]; then shift ; fi ; QMAILDIR="$1" ;;
	--bindir) if [ "$2" != "" ] ; then  shift ; fi ; BINDIR="$1" ;;
	--user|--admin) if [ "$2" != "" ] ; then  shift ; fi ; USERNAME="$1" ;;
	--domain) if [ "$2" != "" ] ; then  shift ; fi ; MAILDOMAIN="$1" ;;
	--notify) if [ "$2" != "" ]; then shift ; fi ; NOTIFY_ADDRESSES="$1" ;;
	--batch) DONOTCONFIRM="1" ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; fi ;;
	--silent-viruses) if [ "$2" != "" ]; then shift ; fi ; FIND_SILENT_VIRUSES="$1" ;;
	--local-domains) if [ "$2" != "" ]; then shift ; fi ; LOCAL_DOMAINS_ARRAY="$1" ;;
	--lang) if [ "$2" != "" ]; then shift ; fi ; QSLANG="$1" ;;
	--debug)  if [ "$2" != "" ] ; then  shift ; fi ; DEBUG_LEVEL="$1" ;;
	--unzip) if [ "$2" != "" ] ; then shift ; fi ; FORCE_UNZIP="$1" ;;
	--block-password-protected)  if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; if [ "`echo $1|egrep -i '^0|^no'`" != "" ]; then QUARANTINE_PASSWORD_PROTECTED="0" ; else  if [ "`echo $1|egrep -i '^1|^yes'`" != "" ]; then QUARANTINE_PASSWORD_PROTECTED="1" ; else  QUARANTINE_PASSWORD_PROTECTED="1" ; fi ;  fi ; fi ;;
	--add-dscr-hdrs) if [ "$2" != "" ] ; then  shift ; fi ; DESCRIPTIVE_HEADERS="$1" ;;
	--scanners) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then shift ; fi ; FIND_SCANNERS="$1" ;;
	--skip-text-msgs) if [ "$2" != "" ] ; then  shift ; fi ; SKIP_TEXT_MSGS="$1" ;;
        --archive) ARCHIVEIT="1" ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; if [ "`echo $1|egrep -i '^0|^no'`" != "" ]; then ARCHIVEIT="0" ; else  if [ "`echo $1|egrep -i '^1|^yes'`" != "" ]; then ARCHIVEIT="1" ; else  ARCHIVEIT="$1" ; fi ;  fi ; fi ;;
	--redundant) REDUNDANT="no" ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; REDUNDANT="$1" ;  fi ;;
        --log-details) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; LOG_DETAILS="$1" ;  fi ;;
	--log-crypto) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; LOG_CRYPTO="$1" ;  fi ;;
        --fix-mime) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; if [ "`echo $1|egrep -i '^0|^no'`" != "" ]; then FIX_MIME="0" ; fi ;  if  [ "`echo $1|egrep -i '^[1-9]+$'`" != "" ]; then FIX_MIME="$1" ; fi ;  fi ;;
	--ignore-eol-check) DISABLE_EOL_CHECK=0 ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; if [ "`echo $1|egrep -i '^1|^yes'`" != "" ]; then DISABLE_EOL_CHECK=1 ; fi ; fi ;;
	--no-QQ-check) MANUAL_INSTALL="1";;
	--skip-setuid-test) SKIP_SETUID_TEST=1 ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; if [ "`echo $1|egrep -i '^1|^yes'`" != "" ]; then SKIP_SETUID_TEST="1" ; fi ;  if  [ "`echo $1|egrep -i '^[1-9]+$'`" != "" ]; then SKIP_SETUID_TEST="$1" ; fi ; if [ "`echo $1|egrep -i '^0|^no'`" != "" ]; then SKIP_SETUID_TEST="0" ; fi ; fi ;;
	--qmail-queue-binary) if [ "$2" != "" ] ; then  shift ; fi ; QMAILQUEUE_BIN="$1" ;;
        --mime-unpacker) if [ "$2" != "" ] ; then  shift ; fi ; MIME_UNPACKER="$1" ;;
	--install) INSTALLIT="1" ; if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; fi ;;
        *) cat <<EOF >&2

valid options:
  --qs-user <username>  User that Qmail-Scanner runs as (default: $QS_USER)
  --qmaildir <top of qmail> defaults to $QMAILDIR/
  --spooldir <spooldir> defaults to $AS_QQ/
  --bindir <installdir> where to install qmail-scanner-queue.pl
                        Defaults to /var/qmail/bin/
  --admin <username> user to Email alerts to (default: $USERNAME)
  --domain <domain name> "user"@"domain" makes up Email address
	 to Email alerts to.
  --scanners <list of installed content scanners>
				    Defaults to "auto" - will use
				    whatever scanners are found on system.
				    Use this option to override "auto" - set
				    to one or more of the following:

auto,none,$SUPPORTED_SCANNERS

				    Note the special-case "none". This
				    will disable all but the internal
				    perlscanner module.
				    
  --skip-text-msgs [yes|no]          Defaults to "yes" - Q-S will skip
                                     running any anti-virus scanners on
				     any messages it works out are text-only.
				     i.e. don't have any attachments.
				     Set to "no" if you want them to be scanned
				     anyway.

  --notify "none|sender|recips|admin|nmladm|nmlvadm|all" Defaults to "psender,nmlvadm". 
				     Comma-separated list (no spaces!)
				     of addresses to which alerts should
				     be sent to. "nmladm" means only
				     notify admin for "user infections", 
				     i.e. non-mailing-list mail.
				     "nmlvadm" is the same as nmladm - except
				     that it also doesn't notify for viral e-mails.
				     i.e. just "policy" quarantines get e-mails. This allows you to 
				     still notify people when an e-mail is blocked due to
				     a policy decision (such as blocking password-protected
				     zip files), but a message tagged as viral by an AV system
				     will *not* trigger notification.
				     Similarly, "psender" means notify the sender only if their
				     e-mail was blocked for policy reasons. i.e. if an AV system found 
				     a virus, then don't notify the sender as the address was probably
				     forged.
  --local-domains "one.domain,two.domain" Defaults to the 
				     value of the "--domain" setting.
				     Comma-separated list (no spaces!) 
				     of domains that are classified as
				     "local". This is needed to ensure 
				     alerts are only sent to local users
				     and not remote when '--notify "recips"'
				     is chosen. This will drastically
				     reduce the chance of alerts being
				     sent to mailing-lists.	     
  --silent-viruses "virus1,virus2"   Defaults to "auto".
				     This option allows you to tell 
				     Qmail-Scanner *not* to notify 
				     senders when it quarantines one
				     of these viruses. Viruses such 
				     as Klez alter the sender address
				     so that it has no relation to the
				     actual sender - so there's no point
				     in responding to Klez messages - it
				     just confuses people. The admin and
				     recips will still be notified as set
				     by "--notify".
                                     Use this option to override "auto".
                                     By default this is set to:
                                     $SILENT_VIRUSES
  --lang "$LANGUAGES"   
			    Defaults to $QSLANG.
  --archive [yes|no|regex] Defaults to "no". Whether to archive mail after
			   it as been processed. If "yes", all copies of 
			   processed mail will be moved into the maildir 
			   "$AS_QQ/$ARCHIVEDIR/". Any other string besides
			   "yes" and "no" will be treated as a REGEX. Only mail
			   from or to an address that contains that regex will
			   be archived. e.g. "jhaar|harry" or "\@our.domain".
			   Be careful with this option, a badly written regex
			   will cause Qmail-Scanner to crash.
  --redundant [yes|no]     Defaults to "no". Whether or not to let the scanners
			   also scan any zip files and the original "raw" Email
		           file.
  --log-details [yes|syslog|no] Whether or not to log to mailstats.csv/via 
                                syslog the attachment structure of every Email 
                                message. Off (no) by default.
  --fix-mime [yes|no|num]  Defaults to "yes". Whether or not to attempt to 
                          "fix" broken MIME messages before doing anything
                          else. Should be safe, but *may* break some 
                          strange, old mailers (none known yet).
  --ignore-eol-check [yes|no]   Defaults to "no". Making this "yes" stops Qmail-Scanner
                          from treating "\r" or "\0" chars in the headers of 
                          MIME mail messages as being suspicious enough to quarantine
                          mail over. Some sites receive so much broken e-mail that this
                          option has been created so that they can still receive such
                          messages without having to be as drastic as to "--fix-mime no"
                          - which disables all sorts of other good stuff. Use only if you
                          have to.

  --add-dscr-hdrs [yes|no|all]  Defaults to "no". This adds the now old-fashion
			    X-Qmail-Scanner headers to the message. "all" adds
			    the "rcpt to" headers too - this is a privacy hole.
  --debug    [yes|no]     Whether or not debugging is turned on. On (yes)
                          by default. Can be also set to a number. Numbers 
			  over 100 cause Q-S to not cleanup working files
			  - thus allowing for offline debugging...
  --unzip    [yes|no]     Whether or not to forcibly unzip all zip files. Off 
                          by default as most AV's do unzip'ping themselves.
  --block-password-protected [yes|no]  Defaults to "no". Setting this to "yes" allows
			  you to quarantine any incoming zip files that are password 
		          protected. This is primarily to stop viruses such as Bagle which
			  arrive within a password-protected zip file. 
  --batch                 Do not confirm configure information (mainly for scripting)
  --install               Create directory paths, install perl script,
                          and change ownerships to match.
  --mime-unpacker "reformime"        Defaults to reformime.

       ****************
         Rarely Used
       ****************

  --no-QQ-check           Do not check that the QMAILQUEUE patch is installed. 
                          This explicitly disables any "--install" reference 
                          as that is NOT POSSIBLE with a manual install.
			  Use ONLY IF YOU MUST. The QMAILQUEUE patch is REALLY
			  a GOOD THING!!!!

  --skip-setuid-test      don't test for setuid perl. Only of use for those wanting
			  to run the C-wrapper version.

  --qmail-queue-binary    Set this to the FULL PATH to the Qmail qmail-queue 
			  binary. This is only EVER set when doing a manual 
			  install.


This script must be run as root so it can detect problems with setuid
perl scripts! 

invalid option: $1

See above for the valid options

EOF
           exit 1 ;;
    esac
    shift
done

DD="`id $QS_USER 2>/dev/null`"

if [ "$?" -ne 0 -o "$DD" = "" ]; then
    cat<<EOF

Fatal Error: Qmail-Scanner must be installed and run as a separate
account.

Please create the username and group "$QS_USER" before continuing.

e.g.

groupadd $QS_USER 
useradd -c "Qmail-Scanner Account" -g $QS_USER  -s /bin/false $QS_USER

EOF
    exit 1
fi

#Reset these Qmail vars again so that any changes made during configure time 
#come out here too

QMAILQUEUE_BIN="${QMAILQUEUE_BIN:-$BINDIR/qmail-queue}"


if [ "$FIND_SILENT_VIRUSES" = "" -o "`echo $FIND_SILENT_VIRUSES|grep auto`" != "" ]; then
    FIND_SILENT_VIRUSES="$SILENT_VIRUSES"
fi

if [ "$FIND_SILENT_VIRUSES" != "" ]; then
    FIND_SILENT_VIRUSES="`echo $FIND_SILENT_VIRUSES|sed -e 's/\"//g'  -e 's/ //g'`"
    if [ "$FIND_SILENT_VIRUSES" != "" ]; then
	VLA=""
	for virus in `echo $FIND_SILENT_VIRUSES|sed 's/,/ /g'`
	do
	    VLA="$VLA,'$virus'"
	done
	FIND_SILENT_VIRUSES_ARRAY="`echo $VLA|sed 's/^,//g'`"
    fi
fi

if [ "$FIND_SCANNERS" = "" -o "`echo $FIND_SCANNERS|grep auto`" != "" ]; then
    FIND_SCANNERS="$SUPPORTED_SCANNERS"
    AUTO_FIND_SCANNERS=1
fi

if [ "`echo $FIND_SCANNERS|grep none`" != "" ]; then
    FIND_SCANNERS="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
fi


FIND_SCANNERS="_ $FIND_SCANNERS _"

if [ "$MANUAL_INSTALL" != "0" ]; then
    INSTALLIT="0"
fi

if [ "`echo $FORCE_UNZIP|egrep -i 'off|0|no' `" != "" ]; then
    FORCE_UNZIP=0
else
    FORCE_UNZIP=1
fi


if [ "`echo $SKIP_TEXT_MSGS|egrep -i '^1|^y'`" != "" ]; then
    SKIP_TEXT_MSGS="1"
else
    SKIP_TEXT_MSGS="0"
fi

if [ ! -d "./locale/$QSLANG" ]; then
    echo "

                ***** NOTE ******

Qmail-Scanner doesn't have language translations for $QSLANG, 
- so setting back to english...
"
    if [ "$DONOTCONFIRM" != "1" ]; then
      echo "[Hit <RETURN> to continue]
"
      read ans
    fi
    QSLANG="en_GB"
fi

export QSLANG


cat<<EOF

This script will search your system for the virus scanners it knows
about, and will ensure that all external programs
qmail-scanner-queue.pl uses are explicitly pathed for performance
reasons.

EOF

if [ "$INSTALLIT" != "1" ]; then 
 cat<<EOF

It will then generate qmail-scanner-queue.pl - it is up to you to install it
correctly.

EOF
fi
if [ "$DONOTCONFIRM" != "1" ]; then
 cat<<EOF
Continue? ([Y]/N)
EOF
 read ans
 if [ "`echo $ans|grep -i n`" != "" ]; then
    exit
 fi
fi
PATH="$PATH:$QMAILDIR/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/etc/iscan:/usr/local/uvscan:/usr/lib/AntiVir:/usr/lib/uvscan:/usr/local/av:/opt/AV:/opt/AVP:/usr/local/f-prot:/usr/local/rav8/bin:/usr/lib/Vexira" export PATH

if [ "$MIME_UNPACKER" != "" ]; then
	if [ "`echo $MIME_UNPACKER |egrep -v '^(reformime|ripmime)$'`" != "" ]; then
	    cat<<EOF


                *************************

Sorry, but your "--mime-unpacker" definition contains invalid options. 

The only valid options are:

reformime
ripmime

               **************************
EOF
	    exit
	fi
else
	MIME_UNPACKER="reformime"
fi

NOTIFY_ADDRESSES="`echo $NOTIFY_ADDRESSES|sed -e 's/\"//g'  -e 's/ //g'`"

if [ "$NOTIFY_ADDRESSES" = "all" ]; then
    NOTIFY_ADDRESSES="sender,admin,recips"
fi

if [ "$NOTIFY_ADDRESSES" ]; then
     for addr in `echo $NOTIFY_ADDRESSES|sed 's/,/ /g'`
     do
	if [ "`echo $addr|egrep -v '^(admin|nmladm|nmlvadm|none|sender|psender|recips|all)$'`" != "" ]; then
	    cat<<EOF


                *************************

Sorry, but your "--notify" definition contains invalid options. 

The only valid options are:

none
admin
nmladm
nmlvadm
sender
psender
recips
all

               **************************
EOF
	    exit
	fi
     done
fi



#Check out command line

SCANNERS=`echo $FIND_SCANNERS|sed -e 's/^_ //' -e 's/ _$//'`

if [ "$SCANNERS" = "$SUPPORTED_SCANNERS" ]; then
    SCANNERS="auto"
fi

FIND_SCANNERS=" `echo $FIND_SCANNERS|sed 's/,/ /g'` "

rm -f 1234.* 2> /dev/null
DD="`mktemp -d 1234.XXXXXX  2>&1`"
if [ "`ls 1234.* 2>/dev/null`" != "" ]; then
    if test -d "$DD" ; then
	TMP_DIR="`mktemp -d /tmp/mkt_qs.XXXXXX`"
    fi
fi
if [ "$TMP_DIR" = "" ]; then
    TMP_DIR="/tmp/mkt_qs.$$-`date +%s`"
    mkdir $TMP_DIR
fi

rm -rf 1234* "$DD"

TMPFILE="$TMP_DIR/tempfile"
touch $TMPFILE
chmod -R 755 $TMP_DIR/

#Let's create a test EICAR virus to ensure
#the installed virus scanners are working
echo  'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*' > $TMP_DIR/eicar.com
chown $QS_USER:$QS_USER $TMP_DIR/eicar.com
chmod 644  $TMP_DIR/eicar.com


for dir in `echo $PATH|sed 's/:/ /g'`
do
    if test -x $dir/perl
    then
	PERLRELEASE_DETAILS=${PERLRELEASE_DETAILS:-`$dir/perl -V 2>&1|grep perl5 |head -1`}
	if [ "$?" = "0" ]; then	
	    PERL5="${PERL5:-$dir/perl}"
	fi
    fi    
    if test -x $dir/suidperl
    then
	SUIDPERL="${SUIDPERL:-$dir/suidperl}"
    fi
    if test -x $dir/rm ; then RM_BINARY="${RM_BINARY:-$dir/rm}" ; fi
    if test -x $dir/grep ; then GREP_BINARY="${GREP_BINARY:-$dir/grep}" ; fi
    if test -x $dir/hostname
    then
	HOST="${HOST:-`$dir/hostname`}"
    fi
    if test -x $dir/uname
    then
	UNAME="${UNAME:-`$dir/uname -n`}"
    fi

    if test -x $dir/qmail-inject
    then
	QMAILINJECT_BIN="${QMAILINJECT_BIN:-$dir/qmail-inject}"
    fi

    if test -x $dir/qmail-queue
    then
	QMAILQUEUE_BIN="${QMAILQUEUE_BIN:-$dir/qmail-queue}"
    fi

    if test -x $dir/qmail-smtpd
    then
	QMAILSMTPD="${QMAILSMTPD:-$dir/qmail-smtpd}"
    fi

    if test -x $dir/setuidgid
    then
	SETUIDGID="${SETUIDGID:-$dir/setuidgid}"
    fi
    if test -x $dir/strings
    then
	STRINGS="${STRINGS:-$dir/strings}"
    fi

if [ "$MIME_UNPACKER" = "reformime" ]; then
    if test -x $dir/reformime
    then
	UNMIME_BINARY="${UNMIME_BINARY:-$dir/reformime}"
        DD=`reformime -s1.2 -xTEST- < ./contrib/reformime-test.eml`
	if [ "`grep hello TEST-hello.txt`" = "" ]; then
	   echo "** FATAL ERROR ***"
	   echo ""
	   echo "$UNMIME_BINARY contains bugs. Please upgrade to a release"
	   echo "that post-dates Mar 22 2002 (e.g. 1.3.8)"
	   echo ""
	   rm -f TEST-hello.txt
	   exit 1
	fi
	rm -f TEST-hello.txt
    fi    
    if test -x $dir/maildrop
    then
	MAILDROP_BINARY="${MAILDROP_BINARY:-$dir/maildrop}"
    fi    
    #Note that TNEF is only defined if using reformime as
    #ripmime does TNEF support internally
    if test -x $dir/tnef
    then
	TNEF_BINARY="${TNEF_BINARY:-$dir/tnef}"
	#There's a LOCALE bug in uudecode - workaround...
	if [ "`$TNEF_BINARY --help 2>&1|grep number-backups`" = "" ]; then
	    TNEF_BINARY=""
	    cat<<EOF

Old tnef binary found on your system!

Please upgrade ASAP to a version supporting the "--number-backups" option.

EOF
	    exit 1
	fi
    fi
fi
if [ "$MIME_UNPACKER" = "ripmime" ]; then
    if test -x $dir/ripmime
    then
	UNMIME_BINARY="${UNMIME_BINARY:-$dir/ripmime}"
        DD=`ripmime -i - < ./contrib/reformime-test.eml`
	if [ "`grep hello hello.txt`" = "" ]; then
	   echo "** FATAL ERROR ***"
	   echo ""
	   echo "$UNMIME_BINARY contains bugs. Please upgrade to a newer release."
	   echo ""
	   rm -f hello.txt textfile*
	   exit 1
	fi
	rm -f hello.txt textfile*
    fi
fi
    if test -x $dir/unzip
    then
	UNZIP_BINARY="${UNZIP_BINARY:-$dir/unzip}"
	#Now check for password support
	touch file.zip
	UNZIP_PASSWD="xx${RANDOM}$$xx"
	DD=`unzip -P$UNZIP_PASSWD file.zip 2>&1|grep '^Archive:'`
	if [ "$DD" != "" ]; then
	 UNZIP_OPTIONS="-P$UNZIP_PASSWD"
	else
	 UNZIP_OPTIONS=""
	fi
	rm -f file.zip
    fi    
    
    if test -x $dir/uvscan 
    then
	if [ "`echo $FIND_SCANNERS|grep ' uvscan '`" != "" ]; then
	    if [ "`$dir/uvscan -r --secure --fam --unzip --macro-heuristics -v $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		UVSCAN="${UVSCAN:-$dir/uvscan}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
uvscan"
	    fi
	fi
    fi    
    if test -x $dir/csav
    then
	if [ "`echo $FIND_SCANNERS|grep ' csav '`" != "" ]; then
	    if [ "`$dir/csav -list -nomem -packed -archive -noboot $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		CSAV="${CSAV:-$dir/csav}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
csav"
	    fi
	fi
    fi
    if [ "`echo $FIND_SCANNERS|grep ' sophie '`" != "" ]; then
    if test -x $dir/sophie
    then
	SOCKET="`$dir/sophie -d -f README 2>&1|grep 'Socket path'|awk '{print $NF}'|sed 's/\"//g'`"	
	if [ "$SOCKET" != "" ]; then
	    DD=
	    if [ "`perl ./contrib/test-sophie.pl -s $SOCKET -f $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		SOPHIE="${SOPHIE:-$dir/sophie}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
sophie"
		SSOCKET="$SOCKET"
	    else 
	        echo "
Something like sophie for Sophos detected - but not correctly installed. 
Please read Q-S FAQ if you want it - especially check that sophie daemon 
can read files owned by $QS_USER.
".
	    fi
	fi
    fi
    fi
    if [ "`echo $FIND_SCANNERS|egrep ' (sophie|sweep) '`" != "" ]; then
    if test -x $dir/sweep
    then
	if [ "`$dir/sweep -h 2>&1|grep LAM`" = "" ]; then
	    if [ "`$dir/sweep -f -eec -all -sc -nc -ss -nb -archive $TMP_DIR  2>&1|egrep -i 'virus|test'`" != "" ]; then
		SWEEP="${SWEEP:-$dir/sweep}"	    
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
sweep"
	    fi
	    if [ "$SAV_IDE" = "" ]; then
		if [ ! -f "/etc/sav.conf" ]; then
		  if [ -d "/usr/local/sophos" ]; then
		    SAV_IDE="/usr/local/sophos"
		  fi
		  if [ -d "/usr/local/sav" ]; then
		    SAV_IDE="/usr/local/sav"
		  fi
		else 
		    SAV_IDE_CONF="1"
		fi
	    fi
	fi
    fi    
    fi
    if [ "`echo $FIND_SCANNERS|grep ' trophie '`" != "" ]; then
    if test -x $dir/trophie
    then
	SOCKET="`$dir/trophie -d -f README 2>&1|grep 'Socket path'|awk '{print $NF}'|sed 's/\"//g'`"
	if [ "$SOCKET" != "" ]; then
	    if [ "`perl ./contrib/test-trophie.pl -s $SOCKET -f $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		TROPHIE="${TROPHIE:-$dir/trophie}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
trophie"
		TSOCKET="$SOCKET"
	    else 
	        echo "
Something like trophie for Trend detected - but not correctly installed. 
Please read Q-S FAQ if you want it - especially check that trophie daemon 
can read files owned by $QS_USER.
".
	    fi
	fi
    fi
    fi
    if [ "`echo $FIND_SCANNERS|egrep ' (vscan|trophie) '`" != "" ]; then
    if test -x $dir/vscan
    then
	if [ "`$dir/vscan -p/etc/iscan/ -za -a -u -nl -v $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	    INSTALLED_SCANNERS="$INSTALLED_SCANNERS
vscan"
	    ISCAN="${ISCAN:-$dir/vscan}"
	fi
    fi    
    fi
    if [ "`echo $FIND_SCANNERS|grep ' antivir '`" != "" ]; then
    if test -x $dir/antivir
    then
	if [ "`$dir/antivir -allfiles -s -tmp. -z -v $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	    HBEDV="${HBEDV:-$dir/antivir}"
	    HBEDV_OPTIONS="-allfiles -s -tmp. -z -v"
	    INSTALLED_SCANNERS="$INSTALLED_SCANNERS
antivir"
	elif [ "`$dir/antivir -allfiles -s -tmp. -v $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	    HBEDV="${HBEDV:-$dir/antivir}"
	    HBEDV_OPTIONS="-allfiles -s -tmp. -v"
	    INSTALLED_SCANNERS="$INSTALLED_SCANNERS
antivir"
	fi
    fi
    fi
    if test -x $dir/kavscanner
    then
	if [ "`echo $FIND_SCANNERS|grep ' kavscanner '`" != "" ]; then
	    if [ "`$dir/kavscanner -Y -P -B -MP -MD -* $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		AVPSCAN="${AVPSCAN:-$dir/kavscanner}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
kavscanner"
	    fi
	fi
    else 
        if test -x $dir/AvpLinux
	then
	    if [ "`echo $FIND_SCANNERS|grep ' AvpLinux '`" != "" ]; then
		if [ "`$dir/AvpLinux -Y $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		    AVPSCAN="${AVPSCAN:-$dir/AvpLinux}"
		    INSTALLED_SCANNERS="$INSTALLED_SCANNERS
AvpLinux"
		fi
	    fi
	fi   
    fi
    if test -x $dir/kavdaemon
    then
       if [ "`echo $FIND_SCANNERS|grep ' kavdaemon '`" != "" ]; then
	if test -f "sub-avpdaemon.pl"
	then
	    if [ "`$dir/kavdaemon $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		AVPSCAN=""
		AVPDAEMON="${AVDAEMON:-$dir/kavdaemon}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
kavdaemon"
	    fi
	fi
       fi
    else 
       if test -x $dir/AvpDaemonClient
       then
	    if [ "`echo $FIND_SCANNERS|grep ' AvpDaemonClient '`" != "" ]; then
		if test -f "sub-avpdaemon.pl"
		then
		    if [ "`$dir/AvpDaemonClient $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
			AVPSCAN=""
			AVPDAEMON="${AVDAEMON:-$dir/AvpDaemonClient}"
			INSTALLED_SCANNERS="$INSTALLED_SCANNERS
AvpDaemonClient"
		    fi
		fi
	    fi
       fi
    fi
    if test -x $dir/fsav
    then
        if [ "`echo $FIND_SCANNERS|grep ' fsav '`" != "" ]; then
	    if [ "`$dir/fsav --list --archive --auto --dumb $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		FSECURE="${FSECURE:-$dir/fsav}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
fsav"
	    fi
	fi
    fi    
    if test -x $dir/f-prot
    then
        if [ "`echo $FIND_SCANNERS|grep ' fprot '`" != "" ]; then
	    if [ "`$dir/f-prot  -ai -archive -dumb -list $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		FPROT="${FPROT:-$dir/f-prot}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
fprot"
	    fi
	fi
    fi
    if test -x $dir/inocucmd
    then
        if [ "`echo $FIND_SCANNERS|grep ' inocucmd '`" != "" ]; then
	    if [ "`$dir/inocucmd  -SEC -NEX $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		INOCUCMD="${INOCUCMD:-$dir/inocucmd}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
inocucmd"
	    fi
	fi
    fi  
#    if test -x $dir/ravav
#    then
#	if [ "`echo $FIND_SCANNERS|grep ' ravlin '`" != "" ]; then
#	   if [ "`$dir/ravav --mail --archive --heuristics=on --all $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
#	      RAVLIN="${RAVLIN:-$dir/ravav}"
#	      INSTALLED_SCANNERS="$INSTALLED_SCANNERS
#ravlin"
#	   fi
#	fi
#    fi
    if test -x $dir/vexira
    then
	if [ "`echo $FIND_SCANNERS|grep ' vexira '`" != "" ]; then
	   if [ "`$dir/vexira --allfiles  -s -z -nolnk -noboot -nombr -nodef -r1 $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	      VEXIRA="${VEXIRA:-$dir/vexira}"
	      INSTALLED_SCANNERS="$INSTALLED_SCANNERS
vexira"
	   fi
	fi
    fi
    if [ "`echo $FIND_SCANNERS|grep ' clamdscan '`" != "" ]; then
    if test -x $dir/clamdscan
    then
	DD="`$dir/clamdscan -v $TMP_DIR 2>&1`|egrep -i 'virus|test'"	
	if [ "$DD" != "" ]; then
	    CLAMDSCAN="${CLAMDSCAN:-$dir/clamdscan}"
	    INSTALLED_SCANNERS="$INSTALLED_SCANNERS
clamdscan"
	else 
	    echo "
Something like clamdscan for ClamAV detected - but not correctly installed. 
Please read Q-S FAQ if you want it - especially check that clamd daemon 
can read files owned by $QS_USER (i.e. make it run as $QS_USER).
".
	fi
    fi
    fi
    if test -x $dir/clamscan
    then
	if [ "`echo $FIND_SCANNERS|grep ' clamscan '`" != "" ]; then
	   if [ "`$dir/clamscan -v $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	      CLAMSCAN="${CLAMSCAN:-$dir/clamscan}"
	      INSTALLED_SCANNERS="$INSTALLED_SCANNERS
clamscan"
	   fi
	fi
    fi
    if [ "`echo $FIND_SCANNERS|grep spamassassin`" != "" -a "$SPAMASSASSIN_BINARY" = "" ]; then
    if test -x $dir/spamassassin
    then
	SPAMASSASSIN_BINARY="${SPAMASSASSIN_BINARY:-$dir/spamassassin}"
    fi
    if [ -x "$dir/spamc" -a "$SPAMC_BINARY" = "" ]
    then
	#Test it out
	if [ "`$dir/spamc -h|grep 'f: fallback'`" != "" ] ;then
	    SPAMC_BINARY="${SPAMC_BINARY:-$dir/spamc}"
            DD="`$SPAMC_BINARY < ./contrib/spamc-nice.eml`"
            if [ "`echo $DD|grep '^From '`" != "" ]; then
                cat<<EOF

SpamAssassin's spamd daemon is incorrectly installed. You need to run it as:

spamd -F 0 ....

Please try again.

EOF
                 exit
            fi
	    DD="`$SPAMC_BINARY $SA_HN < ./contrib/spamc-nice.eml`"
	    if [ "$?" != "0" -o "`echo $DD|grep 'X-Spam-Status: No'`" = "" ]; then
		echo "
Something like spamc for SpamAssassin detected - but not correctly installed 
(didn't include a \"X-Spam-Status\" line in output). 
Please read Q-S FAQ if you want it - especially check that spamd daemon 
is running. Ignoring...
"
		SPAMC_BINARY=''
	    else
	     DD="`$SPAMC_BINARY $SA_HN < ./contrib/spamc-nasty.eml`"
	     if [ "$?" != "0" -o "`echo $DD|grep 'X-Spam-Status: Yes'`" = "" ]; then
		echo "
Something like spamc for SpamAssassin detected - but not correctly installed 
(didn't include a \"X-Spam-Status: Yes\" line in output) - ignoring...
"
		SPAMC_BINARY=''
	     else
		DD="`$SPAMC_BINARY $SA_HN -c -f < ./contrib/spamc-nasty.eml`"
		if [ "`echo $DD|tail -1|grep /`" != "" ]; then
		    SPAMASSASSIN_VERSION="fast_spamassassin"
		fi
	     fi
	    fi
	    if [ "$SPAMC_BINARY" != "" ]; then
		if [ "`echo $FIND_SCANNERS|grep ' fast_spamassassin'`" != "" ]; then
		  if [ "$SPAMASSASSIN_VERSION" != "fast_spamassassin" ]; then
			cat<<EOF
 Fatal: You have asked for the "fast" v2.1 SpamAssassin support, however
your system is NOT correctly configured for it.

Either upgrade and try again, or reconfigure for the older "verbose"
SpamAssassin support.

EOF
			exit
		  fi
		        SPAMC_OPTIONS="$SA_HN -c -f"
			INSTALLED_SCANNERS="$INSTALLED_SCANNERS
fast_spamassassin"
			#See if they want fast_spamassassin to alter the 
			#Subject: line too...
			SPAMC_SUBJECT="`echo $FIND_SCANNERS|grep ' fast_spamassassin='|sed -e 's/^.*fast_spamassassin=//g' -e 's/ .*$//g'`"
		else
			SPAMASSASSIN_VERSION="verbose_spamassassin"
			SPAMC_OPTIONS="$SA_HN -f"
			INSTALLED_SCANNERS="$INSTALLED_SCANNERS
verbose_spamassassin"
		fi
		else
			SPAMC_BINARY=''
		fi
	    fi
	fi
    fi
    if test -x $dir/find
    then
	FIND="${FIND:-$dir/find}"
    fi
    if test -x $dir/egrep
    then
	GREP="${GREP:-$dir/grep}"
    fi
    if test -x $dir/uudecode
    then
	UUDECODE_BINARY="${UUDECODE_BINARY:-$dir/uudecode}"
    fi
done

if [ "$FIND_SILENT_VIRUSES" = "$SILENT_VIRUSES" ]; then
    SILENT_VIRUSES="auto"
fi
if [ "$UNAME" ] ; then FQDN="$UNAME" ; fi
if [ "$HOST" ] ; then FQDN="$HOST" ; fi

MAILDOMAIN=${MAILDOMAIN:-$FQDN}
LOCAL_DOMAINS_ARRAY=${LOCAL_DOMAINS_ARRAY:-$MAILDOMAIN}
CMDLINE="$0 --spooldir $AS_QQ --qmaildir $QMAILDIR --bindir $BINDIR --qmail-queue-binary $QMAILQUEUE_BIN  --admin $USERNAME --domain $MAILDOMAIN --notify $NOTIFY_ADDRESSES --local-domains $LOCAL_DOMAINS_ARRAY --silent-viruses $SILENT_VIRUSES --lang $QSLANG --debug $DEBUG_LEVEL --unzip $FORCE_UNZIP --block-password-protected $QUARANTINE_PASSWORD_PROTECTED --add-dscr-hdrs $DESCRIPTIVE_HEADERS --archive $ARCHIVEIT --redundant $REDUNDANT --log-details $LOG_DETAILS --log-crypto $LOG_CRYPTO --fix-mime $FIX_MIME  --ignore-eol-check $DISABLE_EOL_CHECK --scanners \"$SCANNERS\""


if [ "$MANUAL_INSTALL" = "1" ]; then
    CMDLINE="$CMDLINE --no-QQ-check $MANUAL_INSTALL"
fi
if [ "$INSTALLIT" = "1" ]; then
    CMDLINE="$CMDLINE --install $INSTALLIT"
fi

#echo "configure called as: $CMDLINE"


INSTALLED_SCANNERS="`echo \"$INSTALLED_SCANNERS\"|sort|uniq`"
INSTALLED_SCANNERS=`echo $INSTALLED_SCANNERS`
INSTALLED_SCANNERS=" $INSTALLED_SCANNERS "

#First off, check that the QMAILQUEUE patch is in place:


if [ "$STRINGS" != "" -a "$QMAILSMTPD" != "" -a "$MANUAL_INSTALL" = "0" ]; then
    DD=`$STRINGS -a $QMAILSMTPD|grep QMAILQUEUE`
    if [ "$DD" = "" ]; then
    cat<<EOF

               **************************

Cannot find evidence of QMAILQUEUE patch in $QMAILSMTPD!

This package REQUIRES that Qmail patch in order to operate.

Please read the README.html file again and download and install the patch
before continuing...

               **************************

EOF
    exit

    fi
fi

if [ "$PERL5" = "" ]; then
    cat<<EOF

               **************************

Cannot find perl 5 on your system!

This package is a perl script - it won't get far without perl5 on your system...

               **************************

EOF
    exit
fi

DD=`$PERL5 -e 'use Time::HiRes;' 2>&1`
if [ "$?" != "0" ]; then
    cat<<EOF

               **************************

 perl doesn't have Time::HiRes module - cannot continue.

Get it from CPAN:

http://search.cpan.org/search?mode=module&query=time%3A%3Ahires

               **************************

Error reported was:

---
$DD
---
EOF
    exit
fi

DD=`$PERL5 -e 'use DB_File;' 2>&1`
if [ "$?" != "0" ]; then
    cat<<EOF

               **************************

 perl doesn't have DB_File module - cannot continue.

Get it from CPAN:

http://search.cpan.org/search?module=DB_File

               **************************


Error reported was:

---
$DD
---
EOF
    exit
fi

if [ "$MIME_UNPACKER" = "reformime" ]; then
#Check version of maildrop to ensure it's not the buggy version

DD="`$MAILDROP_BINARY -v 2>&1|grep '^maildrop'|grep '1\.0'`"
if [ "$DD" != "" ]; then
    cat<<EOF

               **************************

 You have a known bad version of maildrop (1.0). It is buggy and doesn't
work!

Please upgrade to a later release:

http://download.sourceforge.net/courier/

               **************************

EOF
    exit
fi
fi

#Find out if their uudecoder is supported
if [ "$UUDECODE_BINARY" != "" ]; then
if [ "`$UUDECODE_BINARY -h 2>&1|$GREP '\-o'`" != "" ]; then
    UUDECODE_PIPE="-o -"
elif [ "`$UUDECODE_BINARY -h  2>&1|$GREP '\-p'`" != "" ]; then
    UUDECODE_PIPE="-p"
elif [ "`$UUDECODE_BINARY -h  2>&1|$GREP 'cip'`" != "" ]; then 
    UUDECODE_PIPE="-p"
elif [ "`$UUDECODE_BINARY --help 2>&1|$GREP '\-o'`" != "" ]; then
    UUDECODE_PIPE="-o -"
elif [ "`$UUDECODE_BINARY --help  2>&1|$GREP '\-p'`" != "" ]; then
    UUDECODE_PIPE="-p"
fi


#What was this here for? DELETE
#if [ "$UUDECODE_PIPE" = "" ]; then
#    UUDECODE_PIPE="-xxx-junk--here"
#fi

#Now check if uudecode is useful

testfile="qmail-scan-$$.tst"
echo 'begin 664 $testfile
%=&5S=`H`
`
end
'> test-uudecode.uue
DD=`$UUDECODE_BINARY $UUDECODE_PIPE test-uudecode.uue > test-uudecode.tst`
if [ "$?" = "0" ] ; then
    if [ -f test-uudecode.tst -a ! -f $testfile ]; then
	#uudecode is good!
	echo "

$UUDECODE_BINARY works as expected on system...

"
    else
	UUDECODE_BINARY=''
	echo "

broken uudecoder on your system - cannot use uudecode component

"
    fi
else
	UUDECODE_BINARY=''
    	echo "

broken uudecoder on your system - cannot use uudecode component

"
fi

rm -f test-uudecode.tst $testfile test-uudecode.uue

#Finish UUDECODE tests
fi

#Carry on...



if [ "`echo $LOCAL_DOMAINS_ARRAY|grep $MAILDOMAIN`" = "" ]; then
    LOCAL_DOMAINS_ARRAY="$MAILDOMAIN,$LOCAL_DOMAINS_ARRAY"
fi


LOCAL_DOMAINS_ARRAY="`echo $LOCAL_DOMAINS_ARRAY|sed -e 's/\"//g'  -e 's/ //g'`"

if [ "$LOCAL_DOMAINS_ARRAY" ]; then
     LDA=""
     for dom in `echo $LOCAL_DOMAINS_ARRAY|sed 's/,/ /g'`
     do
	LDA="$LDA,'$dom'"
     done
     LOCAL_DOMAINS_ARRAY="`echo $LDA|sed 's/^,//g'`"
fi

if [ "$MIME_UNPACKER" = "reformime" ]; then
if [ "$UNMIME_BINARY" = "" ]
then
    cat<<EOF

               **************************

 Cannot find reformime on your system!

This package relies on reformime (part of the maildrop package)
 to unpack MIME mail messages, please install it from
http://www.flounder.net/~mrsam/maildrop/

               **************************

EOF
    exit
fi
fi
if [ "$UNZIP_BINARY" = "" ]; then
    cat<<EOF

               **************************

Cannot find unzip on your system!

As this is probably a virus scanning package, there's not much point
in trying to detect PC-type viruses when you can't even unpack the
primary file type they arrive in...


               **************************

EOF
    exit
fi

if [ "$TNEF_BINARY" != "" ]; then
    cat<<EOF
    
Found tnef on your system! That means we'll be able to decode stupid
M$ attachments :-)

EOF
fi

echo "
The following binaries and scanners were found on your system:
"
if [ "$UNMIME_BINARY" != "" ]
then 
    echo "mimeunpacker=$UNMIME_BINARY"
    MIMEUNPACKER="$UNMIME_BINARY "
fi
if [ "$UUDECODE_BINARY" != "" ] 
then
    echo "uudecode=$UUDECODE_BINARY"
fi
if [ "$UNZIP_BINARY" != "" ] 
then
    echo "unzip=$UNZIP_BINARY"
fi
if [ "$TNEF_BINARY" != "" ] 
then
    echo "tnef=$TNEF_BINARY"
fi

echo ""
echo "Content/Virus Scanners installed on your System"
echo ""
if [ "$CLAMSCAN" != "" -a "$CLAMDSCAN" = "" ]; then
    echo "clamscan=$CLAMSCAN";
    SCANNER_ARRAY="$SCANNER_ARRAY,\"clamscan_scanner\""
fi

if [ "$CLAMDSCAN" != "" ]; then
    echo "clamdscan=$CLAMDSCAN (which means clamscan won't be used as clamdscan is better)";
    SCANNER_ARRAY="$SCANNER_ARRAY,\"clamdscan_scanner\""
fi

if [ "$ISCAN" != "" -a "$TROPHIE" = "" ]; then 
    
    echo "vscan=$ISCAN" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"iscan_scanner\""
fi
if [ "$CSAV" != "" ]; then 
    echo "csav=$CSAV" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"csav_scanner\""
fi
if [ "$TROPHIE" != "" ]; then 
    echo "trophie=$TROPHIE (which means vscan won't be used as trophie is better)" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"trophie_scanner\""
fi
if [ "$UVSCAN" != "" ]; then 
    echo "uvscan=$UVSCAN" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"uvscan_scanner\""
fi
if [ "$SWEEP" != "" -a "$SOPHIE" = "" ]; then 
    echo "sweep=$SWEEP" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"sweep_scanner\""
fi
if [ "$SOPHIE" != "" ]; then 
    echo "sophie=$SOPHIE (which means sweep won't be used as sophie is better)" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"sophie_scanner\""
fi

if [ "$HBEDV" != "" ]; then 
    DD=''
    if [ "`echo $HBEDV_OPTIONS|grep '\-z'`" = "" ]; then
     DD="(NOTE: demo mode: no archive unpacking!!!)"
    fi
    echo "hbedv_antivir=$HBEDV $DD" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"hbedv_scanner\""
fi

if [ "$AVPSCAN" != "" ]; then 
    echo "avp_antivir=$AVPSCAN" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"avp_scanner\""
fi

if [ "$AVPDAEMON" != "" ]; then 
    echo "avpdaemon_antivir=$AVPDAEMON" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"avpdaemon_scanner\""
fi

if [ "$FPROT" != "" ]; then
    echo "fprot=$FPROT"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"fprot_scanner\""
fi

if [ "$FSECURE" != "" ]; then 
    echo "fsecure=$FSECURE" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"fsecure_scanner\""
fi

if [ "$INOCUCMD" != "" ]; then 
    echo "inoculate=$INOCUCMD" 
    SCANNER_ARRAY="$SCANNER_ARRAY,\"inocucmd_scanner\""
fi

if [ "$RAVLIN" != "" ]; then
    echo "ravlin=$RAVLIN"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"ravlin_scanner\""
fi

if [ "$VEXIRA" != "" ]; then
    echo "vexira=$VEXIRA"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"vexira_scanner\""
fi

if [ "$SPAMC_BINARY" != "" ]; then
    if [ "$SPAMASSASSIN_VERSION" = "fast_spamassassin" ]; then
	echo "fast_spamassassin=$SPAMC_BINARY"
	SCANNER_ARRAY="$SCANNER_ARRAY,\"fast_spamassassin\""
    else 
	echo "verbose_spamassassin=$SPAMC_BINARY"
	SCANNER_ARRAY="$SCANNER_ARRAY,\"verbose_spamassassin\""
    fi
fi

USERNAME=${USERNAME:-$LOGNAME}

SCANNER_ARRAY=`echo $SCANNER_ARRAY|sed 's/^,//g'`

echo ""
echo "Qmail-Scanner details."
echo ""

if [ "`echo $LOG_DETAILS|egrep -i 'yes|^1|^y|on|true|syslog'`" != "" ]; then
    if [ "`echo $LOG_DETAILS|egrep -i '^1|yes|^y|on|true'`" != "" ]; then
	LOG_DETAILS="mailstats.csv"
    else
	LOG_DETAILS="syslog"
        DD=`$PERL5 -e 'use Sys::Syslog;' 2>&1`
        if [ "$?" != "0" ]; then
	  cat<<EOF

               **************************

 perl doesn't have Sys::Syslog module - therefore cannot configure with the
 "--log-details yes" option.

Your version of perl is too old to support this feature. Either upgrade or
configure again with "--log-details no".

               **************************

EOF
	  exit
        fi
     fi
else
   LOG_DETAILS=0
fi

if [ "$LOG_DETAILS" = "0" ]; then
    LOG_DETAILS="0"
    LOG_MSG_SUB="
sub log_msg() {};
"
fi


if [ "`echo $DEBUG_LEVEL|egrep -i '^no|^0'`" != "" ]; then
    DEBUG_LEVEL="0"
else
    if  [ "`echo $DEBUG_LEVEL|egrep '^[0-9]+$'`" = "" ]; then
       DEBUG_LEVEL="1"
    fi
fi

if [ "$LOG_DETAILS" != "" ]; then
    echo "log-details=$LOG_DETAILS"
fi
if [ "$FIX_MIME" != "" ]; then
    echo "fix-mime=$FIX_MIME"
fi
if [ "$DISABLE_EOL_CHECK" != "" ]; then
    echo "ignore-eol-check=$DISABLE_EOL_CHECK"
fi
if [ "$DEBUG_LEVEL" != "" ]; then
    echo "debug=$DEBUG_LEVEL"
fi
if [ "$NOTIFY_ADDRESSES" != "" ]; then
    echo "notify=$NOTIFY_ADDRESSES"
fi
if [ "$REDUNDANT" != "" ]; then
    echo "redundant-scanning=$REDUNDANT" 
fi
if [ "$ARCHIVEIT" != "0" ]; then
    if [ "$ARCHIVEIT" = "1" ]; then 
     ASTRING="everything"
    else 
     ASTRING="($ARCHIVEIT) only"
    fi
    echo "archiving $ASTRING into $AS_QQ/$ARCHIVEDIR/"
fi
echo "virus-admin=$USERNAME@$MAILDOMAIN"
echo "local-domains=$LOCAL_DOMAINS_ARRAY"
echo "silent-viruses=$FIND_SILENT_VIRUSES_ARRAY"
echo "scanners=$SCANNER_ARRAY"

SCANNER_ARRAY="`echo $SCANNER_ARRAY|sed -e 's/fast_spamassassin/spamassassin/g' -e 's/verbose_spamassassin/spamassassin/g'`"
cat<<EOF

If that looks correct, I will now generate qmail-scanner-queue.pl
for your system...
EOF
if [ "$DONOTCONFIRM" != "1" ]; then
 cat<<EOF
Continue? ([Y]/N) 
EOF
 read ans 
 if [ "`echo $ans|grep -i n`" != "" ]; then
    exit
 fi
fi

if [ "`echo $REDUNDANT|egrep -i '1|yes|^y|on|true'`" != "" ]; then
    REDUNDANT=1
else
    REDUNDANT=0
fi


if [ "`echo $DESCRIPTIVE_HEADERS|egrep -i '1|yes|^y|on|true'`" != "" ]; then
    DESCRIPTIVE_HEADERS="1"
elif [ "`echo $DESCRIPTIVE_HEADERS|egrep -i 'all'`" != "" ]; then
    DESCRIPTIVE_HEADERS="2"
else
    DESCRIPTIVE_HEADERS="0"
fi


#Gather info about OS this is been run on.

HOST_OS=`uname -s`
HOST_RELEASE=`uname -r`
HOST_HARDWARE=`uname -m`

#Gather up locale info for translations

> .locale_vars
for i in ./locale/$QSLANG/*
do
    name=`basename $i`
    expr LOCALE_$name="\"`cat $i`\"" >> .locale_vars
    echo "export LOCALE_$name" >> .locale_vars
done

#For those systems that don't have suidperl installed, assume
#their "magic" systems still support that feature by
#redefining SUIDPERL to be PERL5...

#if [ "$SUIDPERL" = "" ]; then
    SUIDPERL="$PERL5"
#fi

if [ "$SKIP_SETUID_TEST" = "" ]; then
 if [ "$SETUIDGID" != "" ]; then
  echo "Testing suid nature of $SUIDPERL..."
  (cat<<EOF
#!$SUIDPERL
\$uid=\$>;
print "Checking for setuid nature of perl install\n";
print "\nWhat follows should be the UID of the $QS_USER acount...\n\n";
print "uid=\$uid\n";
print "\n";
EOF
)> .perl-test.pl
    chmod 0755 .perl-test.pl
    DD=`$SETUIDGID $QS_USER $PERL5 ./.perl-test.pl 2>&1`
    QS_UID=`echo "$DD"|grep ^uid=|sed 's/^uid=//g'|egrep '^[0-9]+$'`
    #Now setuid it and see if the output changes
  chown $QS_USER:$QS_USER .perl-test.pl
  chmod 4755 .perl-test.pl
  DD=`$SETUIDGID qmailq ./.perl-test.pl 2>&1`
  QS_SUID=`echo "$DD"|grep ^uid=|sed 's/^uid=//g'|egrep '^[0-9]+$'`
  if [ "$QS_SUID" = "" -o "$QS_SUID" != "$QS_UID" ]; then
    echo "Whoa - broken perl install found."
    echo "Cannot even run a simple script setuid"
    echo ""
    echo "Installation of Qmail-Scanner FAILED"
    echo ""
    echo "Error was:"
    echo "$DD"
    echo ""
    if [ -f /etc/redhat-release ]; then
     echo "Redhat hosts need to have perl-suidperl installed to get setuid support"
    fi
    echo "See FAQ for further details"
    echo ""
    exit 1
  else
    echo "Looks OK..."
    #rm -f .perl-test.pl
  fi
 fi
fi

#Unconfigure a few options if ripmime being used

if [ "`echo $UNMIME_BINARY|grep ripmime`" != "" ]; then
 unset UUDECODE_BINARY 
 unset TNEF_BINARY
fi

. ./.locale_vars

perl -p -e "s?UNMIME_BINARY?$UNMIME_BINARY?g;
s?PERLRELEASE_DETAILS?$PERLRELEASE_DETAILS?g;
s?HOST_OS?$HOST_OS?g;
s?HOST_RELEASE?$HOST_RELEASE?g;
s?HOST_HARDWARE?$HOST_HARDWARE?g;
s?DEBUG_LEVEL?$DEBUG_LEVEL?g;
s?DESCRIPTIVE_HEADERS?$DESCRIPTIVE_HEADERS?g;
s?CMDLINE?$CMDLINE?g;
s?PERL5?$PERL5?g;
s?SUIDPERL?$SUIDPERL?g;
s?QMAILINJECT_BIN?$QMAILINJECT_BIN?g;
s?QMAILQUEUE_BIN?$QMAILQUEUE_BIN?g;
s?RM_BINARY?$RM_BINARY?g;
s?GREP_BINARY?$GREP_BINARY?g;
s?UNZIP_BINARY?$UNZIP_BINARY?g;
s?QUARANTINE_PASSWORD_PROTECTED?$QUARANTINE_PASSWORD_PROTECTED?g;
s?UNZIP_OPTIONS?$UNZIP_OPTIONS?g;
s?FORCE_UNZIP?$FORCE_UNZIP?g;
s?TNEF_BINARY?$TNEF_BINARY?g;
s?MIMEUNPACKER?$MIMEUNPACKER?g;
s?SILENT_VIRUSES_ARRAY?$FIND_SILENT_VIRUSES_ARRAY?g;
s?UVSCAN?$UVSCAN?g;
s?CSAV?$CSAV?g;
s?SWEEP?$SWEEP?g;
s?SOPHIE?$SOPHIE?g;
s?SSOCKET?$SSOCKET?g;
s?TROPHIE?$TROPHIE?g;
s?TSOCKET?$TSOCKET?g;
s?ISCAN?$ISCAN?g;
s?HBEDV_OPTIONS?$HBEDV_OPTIONS?g;
s?HBEDV?$HBEDV?g;
s?AVPSCAN?$AVPSCAN?g;
s?AVPDAEMON?$AVPDAEMON?g;
s?FPROT?$FPROT?g;
s?FSECURE?$FSECURE?g;
s?INOCUCMD?$INOCUCMD?g;
s?RAVLIN?$RAVLIN?g;
s?VEXIRA?$VEXIRA?g;
s?CLAMSCAN?$CLAMSCAN?g;
s?CLAMDSCAN?$CLAMDSCAN?g;
s?SPAMASSASSIN_BINARY?$SPAMASSASSIN_BINARY?g;
s?SPAMC_BINARY?$SPAMC_BINARY?g;
s?SPAMC_OPTIONS?$SPAMC_OPTIONS?g;
s?SPAMC_SUBJECT?$SPAMC_SUBJECT?g;
s?USERNAME?$USERNAME?g;
s?SKIP_TEXT_MSGS?$SKIP_TEXT_MSGS?g;
s?FQDN?$FQDN?g;
s?MAILDOMAIN?$MAILDOMAIN?g;
s?LOCAL_DOMAINS_ARRAY?$LOCAL_DOMAINS_ARRAY?g;
s?NOTIFY_ADDRESSES?$NOTIFY_ADDRESSES?g;
s?FIND?$FIND?g;
s?GREP?$GREP?g;
s?UUDECODE_BINARY?$UUDECODE_BINARY?g;
s?UUDECODE_PIPE?$UUDECODE_PIPE?g;
s?AS_QQ?$AS_QQ?g;
s?ARCHIVEIT?$ARCHIVEIT?g;
s?ARCHIVEDIR?$ARCHIVEDIR?g;
s?REDUNDANT?$REDUNDANT?g;
s?LOG_DETAILS?$LOG_DETAILS?g;
s?LOG_CRYPTO?$LOG_CRYPTO?g;
s?FIX_MIME?$FIX_MIME?g;
s?DISABLE_EOL_CHECK?$DISABLE_EOL_CHECK?g;
s?LOCALE_sender_subject?$LOCALE_sender_subject?g;
s?LOCALE_recips_subject?$LOCALE_recips_subject?g;
s?LOCALE_attention?$LOCALE_attention?g;
s?LOCALE_recips_automated_mail_note?$LOCALE_recips_automated_mail_note?g;
s?LOCALE_recips_content?$LOCALE_recips_content?g;
s?LOCALE_recips_explanation?$LOCALE_recips_explanation?g;
s?LOCALE_recips_msg_description?$LOCALE_recips_msg_description?g;
s?LOCALE_recips_quarantine?$LOCALE_recips_quarantine?g;
s?LOCALE_recips_not_automated_mail_note?$LOCALE_recips_not_automated_mail_note?g;
s?LOCALE_sender_virus_content?$LOCALE_sender_virus_content?g;
s?LOCALE_sender_quarantine?$LOCALE_sender_quarantine?g;
s?LOCALE_sender_automated_mail_note?$LOCALE_sender_automated_mail_note?g;
s?LOCALE_sender_explanation?$LOCALE_sender_explanation?g;
s?LOCALE_sender_msg_description?$LOCALE_sender_msg_description?g;
s?LOCALE_sender_other_content?$LOCALE_sender_other_content?g;
s?SCANNER_ARRAY?$SCANNER_ARRAY?g;" qmail-scanner-queue.template > qmail-scanner-queue.pl-1
perl -pe 's/%%/\$/g' qmail-scanner-queue.pl-1 > qmail-scanner-queue.pl
rm -f qmail-scanner-queue.pl-1

if [ "$UUDECODE_BINARY" != "" ]; then
    cat sub-uudecode.pl >> qmail-scanner-queue.pl
else
    echo 'sub check_and_grab_uuencoding {}; ' >> qmail-scanner-queue.pl
fi

if [ "$LOG_MSG_SUB" != "" ]; then
    echo "$LOG_MSG_SUB" >> qmail-scanner-queue.pl
else
    cat sub-log_msg.pl >> qmail-scanner-queue.pl
fi


(cat<<EOF

###############################
#
##  END of standard subroutines
##  Virus-scanner specific subroutines automatically added below by setup.sh
##
###############################

EOF
)  >> qmail-scanner-queue.pl

for scanner in `echo $SCANNER_ARRAY|sed -e 's/\"//g' -e 's/,/ /g' -e's/_scanner//g'` 
do 
    if [ "$scanner" = "sophie" ]; then
	sed "s?SSOCKET?$SSOCKET?" sub-sophie.template > sub-sophie.pl
    fi
    if [ "$scanner" = "trophie" ]; then
	sed "s?TSOCKET?$TSOCKET?" sub-trophie.template > sub-trophie.pl
    fi
    if [ "$scanner" = "sweep" ]; then
	#Special case for Sweep
	if [ "$SAV_IDE" != "" -o "$SAV_IDE_CONF" = "1" ]; then
	    sed "s?SOPHOS_SAV_IDE?$SAV_IDE?" sub-sweep.template > sub-sweep.pl
	else
	    echo "You have Sophos installed, but  SAV_IDE is not defined!"
	    echo ""
	    echo "Bad install of Sophos, exiting...."
	    exit 1
	fi
    fi
    if test -f "sub-$scanner.pl"
    then
	cat sub-$scanner.pl >> qmail-scanner-queue.pl
    else
	echo ""
	echo "** scanner subroutine for $scanner not found **"
	echo "** disabled as not officially supported. ** "
	echo ""
    fi
done

(cat<<EOF
#########################
## END of scanner definitions
##
#########################
EOF
) >> qmail-scanner-queue.pl

chmod 755 qmail-scanner-queue.pl

if [ "$INSTALLIT" != "0" ]; then
    if [ "$DONOTCONFIRM" != "1" ]; then
     echo  "Hit RETURN to create initial directory structure under $AS_QQ,"
     printf "and install qmail-scanner-queue.pl under $BINDIR: "
     read ans 
    fi
    mv -f $BINDIR/qmail-scanner-queue.pl $BINDIR/qmail-scanner-queue.pl.old 2>/dev/null
    cp -f qmail-scanner-queue.pl $BINDIR/qmail-scanner-queue.pl
    chown $QS_USER:$QS_USER $BINDIR/qmail-scanner-queue.pl
    chmod 4755 $BINDIR/qmail-scanner-queue.pl
    if [ -f "$BINDIR/antivirus-qmail-queue.pl" -a ! -L "$BINDIR/antivirus-qmail-queue.pl" ]; then
	mv -f $BINDIR/antivirus-qmail-queue.pl $BINDIR/antivirus-qmail-queue.pl.old
	ln -s $BINDIR/qmail-scanner-queue.pl $BINDIR/antivirus-qmail-queue.pl
	cat<<EOF

 ** REMEMBER to alter your Qmail startup scripts to call 
 $BINDIR/qmail-scanner-queue.pl instead of 
 $BINDIR/antivirus-qmail-queue.pl  **

EOF
    fi
    mkdir $AS_QQ 2>/dev/null
    if [ -f "$AS_QQ/quaranteen.log" ]; then
	mv -f $AS_QQ/quaranteen.log $AS_QQ/quarantine.log 2>/dev/null
	rm -f $AS_QQ/viruses.log
	ln -s $AS_QQ/quarantine.log $AS_QQ/viruses.log
    fi
    mv -f $AS_QQ/quaranteen $AS_QQ/quarantine 2>/dev/null
    mv -f $AS_QQ/quaranteen-attachments.txt $AS_QQ/quarantine-attachments.txt 2>/dev/null
    mv -f $AS_QQ/quaranteen-attachments.db $AS_QQ/quarantine-attachments.db 2>/dev/null
    if [ -d "$AS_QQ/viruses/cur" -a -d "$AS_QQ/viruses" -a ! -L "$AS_QQ/viruses" -a ! -f "$AS_QQ/quarantine" ] ; then
	mv -f $AS_QQ/viruses $AS_QQ/quarantine
    fi
    rm -f $AS_QQ/viruses 2>/dev/null
    ln -s $AS_QQ/quarantine $AS_QQ/viruses
    mkdir $AS_QQ/quarantine 2>/dev/null
    mkdir $AS_QQ/quarantine/tmp 2>/dev/null
    mkdir $AS_QQ/quarantine/cur 2>/dev/null
    mkdir $AS_QQ/quarantine/new 2>/dev/null
    mkdir $AS_QQ/working 2>/dev/null
    mkdir $AS_QQ/working/new 2>/dev/null
    mkdir $AS_QQ/working/cur 2>/dev/null
    mkdir $AS_QQ/working/tmp 2>/dev/null
    if [ -f "$AS_QQ/viruses.log" -a ! -L "$AS_QQ/viruses.log" ]; then
	mv -f $AS_QQ/viruses.log $AS_QQ/quarantine.log 2>/dev/null
    fi
    if [ -L "$AS_QQ/viruses.log" ]; then
	rm -f $AS_QQ/viruses.log
    fi
    touch $AS_QQ/quarantine.log 
    ln -s $AS_QQ/quarantine.log $AS_QQ/viruses.log
    if [ "$ARCHIVEIT"  != "0" ]; then
	mkdir $AS_QQ/$ARCHIVEDIR 2>/dev/null
	mkdir $AS_QQ/$ARCHIVEDIR/cur 2>/dev/null
	mkdir $AS_QQ/$ARCHIVEDIR/tmp 2>/dev/null
	mkdir $AS_QQ/$ARCHIVEDIR/new 2>/dev/null
    fi

    if [ "$LOG_DETAILS" = "mailstats.csv" ]; then
	if [ ! -f "$AS_QQ/$LOG_DETAILS" ]; then
	    echo "#Virus_Found	Process_Time	From	Recipients	Subject	Message-ID	Msg_Size	Date	Attachment_Filenames" > $AS_QQ/$LOG_DETAILS
		chown $QS_USER:$QS_USER $AS_QQ/$LOG_DETAILS
	fi
    fi

    if [ ! -f "$AS_QQ/quarantine-attachments.txt" ] ; then 
       cp quarantine-attachments.txt $AS_QQ/
    fi
    chown -R $QS_USER:$QS_USER $AS_QQ/
    $BINDIR/qmail-scanner-queue.pl -g
    if [ "$?" != "0" ]; then
	cat<<EOF


	    ******* FATAL ERROR *******


Whoa! Newly installed version of qmail-scanner-queue.pl exits with 
an error when run. 

Replacing with original version (if any) and exiting...

See ./qmail-scanner-queue.pl (current dir) for the errors refered to by 
perl above...

	    ****************************


EOF
        if [ -f "$BINDIR/qmail-scanner-queue.pl.old" ]; then
	 mv -f $BINDIR/qmail-scanner-queue.pl.old $BINDIR/qmail-scanner-queue.pl
	fi
	exit
    fi
    if [ -x "$BINDIR/qmail-scanner-queue.pl" ]; then $BINDIR/qmail-scanner-queue.pl -z ; fi
    cat<<EOF

Finished installation of initial directory structure for Qmail-Scanner
under $AS_QQ and qmail-scanner-queue.pl under $BINDIR.

Finished. Please read README(.html) and then go over the script
($BINDIR/qmail-scanner-queue.pl) to check paths/etc. 

"$BINDIR/qmail-scanner-queue.pl -r" should return some well-known virus
definitions to show that the internal perlscanner component is working.

That's it!

EOF

else

cat<<EOF 

Finished. Please read README(.html) and then go over the script to
check paths/etc, and then install as you see fit.

Remember to copy quarantine-attachments.txt to $AS_QQ and then
run "qmail-scanner-queue.pl -g" to generate DB version.

EOF



fi


if [ "$SCANNER_ARRAY" = "" ]; then
    cat<<EOF

########################################################################
##
#

NOTE: No content/virus scanner was found on your system - so only the 
internal perlscaner will be available for you to use.

Hope that's what you expected :-)

#
##
########################################################################

EOF

fi

if [ -x "$BINDIR/qmail-scanner-queue.pl" ]; then
    $BINDIR/qmail-scanner-queue.pl -v > SYSDEF
    (cat<<EOF

Configuration information: 

$CMDLINE

EOF
) >> SYSDEF
fi

rm -rf $TMP_DIR

cat<<EOF


              ****** FINAL TEST ******

Please log into an unpriviledged account and run 
$BINDIR/qmail-scanner-queue.pl -g

If you see the error "Can't do setuid", or "Permission denied", then  
refer to the FAQ.

(e.g.  "setuidgid qmaild $BINDIR/qmail-scanner-queue.pl -g")

EOF

cat<<EOF

That's it! To report success:

   % (echo 'First M. Last'; cat SYSDEF)|mail jhaar-s4vstats@crom.trimble.co.nz
Replace First M. Last with your name.
EOF
