#!/bin/bash
#
# $Id: Sophos.install 4144 2007-08-20 18:48:13Z sysjkf $
#
# Run this script to install Sophos in the right place, with the right
# options for the MailScanner.
# Run this script from inside the directory in which you have copied
# and unpacked the Sophos distribution.
# Tweaked for Solaris.

# Handle Version 5 first
if [ \! -d talpa ]; then
  echo
  echo 'If you are trying to install Sophos version 5,'
  echo 'then please expand the tar file with a command like'
  echo '    tar xzf sav-linux-5-i386.tgz'
  echo '    cd sophos-av'
  echo 'Then run me again!'
  echo
  echo 'Press return to continue, or Ctrl-C to escape.'
  read a
else
  if [ \! -d "sophos-av" ]; then
    gunzip -c sav*tgz | tar xvBpf -
    cd sophos-av
  fi
  echo
  echo '======================================='
  echo 'MailScanner installing Sophos version 5'
  echo '======================================='
  echo
  echo 'Install using the defaults, but do *NOT* enable on-access scanning.'
  echo 'If you do enable this feature, you will break MailScanner completely.'
  echo
  echo 'If you install it somewhere other than /opt/sophos-av, then you'
  echo 'will need to edit the etc/virus.scanners.conf file in your'
  echo 'MailScanner installation. It needs to point to the directory which'
  echo 'has the bin subdirectory in it, such as /opt/sophos-av.'
  echo 'This is a change from previous versions.'
  echo
  echo 'Press return to continue.'
  read a
  if [ -d sophos-av ]; then
    cd sophos-av
  fi
  ./install.sh

  # Fix up the MailScanner virus.scanners.conf file
  MSETC=''
  if [ -f /etc/MailScanner/virus.scanners.conf ]; then
    MSETC=/etc/MailScanner/virus.scanners.conf
  elif [ -f /usr/local/MailScanner/etc/virus.scanners.conf ]; then
    MSETC=/usr/local/MailScanner/etc/virus.scanners.conf
  elif [ -f /usr/local/etc/virus.scanners.conf ]; then
    MSETC=/usr/local/etc/virus.scanners.conf
  elif [ -f /opt/MailScanner/etc/virus.scanners.conf ]; then
    MSETC=/opt/MailScanner/etc/virus.scanners.conf
  else
    echo 'Cannot find your virus.scanners.conf file.'
    echo 'When this has finished, you will have to edit it'
    echo 'and change the entries for sophos and sophossavi'
    echo 'to point to the installation directory into which'
    echo 'you install Sophos version 5. It is the directory'
    echo 'containing the bin directory, where savscan exists.'
    echo 'The default is /opt/sophos-av'
    sleep 10
  fi
  export MSETC
  SOPHOS=''
  # Find their Sophos V5 installation directory
  if [ -f /usr/local/etc/savd.cfg ]; then
    SOPHOS=/usr/local
  elif [ -f /usr/local/sophos-av/etc/savd.cfg ]; then
    SOPHOS=/usr/local/sophos-av
  elif [ -f /opt/sophos-av/etc/savd.cfg ]; then
    SOPHOS=/opt/sophos-av
  else
    echo
    echo 'Cannot find your Sophos V5 installation.'
    echo 'When this has finished, you will have to edit your'
    echo 'MailScanner virus.scanners.conf file and set the sophos'
    echo 'and sophossavi entries to point to your Sophos'
    echo 'installation. It needs to be the directory that contains'
    echo 'the bin subdirectory with savscan and savupdate withing it.'
    echo 'By default, this will have been /opt/sophos-av.'
    sleep 10
  fi
  export SOPHOS

  # Add Sophos's lib dir to /etc/ld.so.conf and rebuild cache
  if [ -f /etc/ld.so.conf ]; then
    if [ "x$SOPHOS" = "x" ]; then
      echo 'If you want to use the "sophossavi" virus scanner, then you need'
      echo 'to add the full path of your Sophos "lib" directory to your'
      echo '/etc/ld.so.conf file and then run the command "ldconfig" before'
      echo 'you attempt to install the Perl-SAVI Perl module'.
    else
      if fgrep -q $SOPHOS/lib /etc/ld.so.conf; then
        echo Good, found $SOPHOS/lib in /etc/ld.so.conf
      else
        echo Adding the Sophos lib directory to your run-time linker config.
        echo $SOPHOS/lib >> /etc/ld.so.conf
      fi
      echo Refreshing your run-time linker cache...
      ldconfig
    fi
  fi

  if [ "x$MSETC" = "x" ]; then
    :
  else
    /usr/bin/perl -pi.bak -e 's%^(sophos\S*\s+\S+\s+)(\S+)(.*)$%$1'$SOPHOS'$3%' $MSETC
    echo 'I have updated your virus.scanners.conf file to point to'
    echo 'your new Sophos version 5 installation.'
  fi
  # prevent notifications from causing a mail loop
  $SOPHOS/bin/savconfig -u set EmailNotifier disabled
  # prevent autoupdates (let MailScanner do it instead)
  $SOPHOS/bin/savdctl stop sav-protect
  $SOPHOS/bin/savdctl disableOnBoot sav-protect 

  # Remove the Sophos root cron job
  sleep 5
  echo 'Commenting out root cron job.'
  rm -f /tmp/crontab.$$
  crontab -l | sed -e 's/^\(.*savupdate.*\)$/# &/' > /tmp/crontab.$$
  crontab /tmp/crontab.$$

  # Shutdown and kill off all the Sophos V5 daemons
  sleep 2
  echo 'Stopping and disabling unwanted Sophos daemons.'
  for F in sav-protect sav-rms sav-web
  do
    /etc/init.d/$F stop
    chkconfig $F off
  done

  exit 0
fi


SOPHOS=/usr/local/Sophos
SCRIPTS=/usr/lib/MailScanner
COMPD=linux.intel.libc6.tar.Z
DISTRIB=linux.intel.libc6.tar

mkdir -p ${SOPHOS}
chown -R root ${SOPHOS}
chmod -R go+rX ${SOPHOS}

# Clear out any old libs from /usr/local/lib which is where
# a default SAVI installation will have left them
echo Clearing out old default Sophos installation libraries
rm -f /usr/local/lib/libsavi.so*

# Have we got to uncompress the distribution for them?
if [ -f $COMPD ]; then
  echo Uncompressing Sophos distribution
  uncompress $COMPD >/dev/null 2>&1 || \
  gunzip     $COMPD >/dev/null 2>&1 || \
  /usr/local/bin/gunzip $COMPD >/dev/null 2>&1
fi

# Have we got to unpack the distribution for them?
if [ -f $DISTRIB ]; then
  # Is there an old unpacked distribution here too??
  if [ -d sav-install ]; then
    echo Clearing out unpacked distribution
    rm -rf sav-install
  fi
  # Unpack the distribution
  echo Unpacking distribution
  tar xBf $DISTRIB
fi

# JKF 31/08/2001 Remove any existing vdl.dat files
if [ -f ${SOPHOS}/lib/vdl.dat ]; then
  rm -f ${SOPHOS}/lib/vdl*
fi
if [ -f ${SOPHOS}/lib/vdln.dat ]; then
  rm -f ${SOPHOS}/lib/vdl*
fi
if [ -f ${SOPHOS}/lib/vdl01.vdb ]; then
  rm -f ${SOPHOS}/lib/vdl*
fi

# Are we in the right directory, or one above it?
if [ -d sav-install ]; then
  cd sav-install
fi

# Check we have found the install.sh script
if [ \! -f install.sh ]; then
  echo Please cd into the directory containing the Sophos install.sh
  echo script and run this command again.
  exit 1
fi

echo Installing Sophos for MailScanner
./install.sh -v -d ${SOPHOS} -s lib -ni

if [ -f /etc/sav.conf ]; then
  echo Removing troublesome and useless configuration file /etc/sav.conf
  rm -f /etc/sav.conf
fi

echo Creating links so Perl-SAVI module compiles
[ -e ${SOPHOS}/lib/libsavi.so.2 ] && ln -sf libsavi.so.2 ${SOPHOS}/lib/libsavi.so
[ -e ${SOPHOS}/lib/libsavi.so.3 ] && ln -sf libsavi.so.3 ${SOPHOS}/lib/libsavi.so

echo
echo Fetching latest IDE virus identities from www.sophos.com
${SCRIPTS}/sophos-autoupdate

if [ -f ${SCRIPTS}/sophos-wrapper ]; then
  chmod a=rx ${SCRIPTS}/sophos-wrapper
  echo Done.
else
  echo Something has gone wrong. There should be a copy of the
  echo script sophos-wrapper in the directory ${SCRIPTS}.
  echo Please re-install the MailScanner or fetch another copy of
  echo sophoswrapper from the distribution web site.
fi

exit 0
