#! /bin/bash
# Generated by clamav.SlackBuild for clamav-0.88.6-i486-1McD_slack10.0.tgz on 05-Nov-2006
# By Martijn Dekker <martijn@inlv.org>
# Based on install script by Jim Simmons for ClamAV 0.81.

function upd_conf {
 	# If there's no config file by that name, mv it over:
 	if ! test -f $1; then
 		mv $1.new $1
 	elif diff --brief $1 $1.new; then	# toss the redundant copy
 		rm $1.new
 	elif ! test -s $1.new; then
 		rm $1.new   # do not keep empty initial files (e.g. log)
 	fi
 	# Otherwise, we leave the .new copy for the admin to consider...
}

function free_user_id {
 	# Find a free user-ID >= 100 (should be < 1000 so it's not a normal user)
 	local FREE_USER_ID=100
 	while grep -q "^.*:.*:$FREE_USER_ID:.*:.*:.*:" /etc/passwd; do
 		let FREE_USER_ID++
 	done
 	echo $FREE_USER_ID
}

# Update configuration files
{
	upd_conf etc/rc.d/rc.clamav
	upd_conf etc/cron.daily/clamav-update
	upd_conf etc/logrotate.d/clamav-milter
	upd_conf etc/clamd.conf
	upd_conf etc/freshclam.conf
} | sed 's/^/[1mConfig alert: [0m/'

# Initialize log files if not already present
upd_conf var/log/clamd 2>&1 > /dev/null
upd_conf var/log/clamav-update 2>&1 > /dev/null

# Set up group
grep -q '^clamav:' etc/group || groupadd clamav

# Set up user: add it if it doesn't exist, update it if it already does.
# In any case, make sure it has or gets a user ID between 100 and 999.
echo "Setting up user 'clamav' and group 'clamav'."
if EXISTING_ENTRY=$(grep '^clamav:' /etc/passwd); then
 	ACTION=mod				# Modify existing user
 	USER_ID=$(echo $EXISTING_ENTRY | cut --fields=3 --delimiter=':')
 	if test $USER_ID -ge 1000; then	# Modify existing uid if >= 1000
 		USER_ID=$(free_user_id)
 	fi
else
 	ACTION=add				# Add non-existent user
 	USER_ID=$(free_user_id)
fi
user$ACTION -g clamav -d /var/lib/clamav -c "ClamAV Antivirus Daemon"		\
 	-u $USER_ID -s /bin/false clamav

# Set up permissions
chown --recursive clamav:clamav var/lib/clamav var/run/clamav-milter		\
 	var/log/clamav-update var/log/clamd
chown root:clamav usr/sbin/clamav-milter usr/sbin/clamd

# Make us start on boot
if ! grep -q "rc.clamav start" etc/rc.d/rc.local; then
 	echo "Adding ClamAV to /etc/rc.d/rc.local so it starts at bootup time."
 	{	echo ""
 		echo "# ClamAV, the open source anti-virus scanner"
 		echo "test -x /etc/rc.d/rc.clamav && . /etc/rc.d/rc.clamav start"
 	} >> etc/rc.d/rc.local
else
 	echo "ClamAV is already in /etc/rc.d/rc.local ."
fi

# Generate Sendmail .mc and .cf files with support for the ClamAV milter
CLMILTER_MC_CMD="dnl# ClamAV milter integration from clamav-0.88.6-i486-1McD_slack10.0.tgz:
INPUT_MAIL_FILTER(\`clamav', \`S=local:/var/run/clamav-milter/milter.sock, F=, T=S:4m;R:4m')dnl
define(\`confINPUT_MAIL_FILTERS', \`clamav')dnl
"
if test -r usr/share/sendmail/cf/cf/sendmail-slackware.mc &&
 	test -x usr/sbin/sendmail && test -x usr/bin/m4
then
 	cd usr/share/sendmail/cf/cf

 	for SLACK in sendmail-slackware sendmail-slackware-tls; do
 		SLACK_MC=$SLACK.mc
 		CLAM_MC=$SLACK-clamav.mc
 		CLAM_CF=$SLACK-clamav.cf
 		if test -f $SLACK_MC; then
 			echo "Generating /usr/share/sendmail/$CLAM_CF ..."
 			sed "/VERSIONID/ s/')dnl/ with ClamAV milter support')dnl/" $SLACK_MC > $CLAM_MC
 			echo "$CLMILTER_MC_CMD" >> $CLAM_MC
 			m4 ../m4/cf.m4 $CLAM_MC > ../../$CLAM_CF
 		else
 			echo "WARNING: Cannot generate /usr/share/sendmail/$CLAM_CF because"
 			echo "	 the file /usr/share/sendmail/cf/cf/$SLACK_MC doesn't exist."
 			rm $CLAM_MC $CLAM_CF	# remove dummies
 		fi
 	done

 	cd ../../../../..
 	echo "[1mTo enable Sendmail integration, copy one of these .cf files to[0m"
 	echo "[1m/etc/mail/sendmail.cf, then start ClamAV and restart Sendmail.[0m"
else
 	# if we can't make 'em, remove the dummies
 	rm usr/share/sendmail/sendmail-slackware-clamav.cf		\
 		usr/share/sendmail/sendmail-slackware-tls-clamav.cf	\
 		usr/share/sendmail/cf/cf/sendmail-slackware-clamav.mc	\
 		usr/share/sendmail/cf/cf/sendmail-slackware-tls-clamav.mc
 	echo "[1;31mWARNING: [0mThe ClamAV-enabled config files for Sendmail could not be generated."
 	echo "	 If you want ClamAV to work with Sendmail, first make sure the"
 	echo "	 [1msendmail[0m, [1msendmail-cf[0m, and [1mm4[0m packages for Slackware are installed,"
 	echo "	 and then do: [31m# [32mupgradepkg --reinstall clamav-0.88.6-i486-1McD_slack10.0.tgz[0m"
 	echo "	 If Sendmail support is not desired, ignore this warning."
fi

echo "This package does [1mnot[0m include a virus database."
echo "[1;31mYou should update your virus database right away with \`freshclam'.[0m"

# Make symlinks (generated by makepkg)
( cd usr/lib ; rm -rf libclamav.so )
( cd usr/lib ; ln -sf libclamav.so.1.0.18 libclamav.so )
( cd usr/lib ; rm -rf libclamav.so.1 )
( cd usr/lib ; ln -sf libclamav.so.1.0.18 libclamav.so.1 )
