#!/bin/sh
unalias -a
umask 0022
cat <<-.EOF
	MailStripper installation tool.
	Copyright (C) `date '+2003-%Y,'` Eridani Star System,
	All rights reserved.

	.EOF
if [ -d /var/spool/mailstripper ]; then
    echo '=>' Found previous installation, stopping it...
    START=`./mailstripper.init stop | wc -l`
    if [ -h /etc/mailstripper ]; then
	echo '=>' Converting to current configuration system.
	rm -f /etc/mailstripper
	mkdir /etc/mailstripper
	mv /usr/share/mailstripper/* /etc/mailstripper/
	rmdir /usr/share/mailstripper
    fi
else
    echo '=>' Creating configuration directories.
    mkdir -p /etc/mailstripper /var/spool/mailstripper
    START=0
fi
echo '=>' Setting permissions on configuration directory.
mkdir -p /etc/mailstripper/plugins
chmod 0711 /var/spool/mailstripper
chmod 0711 /etc/mailstripper
chmod 0711 /etc/mailstripper/plugins
echo '=>' Copying necessary configuration files across.
for FILE in badips senders.allow senders.deny recipients.allow recipients.tag domains.allow ; do
    if [ ! -f /etc/mailstripper/${FILE} ]; then
	cp -p ${FILE} /etc/mailstripper/
    fi
done
echo '=>' Copying programs to standard locations.
chown 0:0 mailstripper mstripconf xmstripconf score avwrap local_blocklist surbl
chmod 0755 mailstripper mstripconf xmstripconf score avwrap local_blocklist surbl
cp -p mailstripper mstripconf xmstripconf /usr/sbin/
cp -p score /usr/bin/
cp -p avwrap /usr/local/bin/
cp -p local_blocklist surbl /etc/mailstripper/plugins/
if [ -d /etc/rc.d/init.d ]; then
    if [ -f /etc/rc.d/init.d/functions ]; then
	echo '=>' Installing RedHat style boot initialisation facility in /etc/rc.d/init.d
	cp -p mailstripper.rh.init /etc/rc.d/init.d/mailstripper
    else
	echo '=>' Installing non-RedHat style boot initialisation facility in /etc/rc.d/init.d
	cp -p mailstripper.init /etc/rc.d/init.d/mailstripper
    fi
    chown 0:0 /etc/rc.d/init.d/mailstripper
    chmod 755 /etc/rc.d/init.d/mailstripper
elif [ -d /etc/init.d ]; then
    echo '=>' Installing non-RedHat style boot initialisation facility in /etc/init.d
    cp -p mailstripper.init /etc/init.d/mailstripper
    chown 0:0 /etc/init.d/mailstripper
    chmod 755 /etc/init.d/mailstripper
else
    echo '*>' Unable to install boot initialisation facility.
    echo '  ' "Can't find /etc/init.d or /etc/rc.d/init.d"
fi
if [ -d /etc/logrotate.d ]; then
    if [ ! -f /etc/logrotate.d/mailstripper ]; then
	echo '=>' Installing log rotation facility.
	cp -p mailstripper.logrotate /etc/logrotate.d/mailstripper
	chown 0:0 /etc/logrotate.d/mailstripper
    else
    	echo '  ' Existing logrotate script found, not overwriting.
    fi
else
    echo '*>' Log rotation facility not available.
fi
CFG=`which chkconfig`
if [ -n "${CFG}" ]; then
    echo '=>' Configuring boot-time auto-start of MailStripper.
    chkconfig --add mailstripper
else
    echo '*>' NOT configuring boot-time auto-start of MailStripper.
    echo '  ' Unable to locate boot-time auto-start system.
fi
echo '=>' Setting ownership and permissions on binaries.
chown 0:0 /usr/sbin/mailstripper /usr/sbin/mstripconf /usr/sbin/xmstripconf /usr/bin/score /etc/mailstripper/plugins/local_blocklist
echo '=>' Updating internal configuration.
mstripconf -resave >/dev/null
if [ -f /etc/mailstripper/blocklist -a ! -f /etc/mailstripper/honeypot ]; then
    mv -f blocklist blocklist.11X.orig
    mstripconf -cat blocklist.11X.orig > /etc/mailstripper/blocklist.11X
    cat <<-.EOF

	*> WARNING: The blocklist file has changed in format and capability. Your
	*> old blocklist has been renamed /etc/mailstripper/blocklist.11X.orig and
	*> converted to plain text in /etc/mailstripper/blocklist.11X

	.EOF
fi
for FILE in blocklist honeypot ; do
    if [ ! -f /etc/mailstripper/${FILE} ]; then
	cp -p ${FILE} /etc/mailstripper/
    fi
done

chown 0:0 /etc/mailstripper/*
chmod 0644 /etc/mailstripper/*
if [ -f /etc/mailstripper/mstripnotify ]; then
  chmod 0755 /etc/mailstripper/mstripnotify
fi

echo '010300' > /etc/mailstripper/vertag

echo '=>' Attempting to retrieve current blocklist file.
/bin/mv /etc/mailstripper/blocklist /etc/mailstripper/blocklist.preUpdate
mstripconf -update
RES=$?
if [ ${RES} -ne 0 ]; then
  echo '**>' Unable to retrieve blocklist: MailStripper requires a clear path
  echo '**>' to www.eridani.co.uk \(62.49.18.88\) port 80 to retrieve this file
  echo '**>' and to verify the validity of licence codes.
  echo '-->' If this is a first-time installation, this is normal, and upon
  echo '-->' entering your licence code blocklist retrieval will work.
  echo '**>' Please press ENTER.
  /bin/mv /etc/mailstripper/blocklist.preUpdate /etc/mailstripper/blocklist
  read DUMMY
else
  echo '==>' Blocklist retrieved OK. Any old version present has been renamed
  echo '==>' as blocklist.preUpdate in /etc/mailstripper.
  echo '==>' If you wish to continue using your old blocklist, just copy it
  echo '==>' back in place over /etc/mailstripper/blocklist.
  echo '**>' Please press ENTER.
  read DUMMY
fi

if [ ${START} -gt 0 ]; then
    echo '=>' Restarting MailStripper.
    ./mailstripper.init start | sed 's/^/   /'
fi
cat <<-.EOF
	=> Configuration completed.

	Thank you for installing MailStripper Pro!

	Before trying to start MailStripper, please run xmstripconf, the GUI
	configuration utility. You do not need to be running an X server - the
	GUI can be exported to another X display using the DISPLAY environment
	variable. Alternatively, run mstripconf on a text display.

	Before you can make any changes, you must obtain a licence for
	MailStripper, either a personal, commercial or evaluation one depending
	on the use it will be put to. Details may be found on the MailStripper
	website. KEEP THIS SAFE as you will need it if you make a hardware or
	kernel change to your system, or need to reinstall.

	By default, MailStripper will put any mail caught by the spam trap in
	/root/mail/spam, and mail caught by the anti-virus function in
	/root/mail/virus in mbox format that Pine can read. This way you can
	review it for false positives and bounce it to the correct place.

	Any suggestions, enhancements, bug reports etc, please let me know!
	Michael McConnell, mailstripper@eridani.co.uk

	.EOF
