#!/bin/sh
# This is a post-install script which will update your ClamAV config
# in order to use it with qscanq-psa package. Please refer to
# http://www.hostbird.com for more details.
#
# Copyright by Ales Djordjevic, 2004. All rights reserved.
# We can be contacted at projects@hostbird.com

# TODO: check if certain files exist before trying to delete them, etc.
# Purely cosmetic... Perhaps in 2.0 ;)

# version
clamav_ver=clamav-0.91-x.dag
ver=v1.55

alter() {

# let's stop the daemons first, if they're running
service clamd stop
service freshclam stop

# change the settings of the /var/run/clamav dir and any stale contents
chown gqscanq.clamav /var/run/clamav -R
chmod 770 /var/run/clamav

# change the settings of the /var/log/clamav dir and contents (if any)
chown gqscanq.clamav /var/log/clamav -R
chmod 775 /var/log/clamav
chmod 660 /var/log/clamav/*

# change the settings of the /var/clamav dir and contents (if any)
chown clamav.clamav /var/clamav -R

#get rid of some junk, if it exists
rm /etc/logrotate.d/freshcla*
rm /etc/logrotate.d/clamd
rm /etc/logrotate.d/clamav
rm /etc/cron.daily/freshcla*
rm /var/clamav/daily.cvd.rpmne*
rm /var/clamav/main.cvd.rpmne*

# recreate the logrotate script for clamd
echo "#"				> /etc/logrotate.d/clamav
echo "# Rotate Clam AV daemon log file" >> /etc/logrotate.d/clamav
echo "#"				>> /etc/logrotate.d/clamav
echo ""					>> /etc/logrotate.d/clamav
echo "/var/log/clamav/clamd.log {"	>> /etc/logrotate.d/clamav
echo "  missingok"			>> /etc/logrotate.d/clamav
echo "  nocompress"			>> /etc/logrotate.d/clamav
echo "  create 660 gqscanq clamav"	>> /etc/logrotate.d/clamav
echo "  postrotate"			>> /etc/logrotate.d/clamav
echo "	    /bin/kill -HUP \`cat /var/run/clamav/clamd.pid 2> /dev/null\` 2> /dev/null || true" >> /etc/logrotate.d/clamav
echo "  endscript"			>> /etc/logrotate.d/clamav
echo "}"				>> /etc/logrotate.d/clamav

# recreate the logrotate script for freshclam
echo "#"				> /etc/logrotate.d/freshclam
echo "# Rotate FreshClam daemon log file"	>> /etc/logrotate.d/freshclam
echo "#"				>> /etc/logrotate.d/freshclam
echo ""					>> /etc/logrotate.d/freshclam
echo "/var/log/clamav/freshclam.log {"	>> /etc/logrotate.d/freshclam
echo "  missingok"			>> /etc/logrotate.d/freshclam
echo "  nocompress"			>> /etc/logrotate.d/freshclam
echo "  create 660 gqscanq clamav"	>> /etc/logrotate.d/freshclam
echo "  postrotate"			>> /etc/logrotate.d/freshclam
echo "	    /bin/kill -HUP \`cat /var/run/clamav/freshclam.pid 2> /dev/null\` 2> /dev/null || true" >> /etc/logrotate.d/freshclam
echo "  endscript"			>> /etc/logrotate.d/freshclam
echo "}"				>> /etc/logrotate.d/freshclam

# recreate the freshclam init script (Dag doesn't use it as a daemon...)
echo "#!/bin/sh"						> /etc/init.d/freshclam
echo "#"							>> /etc/init.d/freshclam
echo "# freshclam      Start/Stop the freshclam."		>> /etc/init.d/freshclam
echo "#"							>> /etc/init.d/freshclam
echo "# chkconfig: - 62 38"					>> /etc/init.d/freshclam
echo "# description: freshclam is an update daemon for Clam AV database." >> /etc/init.d/freshclam
echo "#"							>> /etc/init.d/freshclam
echo "# processname: freshclam"					>> /etc/init.d/freshclam
echo "# config: /etc/freshclam.conf"				>> /etc/init.d/freshclam
echo "# pidfile: /var/run/clamav/freshclam.pid"			>> /etc/init.d/freshclam
echo "#"							>> /etc/init.d/freshclam
echo "# (c) 2004/05/17 Petr@Kristof.CZ under GNU GPL 2.0+"	>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "# Source function library"				>> /etc/init.d/freshclam
echo ". /etc/init.d/functions"					>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "# Get network config"					>> /etc/init.d/freshclam
echo ". /etc/sysconfig/network"					>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "test -f /etc/freshclam.conf || exit 0"			>> /etc/init.d/freshclam
echo "RETVAL=0"							>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "start() {"						>> /etc/init.d/freshclam
echo "		echo -n $\"Starting freshclam: \""		>> /etc/init.d/freshclam
echo "		# Start me up!"					>> /etc/init.d/freshclam
echo "	        daemon /usr/bin/freshclam -d -p /var/run/clamav/freshclam.pid" >> /etc/init.d/freshclam
echo "	        RETVAL=\$?"					>> /etc/init.d/freshclam
echo "	        echo"						>> /etc/init.d/freshclam
echo "	        [ \$RETVAL -eq 0 ] && touch /var/lock/subsys/freshclam" >> /etc/init.d/freshclam
echo "	        return \$RETVAL"				>> /etc/init.d/freshclam
echo "		}"						>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "stop() {"							>> /etc/init.d/freshclam
echo "	        echo -n $\"Stopping freshclam: \""		>> /etc/init.d/freshclam
echo "	        killproc freshclam"				>> /etc/init.d/freshclam
echo "	        RETVAL=\$?"					>> /etc/init.d/freshclam
echo "		echo"						>> /etc/init.d/freshclam
echo "		[ \$RETVAL -eq 0 ] && rm -f /var/run/clamav/freshclam.pid /var/lock/subsys/freshclam" >> /etc/init.d/freshclam
echo "	        return \$RETVAL"				>> /etc/init.d/freshclam
echo "		}"						>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "restart() {"						>> /etc/init.d/freshclam
echo "	        stop"						>> /etc/init.d/freshclam
echo "	        start"						>> /etc/init.d/freshclam
echo "		}"						>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "reload() {"						>> /etc/init.d/freshclam
echo "	        echo -n $\"Reloading DB: \""			>> /etc/init.d/freshclam
echo "	        killproc freshclam -ALRM"			>> /etc/init.d/freshclam
echo "	        RETVAL=\$?"					>> /etc/init.d/freshclam
echo "	        echo"						>> /etc/init.d/freshclam
echo "	        return \$RETVAL"				>> /etc/init.d/freshclam
echo "		}"						>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "case \"\$1\" in"						>> /etc/init.d/freshclam
echo "	start)"							>> /etc/init.d/freshclam
echo "		start"						>> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	stop)"							>> /etc/init.d/freshclam
echo "		stop"						>> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	status)"						>> /etc/init.d/freshclam
echo "	status freshclam"					>> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	restart)"						>> /etc/init.d/freshclam
echo "		restart"					>> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	condrestart)"						>> /etc/init.d/freshclam
echo "		[ -f /var/lock/subsys/freshclam ] && restart || :" >> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	reload)"						>> /etc/init.d/freshclam
echo "		reload"						>> /etc/init.d/freshclam
echo "		;;"						>> /etc/init.d/freshclam
echo "	*)"							>> /etc/init.d/freshclam
echo "echo $\"Usage: \$0 {start|stop|status|restart|condrestart|reload}\"" >> /etc/init.d/freshclam
echo "exit 1"							>> /etc/init.d/freshclam
echo "esac"							>> /etc/init.d/freshclam
echo ""								>> /etc/init.d/freshclam
echo "exit \$?"							>> /etc/init.d/freshclam

# chmod the freshclam init script
chmod 755 /etc/init.d/freshclam

# make sure clamd and freshclam are set to start after a server reboot
chkconfig clamd on
chkconfig freshclam on

# start the daemons again
# we're using restart here just in case some service monitor restarted
# these in the mean time... doesn't hurt
service clamd restart
service freshclam restart

# wrap it up...
echo ""
echo "NOTE: some FAILED statuses and missing files above are perfectly normal."
echo ""
echo "The modifications to /var/run/clamav, /var/log/clamav, /var/clamav,"
echo "logrotate, cron and startup scripts are done."
echo ""
echo "Make sure the clamd and freshclam daemon are running and that there"
echo "are no errors in the log files."
echo ""
}

case "$1" in
alter)
  alter
  ;;
*)
# display the version info, instructions for /etc/clamd.conf and freshclam.conf
# and other instructions
echo ""
echo "This is the qscanq-psa post-install config script $ver for ClamAV,"
echo "valid for $clamav_ver. If you have any other version of"
echo "ClamAV, this script might damage your instalation."
echo ""
echo "You should also modify your /etc/clamav.conf and /etc/freshclam.conf"
echo "before running this script."
echo ""
echo "For instructions on how to do that and to get the latest version of"
echo "this script, please visit the project's homepage at"
echo "http://www.hostbird.com"
echo ""
echo "In order to execute the script, invoke it with 'alter' switch, e.g.:"
echo "./clamav_alter.sh alter"
echo ""
exit 1
esac

