#!/bin/sh

config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif [ "`cat $OLD | md5sum`" = "`cat $NEW | md5sum`" ]; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

config etc/clamd.conf.new
config etc/freshclam.conf.new

touch var/log/clamd.log
touch var/log/freshclam.log

###
# Create clamav:user and group if thet don't exist.
###
group_exists=`grep ^clamav etc/group`
if [[ "${group_exists}" == "" ]]; then
	groupadd -g 210 clamav
fi
user_exists=`grep ^clamav  etc/passwd`
if [[ "${user_exists}" == "" ]]; then
	useradd -c "Clamav Antivirus" -d /dev/null  -u 210  -g clamav clamav -s /bin/false
fi


chown clamav usr/sbin/clamav-milter
chmod 4700 usr/sbin/clamav-milter
chown -R clamav:clamav var/run/clamav
chmod 771 var/run/clamav
chown clamav:clamav var/log/{clamd.log,freshclam.log}
chmod 660 var/log/{clamd.log,freshclam.log}
chown -R clamav:clamav var/lib/clamav
chmod -R 770 var/lib/clamav
chmod -R 660 var/lib/clamav/*

( cd usr/lib ; rm -rf libclamav.so )
( cd usr/lib ; ln -sf libclamav.so.6.1.7 libclamav.so )
( cd usr/lib ; rm -rf libclamunrar.so.6 )
( cd usr/lib ; ln -sf libclamunrar.so.6.1.7 libclamunrar.so.6 )
( cd usr/lib ; rm -rf libclamunrar_iface.so )
( cd usr/lib ; ln -sf libclamunrar_iface.so.6.1.7 libclamunrar_iface.so )
( cd usr/lib ; rm -rf libclamav.so.6 )
( cd usr/lib ; ln -sf libclamav.so.6.1.7 libclamav.so.6 )
( cd usr/lib ; rm -rf libclamunrar_iface.so.6 )
( cd usr/lib ; ln -sf libclamunrar_iface.so.6.1.7 libclamunrar_iface.so.6 )
( cd usr/lib ; rm -rf libclamunrar.so )
( cd usr/lib ; ln -sf libclamunrar.so.6.1.7 libclamunrar.so )
