#!/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 1
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

#Check for existence of setuidgid

if [ "`setuidgid 2>&1|grep 'setuidgid account child'`" = "" ]; then
	cat<<EOF

Sorry, but no evidence of setuidgid (which is part of daemontools) can
be found on your system.

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="avgd,avast,clamdscan,clamscan,bitdefender,nod32,esets,clamuko,savscan,sweep,sophie,vscan,trophie,uvscan,csav,antivir,kavscanner,AvpLinux,kavdaemon,AvpDaemonClient,fsav,fpscan,fpscand,inocucmd,ravlin,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`

SPOOLDIR="/var/spool/qscan"
QMAILDIR="/var/qmail"

BINDIR="$QMAILDIR/bin"

VIRUS_MAILDIR="viruses"
SPAM_MAILDIR="spam"
POLICY_MAILDIR="policy"

#QMAILQUEUE_BIN="$BINDIR/qmail-queue"
NORMALIZE_MSG="yes"
ARCHIVEIT="0"
ARCHIVEDIR="archives"
REDUNDANT="yes"
FIX_MIME="2"
DISABLE_EOL_CHECK="0"
DEBUG_LEVEL="1"
Q_REJECT_EXITCODE="31" 
Q_REJECT_FD="6"
FORCE_UNZIP="0"
DESCRIPTIVE_HEADERS="0"
ADMIN_DESCRIPTION="System Anti-Virus Administrator"
NOTIFY_ADDRESSES="psender,nmlvadm"
USERNAME="$USER"
MANUAL_INSTALL="0"
INSTALLIT="0"
DONOTCONFIRM="0"
LOG_DETAILS="syslog"
LOG_CRYPTO="0"
SKIP_TEXT_MSGS="1"
QS_USER="qscand"
SKIP_SETUID_TEST=""
MAX_ZIP_SIZE="1000000000"
MAX_SCAN_SIZE="100000000"
SA_EXAMPLE_QUARANTINE="5"
SA_MAXSIZE="256000"
SA_TIMEOUT="120"
SA_FT=1
SA_TEMPFAIL=1
ETCDIR="$SPOOLDIR"
LOGDIR="$SPOOLDIR" 

while [ -n "$1" ]
do
    case $1 in
        --qs-user) if [ "$2" != "" ] ; then  shift ; fi ; QS_USER="$1" ;; 
        --spooldir) if [ "$2" != "" ] ; then  shift ; fi ; SPOOLDIR="$1" ;;
	--etcdir) if [ "$2" != "" ] ; then  shift ; fi ; ETCDIR="$1" ;;
	--logdir) if [ "$2" != "" ] ; then  shift ; fi ; LOGDIR="$1" ;;
	--docdir) if [ "$2" != "" ] ; then  shift ; fi ; DOCDIR="$1" ;;
	--qmaildir) if [ "$2" != "" ]; then shift ; fi ; QMAILDIR="$1" ;;
	--virusdir) if [ "$2" != "" ]; then shift ; fi ; VIRUS_MAILDIR="$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" ;;
	--admin-description) if [ "$2" != "" ] ; then  shift ; fi ; ADMIN_DESCRIPTION="$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" ;;
	--max-scan-size) if [ "$2" != "" ]; then shift ; fi ; MAX_SCAN_SIZE="$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" ;;
	--max-zip-size) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" -a "`echo $2|egrep '^[0-9]+$'`" != "" ] ; then shift ; fi ; MAX_ZIP_SIZE="$1" ;;
	--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" ;;
	--normalize) if [ "$2" != "" ] ; then  shift ; fi ; NORMALIZE_MSG="$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="yes" ; 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" ;;
	--sa-timeout) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ] ; then  shift ; fi ; SA_TIMEOUT="$1";;
        --sa-maxsize) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ]; then shift ; fi ; SA_MAXSIZE="$1" ;;
	--sa-quarantine) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ]; then shift ; fi ; SA_ISSPAM="$1" ;;
        --sa-faulttolerant) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ]; then shift ; fi ; SA_FT="$1";;
	--sa-tempfail) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ]; then shift ; fi ; SA_TEMPFAIL="$1";;
        --quarantine-reject) if [ "$2" != "" -a "`echo $2|grep '\-'`" = "" ]; then shift ; fi ; Q_REJECT_STATUS="$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 $SPOOLDIR/
  --bindir <installdir> where to install Qmail-Scanner programs
                        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.
  --admin-description <description> Defaults to:
                                    "$ADMIN_DESCRIPTION"
                                    - this plus "--admin" and 
                                    "--domain" settings are used
                                    to construct the From: line
                                    in any e-mails generated by Q-S
  --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.
  
  --normalize [yes|no]              Defaults to "$NORMALIZE_MSG".
                                    This decides if base64/qp attachment 
                                    filenames and/or Subject: headers should 
                                    be "normalized" back to their decoded form 
                                    before being checked against entries in
                                    quarantine-events.txt.

  --notify "none|sender|recips|precips|admin|nmladm|nmlvadm|all" 
                                    Defaults to "$NOTIFY_ADDRESSES". 
				    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"/"precips" means notify 
                                    the sender/recips 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/recip 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 dramatically
				    reduce the chance of alerts being
				    sent to mailing-lists.	     
  --silent-viruses "virus1,virus2"  Depreciated. 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.
                                    Typically not needed anymore, as 
                                    by default alerts are no longer sent
                                    anyway. 
  --sa-timeout "number"             Currently set to "$SA_TIMEOUT".
                                    This is the max number of seconds
                                    you will allow spamc to take on processing
                                    a mail message. Anything longer implies
                                    spamd has hung on some narly DNS lookup
                                    or the like, and will cause QS to give
                                    the message a SPAM score of (?/?)
  --sa-faulttolerant [yes|no]       Currently set to "$SA_FT".
                                    This can be used in addition to sa-timeout
                                    as a way of telling Qmail-Scanner to let
                                    SA "have another go" at processing a message
                                    if it was unable to get it right the first time.
                                    It will cause Q-S to run SA up to THREE TIMES
                                    on a particular email - if SA fails to return any
                                    value (in the past this used to lead to Q-S reporting
                                    (?/?)). This can get around emails from far-off domains
                                    that "hang" SA due to DNS lookups - and *may* allow SA
                                    to operate correctly the next time it is called on the same
                                    message. See "--sa-tempfail" for even more
				    reliability options
  --sa-maxsize "number"             Currently set to "$SA_MAXSIZE".
                                    This size (in bytes) sets the
                                    max size email that will be
                                    processed by SpamAssassin.
  --sa-quarantine "X"               Disabled by default. If you have 
                                    SpamAssassin installed and enabled, then
                                    configuring this allows you to quarantine
                                    SPAM that is more than +X points than
				    the "required_hits" value (typically "5").
				    If you want to use this, a good starting point
				    might be "--sa-quarantine 5"
				    i.e. for required_hits=5, a score of 10 (5+5)
				    gets the message quarantined instead of
				    delivered to the end-user. E-mail
				    alerts are NEVER generated for SPAM, and
				    they are quarantined into the "./spam/"
				    maildir instead of the "./viruses/"
				    maildir where viruses go.
  --sa-tempfail [yes|no]            Should Qmail-Scanner treat SpamAssassin
                                    like AV products and tempfail if it 
                                    fails to return a score? Default: yes
  --quarantine-reject [yes|no]      Defaults to "no". Whether to trigger a SMTP
                                    error response to quarantine events (inc. SPAM).
                                    Qmail installs with the custom error patch will
                                    get a nice little text msg sent, those without 
                                    just produce a generic Qmail error. BE CAREFUL
                                    IF ENABLING AND YOUR Q-S SERVER ISN'T DIRECTLY
                                    FACING THE INTERNET
  --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 
			            "$SPOOLDIR/$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 "yes". Whether or not to let the scanners
			   also scan any zip files and the original "raw" Email
		           file.
  --max-scan-size [number]
                                Defaults to 100Mbytes 
                                Email messages (raw size) larger than this 
                                number (in bytes) will skip all AV and Spam 
                                scanning checks. It's to stop Q-S scanning
                                300Mbyte TIFF file messages and the like.
  --log-details [yes|syslog|no] Whether or not to log to mailstats.csv/via 
                                syslog the attachment structure of every Email 
                                message. Logs to "syslog"  by default.
  --log-crypto [yes|no]    Defaults to "no". Whether or not to log the presence
                           of cryptographic (both signing and encrypting) 
                           technologies in the "log-details". Q-S can flag
                           PGP, S/MIME and password-protected zip files. This
                           is informational logging only.
  --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). If you see blocks
                          occurring due to this setting, try "--fix-mime 1" first
                          before "--fix-mime no".
  --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.
  --max-zip-size [number] Defaults to 1 Gbytes. 
			  This setting allows you to control the maximum size you
			  are willing to allow zip file attachments to unpack to.
			  This is to enable you to limit DoS attacks against your
			  Qmail-Scanner installation (someone could send you a small zip
			  file that unpacks to Gbytes of useless files - filling your harddisk).
			  Set to whatever value you think is appropriate for your system. The
			  default value of 1Gb is set so large so as not to assume anything about
			  your system - YOU WILL NEED TO SET THIS VALUE IN ORDER TO GAIN ANY 
			  PROTECTION. Something like "100000000" (100 Mb) might be appropriate.
  --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


DOCDIR="/usr/share/doc/Qmail-Scanner-${VERSION}"


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

if [ "$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}"

#Sanity check that suid is allowed on the Qmail "/bin" partition
QMNT=`df $BINDIR|head -2|tail -1|awk '{print $1}'`
SUIDSTATUS=`mount|egrep "^$QMNT "|grep nosuid`
if [ "$SUIDSTATUS" != "" ]; then
	cat<<EOF
	
            *** FATAL ERROR ***

As far as I can tell, $BINDIR is mounted on a nonsuid file partition ($QMNT).

This means even Qmail will not operate correctly!

Please sort out your system before trying to install this...

EOF
	exit 1
fi



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 [ "`echo $NORMALIZE_MSG|egrep -i '^1|^y'`" != "" ]; then
    NORMALIZE_MSG="yes"
else
    NORMALIZE_MSG="no"
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 1
 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:/opt/f-prot:/usr/local/rav8/bin:/usr/lib/Vexira:/opt/bdc:/opt/kav/bin:/usr/local/nod32:/usr/local/esets:/opt/sophos-av/bin" 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 1
	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|precips|all)$'`" != "" ]; then
	    cat<<EOF


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

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

The only valid options are:

none
admin
nmladm
nmlvadm
sender
psender
recips
precips
all

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

if [ "$SA_ISSPAM" != "" ]; then
    SA_ISSPAM_Q=`echo $SA_ISSPAM|egrep '^[0-9]+$'`
    if [ $SA_ISSPAM_Q -gt 0 ]; then
	SA_QUARANTINE=$SA_ISSPAM_Q
    else
        cat<<EOF

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

Sorry, but you have defined "--sa-quarantine" but haven't configured it 
correctly.

It must be of the form:

--sa-quarantine $SA_EXAMPLE_QUARANTINE


               **************************
EOF
	exit 1
     fi
else
    SA_QUARANTINE=0
fi


if  [ "`echo $SA_TEMPFAIL|egrep -i '^[n|0]'`" != "" ]; then
	SA_TEMPFAIL=0
fi

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

if [ "`echo $Q_REJECT_STATUS|egrep -i '^[y|1]'`" != "" ]; then
    Q_REJECT_STATUS=1
    #Disable other forms of notifications - don't want both!
    NOTIFY_ADDRESSES="none"
else 
    Q_REJECT_STATUS=0
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 775 $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
	UNZIP_PASSWD="xx${RANDOM}$$xx"
	DD=`unzip -Ptest -t contrib/test_password.zip 2>&1|egrep 'testing:.*OK'`
	if [ "$DD" != "" ]; then
	 UNZIP_OPTIONS="-P$UNZIP_PASSWD"
	else
	 UNZIP_OPTIONS=""
	fi
	#Now check we can get filesizes out
	EE=`unzip -Ptest -lv contrib/test_password.zip 2>&1|egrep '80688.*test/ls'`
	if [ "$EE" = "" ]; then
	    echo "**FATAL ERROR ***"
	    echo ""
	    echo "$UNZIP_BINARY doesn't support the \"-lv\" option to view file details"
	    echo ""
	    echo "Please upgrade to another version of unzip"
	    echo ""
	    exit 1
	fi
    fi    

    if test -x $dir/avgscan
    then
	if [ "`echo $FIND_SCANNERS | grep ' avgd '`" != "" ]; then
	    AVGD_CONFIG_FILE="/etc/avg.conf"
	    AVGD_PORT="55555"
	    if [ -e $AVGD_CONFIG_FILE  ]; then
		AVGD_PORT_TMP="`cat /etc/avg.conf | grep -e port[[:space:]]*=[[:space:]]*[[:digit:]]* | sed s/[[:space:]]//g | cut -f2 -d'='`"
		if [ "$AVGD_PORT_TMP" = "" ]; then
		  AVGD_PORT=$AVGD_PORT_TMP
		  echo "Warning: Can not find the port number option in the $AVGD_CONFIG_FILE configuration file (using default $AVGD_PORT)"
		fi
		TEST_OUTPUT=`perl ./contrib/test-avgd.pl -p $AVGD_PORT -d $TMP_DIR 2>&1`
		if [ "`echo $TEST_OUTPUT | egrep -i '403'`" != "" ]; then
		    AVGD="${AVGD:-$dir/avgscan}"
		    INSTALLED_SCANNERS="$INSTALLED_SCANNERS avgd"
		else
		    if [ "$ERROR_ONCE" != "done" ]; then
			echo
			echo "Error: The AVG Anti-Virus daemon test failed. Please see the output below."
			echo "       Probably the AVG Anti-Virus daemon in not running!"
			echo
			echo "output from the AVG Anti-Virus daemon test skript:"
			echo "$TEST_OUTPUT"
			echo
			echo "Please fix this and run the qmail-scanner configure script again!"
			echo
			ERROR_ONCE="done"
		    fi
		fi
	    else
		echo "Error: Can not find the AVG Anti-Virus configuration file $AVGD_CONFIG_FILE"
	    fi
	fi
    fi	
	    
    if test -x $dir/uvscan 
    then
	if [ "`echo $FIND_SCANNERS|grep ' uvscan '`" != "" -a "$UVSCAN" = "" ]; then
	    if [ "`setuidgid $QS_USER $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 '`" != "" -a "$CSAV" = "" ]; then
	    if [ "`setuidgid $QS_USER $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 '`" != "" -a "$SOPHIE" = "" ]; then
    if test -x $dir/sophie
    then
	SOCKET="`setuidgid $QS_USER $dir/sophie -d -f README 2>&1|grep 'Socket path'|awk '{print $NF}'|sed 's/\"//g'`"	
	if [ "$SOCKET" != "" ]; then
	    DD=
	    if [ "`setuidgid $QS_USER 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 is not correctly installed or operational. 
Please read Q-S FAQ if you want it - especially check that sophie daemon 
can read files owned by $QS_USER (i.e. run it as $QS_USER).
".
	    fi
	fi
    fi
    fi
    if [ "`echo $FIND_SCANNERS|egrep ' (sophie|savscan|sweep) '`" != "" ]; then
    if test -x $dir/savscan
    then
	if [ "`$dir/savscan -h 2>&1|grep LAM`" = "" -a "$SAVSCAN" = "" ]; then
	   if [ "`setuidgid $QS_USER $dir/savscan -f -eec -all -sc -nc -ss -nb -archive $TMP_DIR  2>&1|egrep -i 'virus|test'`" != "" ]; then
		SAVSCAN="$dir/savscan"
		SWEEP="$SAVSCAN"
		INSTALLED_SCANNERS="`echo \"$INSTALLED_SCANNERS\"|grep -v sweep`"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
savscan"
	   fi
	   if [ "$SAV_IDE" = "" ]; then
                if [ ! -f "/etc/sav.conf" ]; then
                  if [ -d "/opt/sophos-av/lib/sav" ]; then
                    SAV_IDE="/opt/sophos-av/lib/sav"
                  fi
               else
                    SAV_IDE_CONF="1"
                fi
            fi
	fi
    fi
    if test -x $dir/sweep
    then
	if [ "`$dir/sweep -h 2>&1|grep LAM`" = "" -a "$SWEEP" = "" ]; then
	    if [ "`setuidgid $QS_USER $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 '`" != "" -a "$TROPHIE" = "" ]; then
    if test -x $dir/trophie
    then
	SOCKET="`setuidgid $QS_USER $dir/trophie -d -f README 2>&1|grep 'Socket path'|awk '{print $NF}'|sed 's/\"//g'`"
	if [ "$SOCKET" != "" ]; then
	    if [ "`setuidgid $QS_USER 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 is not correctly installed or operational. 
Please read Q-S FAQ if you want it - especially check that trophie daemon 
can read files owned by $QS_USER (i.e. run it as $QS_USER).
".
	    fi
	fi
    fi
    fi
    if [ "`echo $FIND_SCANNERS|egrep ' (vscan|trophie) '`" != "" -a "$ISCAN" = "" ]; then
    if test -x $dir/vscan
    then
	if [ "`setuidgid $QS_USER $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 '`" != "" -a "$HBEDV" = "" ]; then
    if test -x $dir/antivir
    then
	if [ "`setuidgid $QS_USER $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 [ "`setuidgid $QS_USER $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 '`" != "" -a "$AVPSCAN" = "" ]; then
	    if [ "`setuidgid $QS_USER $dir/kavscanner  $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 '`" != "" -a "$AVPSCAN" = "" ]; then
		if [ "`setuidgid $QS_USER $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 '`" != "" -a "$AVPDAEMON" = "" ]; then
	if test -f "sub-avpdaemon.pl"
	then
	    if [ "`setuidgid $QS_USER $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 '`" != "" -a "$AVPDAEMON" = "" ]; then
		if test -f "sub-avpdaemon.pl"
		then
		    if [ "`setuidgid $QS_USER $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 '`" != "" -a "$FSECURE" = "" ]; then
	    if [ "`setuidgid $QS_USER $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/fpscan
    then
        if [ "`echo $FIND_SCANNERS|egrep ' (fpscan|fpscand) '`" != "" -a "$FPSCAN" = "" ]; then
	    if [ "`setuidgid $QS_USER $dir/fpscan --adware --applications $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		FPSCAN="${FPSCAN:-$dir/fpscan}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
fpscan"
	    fi
	fi
    fi
    if test -x $dir/fpscand
    then
	if [ "`echo $FIND_SCANNERS|grep ' fpscand '`" != "" -a "$FPSCAND" = "" ]; then
	 #Test to see if the daemon is running
	 FPSCAND_DAEMON=`ps -ef  2>/dev/null|egrep fpscand|grep -v grep`
	 if [ "$FPSCAND_DAEMON" == "" ]; then
	  FPSCAND_DAEMON=`ps ax -ww 2>/dev/null|egrep fpscand|grep -v grep`
	 fi
	 if [ "$FPSCAND_DAEMON" != "" ]; then
	  FPSCAND_LISTENADDRESS=`grep ^FPSCAND_LISTENADDRESS /etc/f-prot.conf 2>/dev/null|sed -e 's/FPSCAND_LISTENADDRESS="//' -e 's/"$//'|grep :`
	  FPSCAND_SERVER=`echo $FPSCAND_LISTENADDRESS|cut -d: -f1`
	  FPSCAND_PORT=`echo $FPSCAND_LISTENADDRESS|cut -d: -f2`
	  if [ "$FPSCAND_SERVER" != "" -a "$FPSCAND_PORT" != "" ]; then
		sed -e "s/FPSCAND_SERVER/$FPSCAND_SERVER/g" -e "s/FPSCAND_PORT/$FPSCAND_PORT/g" sub-fpscand.template > sub-fpscand.pl
		cp -f sub-fpscand.pl sub-fpscand-test.pl
		(cat<<EOF
##
use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use POSIX;

sub debug
{
	my(\$msg)=@_;
	print "DBG: \$msg\n";
}
&fpscand_scanner();
print "quarantine_event=\$quarantine_event\n";
EOF
) >> sub-fpscand-test.pl
		OLD_TMPDIR="$TMPDIR"
		TMPDIR="$TMP_DIR"
		export TMPDIR
            	if [ "`perl sub-fpscand-test.pl 2>&1|egrep 'FPSCAND:'`" != "" ]; then
                 FPSCAND="${FPSCAND:-$dir/fpscan}"
                 INSTALLED_SCANNERS="$INSTALLED_SCANNERS
fpscand"
            	fi
	    	rm -f sub-fpscand-test.pl
		TMPDIR="$OLD_TMPDIR"
		export TMPDIR
	 fi
        fi
      fi
    fi
    if test -x $dir/bdc
    then
        if [ "`echo $FIND_SCANNERS|grep ' bitdefender '`" != "" -a "$BITDEFENDER" = "" ]; then
	    if [ "`setuidgid $QS_USER $dir/bdc --all --alev=10 --flev=10 --arc --mail $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
	     BITDEFENDER="${BITDEFENDER:-$dir/bdc}"
	     INSTALLED_SCANNERS="$INSTALLED_SCANNERS
bdc"
	    fi
	fi
    fi
    if test -x $dir/nod32cli
    then
	if [ "`echo $FIND_SCANNERS|grep ' nod32 '`" != "" ]; then
	    if [ "`$dir/nod32cli -c 1 --subdir $TMP_DIR 2>&1|egrep -i 'virus='`" != "" ]; then
		NOD32="${NOD32:-$dir/nod32cli}"
		UPDNOD="${NOD32:-$dir/nod32upd}"
		INSTALLED_SCANNERS="$INSTALLED_SCANNERS
nod32"
	    fi
	fi
    fi    
    if test -x $dir/esets_cli
    then
       if [ "`echo $FIND_SCANNERS|grep ' esets '`" != "" ]; then
           if [ "`$dir/esets_cli --subdir $TMP_DIR 2>&1|egrep -i 'virus='`" != "" ]; then
                ESETS="${ESETS:-$dir/esets_cli}"
                #UPDESETS="${ESETS:-$dir/esets_cliupd}"
                INSTALLED_SCANNERS="$INSTALLED_SCANNERS
esets"
           fi
	fi
    fi
    if test -x $dir/inocucmd
    then
        if [ "`echo $FIND_SCANNERS|grep ' inocucmd '`" != "" -a "$INOCUCMD" = "" ]; then
	    if [ "`setuidgid $QS_USER $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 [ "`setuidgid $QS_USER $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 '`" != "" -a "$VEXIRA" = "" ]; then
	   if [ "`setuidgid $QS_USER $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 test -x $dir/avastlite
    then
	if [ "`echo $FIND_SCANNERS|grep ' avast '`" != "" -a "$AVASTLITE" = "" ]; then
	 if [ "`setuidgid $QS_USER $dir/avastlite $TMP_DIR 2>&1|egrep -i 'virus|test'`" != "" ]; then
		AVASTLITE="${AVASTLITE:-$dir/avastlite}"
		AVASTCMD="$dir/avastcmd"
                INSTALLED_SCANNERS="$INSTALLED_SCANNERS
avast"
	 elif [ "`setuidgid $QS_USER $dir/avastlite $TMP_DIR 2>&1|egrep 'can.t connect to avast. server'`" != "" ]; then
		AVAST_PERMISSION_FAULT="/etc/avastd.conf needs editing: group needs to be qscand"
	 fi
	fi
    fi
    if test -x $dir/freshclam
    then
	export FRESHCLAM="$dir/freshclam"
    fi
    if [ "`echo $FIND_SCANNERS|grep ' clamdscan '`" != "" -a "$CLAMDSCAN" = "" ]; then
    if test -x $dir/clamdscan
    then
	DD="`setuidgid $QS_USER $dir/clamdscan --help 2>&1|grep -i Daemon`"
	if [ "$DD" = "" ]; then
	    echo "
Something like clamdscan for ClamAV detected - but not correctly installed. 
Please read Q-S FAQ if you want it - especially check that it is a recent
release and that the clamd daemon can read files owned by $QS_USER 
(i.e. make sure clamd runs as $QS_USER).
"
	fi
	DD="`setuidgid $QS_USER $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 it is a recent
release and that the clamd daemon can read files owned by $QS_USER 
(i.e. make sure clamd runs as $QS_USER).
"
	fi
    fi
    fi
    if test -x $dir/clamscan
    then
	if [ "`echo $FIND_SCANNERS|grep ' clamscan '`" != "" -a "$CLAMSCAN" = "" ]; then
	   if [ "`setuidgid $QS_USER $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 [ "$SPAMASSASSIN_BINARY" != "" -a -x "$dir/spamc" -a "$SPAMC_BINARY" = "" ]
    then
	#Test it out
	if [ "`setuidgid $QS_USER $dir/spamc -h 2>&1|grep 'spamd'`" != "" ] ;then
	    SPAMC_BINARY="${SPAMC_BINARY:-$dir/spamc}"

	    #Test to see if spamc is calling Unix sockets instead of TCP
	    SPAMD_SOCKET=`ps -ef 2>/dev/null|egrep 'spamd.*socketpath'|grep -v grep|sed -e 's/^.*socketpath=//'|awk '{print $1}'`
	    if [ "$SPAMD_SOCKET" = "" ]; then
		SPAMD_SOCKET=`ps ax -ww 2>/dev/null|egrep 'spamd.*socketpath'|grep -v grep|sed -e 's/^.*socketpath=//'|awk '{print $1}'`
	    fi
	    if [ "$SPAMD_SOCKET" != "" -a -S "$SPAMD_SOCKET" ]; then
		SPAMC_BINARY="$SPAMC_BINARY -U $SPAMD_SOCKET"
		SA_HN=" -U $SPAMD_SOCKET"
	    fi
            DD="`setuidgid $QS_USER $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 1
            fi
	    DD="`setuidgid $QS_USER $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="`setuidgid $QS_USER $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="`setuidgid $QS_USER $SPAMC_BINARY $SA_HN -c < ./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" SpamAssassin support, however
your system is NOT correctly configured for it.

Either correct and try again, or reconfigure for the potentially slower "verbose"
SpamAssassin support.

EOF
			exit 1
		  fi
		        SPAMC_OPTIONS="$SA_HN -c "
			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
	if [ "`find --help 2>&1|grep gnu`" != "" ]; then
	    GNU_FIND="${GNU_FIND:-$dir/find}"
	else
	    FIND="${FIND:-$dir/find}"
	fi
    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

#Prefer GNU find to other OS's if available
if [ "$GNU_FIND" != "" ]; then
    FIND="$GNU_FIND"
fi

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 $SPOOLDIR --qmaildir $QMAILDIR --bindir $BINDIR --qmail-queue-binary $QMAILQUEUE_BIN --admin $USERNAME --domain $MAILDOMAIN --admin-description \"$ADMIN_DESCRIPTION\" --notify $NOTIFY_ADDRESSES --local-domains $LOCAL_DOMAINS_ARRAY --max-scan-size $MAX_SCAN_SIZE --silent-viruses $SILENT_VIRUSES --sa-timeout $SA_TIMEOUT --sa-faulttolerant $SA_FT --sa-maxsize $SA_MAXSIZE --sa-quarantine $SA_QUARANTINE --sa-tempfail $SA_TEMPFAIL --quarantine-reject $Q_REJECT_STATUS --lang $QSLANG --debug $DEBUG_LEVEL --unzip $FORCE_UNZIP --max-zip-size $MAX_ZIP_SIZE --add-dscr-hdrs $DESCRIPTIVE_HEADERS --normalize $NORMALIZE_MSG --archive $ARCHIVEIT --redundant $REDUNDANT --skip-text-msgs $SKIP_TEXT_MSGS --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 1

    fi
fi


#Now check for evidence of the custom-error patch. Latest uses FD6
 if [ "$Q_REJECT_EXITCODE" = "31" -a "$QMAILINJECT_BIN" != "" -a "$MANUAL_INSTALL" = "0" ]; then
	(cat<<EOF
#!/usr/bin/perl
open(FD6,">&6");
print FD6 "Dtesting custom error patch";
close FD6;
exit 82;
EOF
) > $TMP_DIR/qs-custom.pl
    chmod 755 $TMP_DIR/qs-custom.pl
    QMAILQUEUE="$TMP_DIR/qs-custom.pl"
    export QMAILQUEUE
    DD=`echo xxx|$QMAILINJECT_BIN 2>&1|grep 'fatal: testing custom error patch'`
    if [ "$DD" = "" ]; then
	Q_REJECT_EXITCODE="31"
    else
	Q_REJECT_EXITCODE="82"
	Q_REJECT_FD="6"
    fi
 fi
 if [ "$Q_REJECT_EXITCODE" = "31" -a "$QMAILINJECT_BIN" != "" -a "$MANUAL_INSTALL" = "0" ]; then
    #Nope, try with the FD4 option
    (cat<<EOF
#!/usr/bin/perl
open(FD4,">&4");
print FD4 "Dtesting custom error patch";
close FD4;
exit 82;
EOF
) > $TMP_DIR/qs-custom.pl
    chmod 755 $TMP_DIR/qs-custom.pl
    QMAILQUEUE="$TMP_DIR/qs-custom.pl"
    export QMAILQUEUE
    DD=`echo xxx|$QMAILINJECT_BIN 2>&1|grep 'fatal: testing custom error patch'`
    if [ "$DD" = "" ]; then
	Q_REJECT_EXITCODE="31"
    else
	Q_REJECT_EXITCODE="82"
	Q_REJECT_FD="4"
    fi
 fi
 #Try again with older custom-error patch that uses stderr
 if [ "$Q_REJECT_EXITCODE" = "31" -a "$QMAILINJECT_BIN" != "" -a "$MANUAL_INSTALL" = "0" ]; then
    (cat<<EOF
#!/usr/bin/perl
print STDERR "Dtesting custom error patch";
exit 82;
EOF
) > $TMP_DIR/qs-custom.pl
    chmod 755 $TMP_DIR/qs-custom.pl
    QMAILQUEUE="$TMP_DIR/qs-custom.pl"
    export QMAILQUEUE
    DD=`echo xxx|$QMAILINJECT_BIN 2>&1|grep 'fatal: testing custom error patch'`
    if [ "$DD" = "" ]; then
	Q_REJECT_EXITCODE="31"
    else
	Q_REJECT_EXITCODE="82"
	Q_REJECT_FD="2"
    fi
 fi


 #Sanity check.
 if [ "$Q_REJECT_EXITCODE" = "31" -a "$Q_REJECT_STATUS" = "1" ]; then
    cat<<EOF

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

You want to use "--quarantine-reject" and yet do not have the custom-error
patch installed. This is NOT supported

Exiting...

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

EOF	
	exit 1
 fi
unset QMAILQUEUE

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 1
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 1
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 1
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 1
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 1
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 1
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 [ "$FORCE_UNZIP" = "1" -a "$UNZIP_BINARY" != "" ] 
then
    echo "unzip=$UNZIP_BINARY"
    echo "max-zip-size=$MAX_ZIP_SIZE"
fi
if [ "$TNEF_BINARY" != "" ] 
then
    echo "tnef=$TNEF_BINARY"
fi

echo ""
echo "Content/Virus Scanners installed on your System"
echo ""
if [ "$MAX_SCAN_SIZE" != "" ]; then
    echo "max-scan-size=$MAX_SCAN_SIZE"
fi

if [ "$CLAMDSCAN" != "" ]; then
    if [ "$FRESHCLAM" == "" ]; then
        echo "FATAL ERROR: found $CLAMDSCAN, but cannot find freshclam."
        exit 1
    fi
    echo "clamdscan=$CLAMDSCAN (which means clamscan won't be used as clamdscan is better)";
    SCANNER_ARRAY="$SCANNER_ARRAY,\"clamdscan_scanner\""
fi

if [ "$CLAMSCAN" != "" -a "$CLAMDSCAN" = "" ]; then
    if [ "$FRESHCLAM" == "" ]; then
	echo "FATAL ERROR: found $CLAMSCAN, but cannot find freshclam."
	exit 1
    fi
    echo "clamscan=$CLAMSCAN";
    SCANNER_ARRAY="$SCANNER_ARRAY,\"clamscan_scanner\""
fi

if [ "$AVGD" != "" ]; then
    echo "avgd=$AVGD"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"avgd_scanner\""
fi

if [ "$AVASTLITE" != "" ]; then
	echo "avast=$AVASTLITE"
	SCANNER_ARRAY="$SCANNER_ARRAY,\"avast_scanner\""
fi
if [ "$AVAST_PERMISSION_FAULT" != "" ]; then
	echo "avast=FAILED: $AVAST_PERMISSION_FAULT"
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 [ "$NOD32" != "" ]; then 
    echo "nod32=$NOD32" 
    echo "(NOTE: you should set the following values in /etc/nod32/nod32.cfg"
    echo "write_to_emails = 0"
    echo "action_on_infected = \"accept\""
    echo "quarantine = no   )"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"nod32_scanner\""
fi
if [ "$ESETS" != "" ]; then
    echo "esets=$ESETS"
    # st: thanks to Apro Krisztian
    echo "(NOTE: you should set the following values in /etc/esets/esets.cfg"
    echo "action_av_infected = \"reject\" or action_av_infected = \"discard\""
    echo "av_quarantine_enabled = no   )"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"esets_scanner\""
fi
if [ "$SAVSCAN" != "" -a "$SOPHIE" = "" ]; then
    echo "savscan=$SAVSCAN"
    unset SWEEP
    SCANNER_ARRAY="$SCANNER_ARRAY,\"savscan_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 savscan 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 [ "$FPSCAN" != "" -a "$FPSCAND" == "" ]; then
    echo "fpscan=$FPSCAN"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"fpscan_scanner\""
fi

if [ "$FPSCAND" != "" ]; then
    echo "fpscand=$FPSCAND (which means fpscan is disabled as fpscand is better)"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"fpscand_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 [ "$BITDEFENDER" != "" ]; then
    echo "bitdefender=$BITDEFENDER"
    SCANNER_ARRAY="$SCANNER_ARRAY,\"bitdefender_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 1
        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 [ "$LOG_CRYPTO" != "" ]; then
    echo "log-crypto=$LOG_CRYPTO"
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 $SPOOLDIR/$ARCHIVEDIR/"
fi
if [ $SA_QUARANTINE -gt 0 ]; then
    echo "sa-quarantine=$SA_QUARANTINE"
fi

if [ $SA_TEMPFAIL -gt 0 ]; then
    echo "sa-tempfail=$SA_TEMPFAIL"
fi


if [ $SA_FT -eq 1 ]; then
   echo "sa-faulttolerant=$SA_FT"
fi

if [ $SA_MAXSIZE -gt 0 ]; then
	echo "sa-maxsize=$SA_MAXSIZE"
else
	SA_MAXSIZE="256000"
fi

echo "virus-admin=$ADMIN_DESCRIPTION <$USERNAME@$MAILDOMAIN>"
echo "local-domains=$LOCAL_DOMAINS_ARRAY"
echo "silent-viruses=$FIND_SILENT_VIRUSES_ARRAY"
echo "scanners=`echo $SCANNER_ARRAY|sed 's/_scanner//g'`"

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 1
 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 6755 .perl-test.pl
  #This will be run as a different account than $QS_USER - we'll use qmailq
  #as that must exist on every Qmail system
  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 "You will either have to correct this"
    echo "or use the C-wrapper within the ./contrib dir"
    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 ""
    printf "Hit RETURN to continue, Ctr-C to exit and fix: "
    read ans
  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

#Set maxsize in spamassassin subroutine
#sed -e "s/SA_MAXSIZE/$SA_MAXSIZE/g" sub-spamassassin.template > sub-spamassassin.pl
cp sub-spamassassin.template sub-spamassassin.pl

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?QS_USER?$QS_USER?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?MAX_ZIP_SIZE?$MAX_ZIP_SIZE?g;
s?MAX_MSG_SIZE?$MAX_SCAN_SIZE?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?AVGD?$AVGD?g;
s?AVGD_PORT?$AVGD_PORT?g;
s?UVSCAN?$UVSCAN?g;
s?CSAV?$CSAV?g;
s?SWEEP?$SWEEP?g;
s?SAVSCAN?$SAVSCAN?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?FPSCAND?$FPSCAND?g;
s?FPSCAN?$FPSCAN?g;
s?FSECURE?$FSECURE?g;
s?INOCUCMD?$INOCUCMD?g;
s?RAVLIN?$RAVLIN?g;
s?VEXIRA?$VEXIRA?g;
s?AVASTLITE?$AVASTLITE?g;
s?AVASTCMD?$AVASTCMD?g;
s?NOD32?$NOD32?g;
s?UPDNOD?$UPDNOD?g;
s?ESETS?$ESETS?g;
s?BITDEFENDER?$BITDEFENDER?g;
s?CLAMSCAN?$CLAMSCAN?g;
s?CLAMDSCAN?$CLAMDSCAN?g;
s?FRESHCLAM?$FRESHCLAM?g;
s?Q_REJECT_EXITCODE?$Q_REJECT_EXITCODE?g;
s?Q_REJECT_STATUS?$Q_REJECT_STATUS?g;
s?Q_REJECT_FD?$Q_REJECT_FD?g;
s?SA_QUARANTINE?$SA_QUARANTINE?g;
s?SA_TEMPFAIL?$SA_TEMPFAIL?g;
s?SA_MAXSIZE?$SA_MAXSIZE?g;
s?SA_TIMEOUT?$SA_TIMEOUT?g;
s?SA_FAULT_TOLERANT?$SA_FT?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?ADMIN_DESCRIPTION?$ADMIN_DESCRIPTION?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?QS_SPOOLDIR?$SPOOLDIR?g;
s?QS_LOGDIR?$LOGDIR?g;
s?QS_ETCDIR?$ETCDIR?g;
s?VIRUS_MAILDIR?$VIRUS_MAILDIR?g;
s?SPAM_MAILDIR?$SPAM_MAILDIR?g;
s?POLICY_MAILDIR?$POLICY_MAILDIR?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?LOCALE_destring_problem?$LOCALE_destring_problem?g;
s?LOCALE_destring_disallowed_attachment_type?$LOCALE_destring_disallowed_attachment_type?g;
s?LOCALE_destring_virus?$LOCALE_destring_virus?g;
s?LOCALE_destring_policy_violation?$LOCALE_destring_policy_violation?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

cat sub-attachments.pl >> qmail-scanner-queue.pl

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

if [ "$NORMALIZE_MSG" = "no" ]; then
    (cat<<EOF
sub normalize_string {
  my (\$type,\$string)=@_;
  return \$string;
} ;
EOF
) >> qmail-scanner-queue.pl
else
    cat sub-normalize.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" = "avgd" ]; then
	sed "s?AVGD_PORT?$AVGD_PORT?" sub-avgd.template > sub-avgd.pl
    fi

    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 $SPOOLDIR,"
     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 6755 $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 $SPOOLDIR 2>/dev/null
    mkdir $ETCDIR 2>/dev/null
    mkdir $LOGDIR 2>/dev/null
    #mkdir $DOCDIR 2>/dev/null
    rm -f $SPOOLDIR/viruses $SPOOLDIR/quarantine 2>/dev/null
    mkdir $SPOOLDIR/quarantine 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$VIRUS_MAILDIR 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$VIRUS_MAILDIR/tmp 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$VIRUS_MAILDIR/cur 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$VIRUS_MAILDIR/new 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$SPAM_MAILDIR 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$SPAM_MAILDIR/tmp 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$SPAM_MAILDIR/cur 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$SPAM_MAILDIR/new 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$POLICY_MAILDIR 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$POLICY_MAILDIR/tmp 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$POLICY_MAILDIR/cur 2>/dev/null
    mkdir $SPOOLDIR/quarantine/$POLICY_MAILDIR/new 2>/dev/null
    mkdir $SPOOLDIR/working 2>/dev/null
    mkdir $SPOOLDIR/working/new 2>/dev/null
    mkdir $SPOOLDIR/working/cur 2>/dev/null
    mkdir $SPOOLDIR/working/tmp 2>/dev/null
    touch $LOGDIR/quarantine.log 
    #if [ "$ARCHIVEIT"  != "0" ]; then
	mkdir $SPOOLDIR/$ARCHIVEDIR 2>/dev/null
	mkdir $SPOOLDIR/$ARCHIVEDIR/cur 2>/dev/null
	mkdir $SPOOLDIR/$ARCHIVEDIR/tmp 2>/dev/null
	mkdir $SPOOLDIR/$ARCHIVEDIR/new 2>/dev/null
    #fi

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

    if [ ! -f "$ETCDIR/quarantine-events.txt" ] ; then 
       cp quarantine-events.txt $ETCDIR/
    fi
    find $SPOOLDIR/ -type d -exec chown -R $QS_USER:$QS_USER {} \;
    if [ "$SPOOLDIR" != "$ETCDIR" ]; then
	chown -R $QS_USER:$QS_USER $ETCDIR
	chmod -R 750 $ETCDIR
    fi
    chown -R $QS_USER:$QS_USER $LOGDIR/
    $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 1
    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 $SPOOLDIR 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-events.txt to $SPOOLDIR 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
