#!/bin/bash

#####################
# stop the milter if it is already installed
if [ -f /etc/rc.d/init.d/dnsbl ]; then
    /etc/rc.d/init.d/dnsbl stop
    rm -f /etc/rc.d/init.d/dnsbl
fi


#####################
# build the milter
# add compiler flags - suggested by Nigel Horne
g++ -c $CXXFLAGS -pthread dnsbl.cpp
if [ $? -ne 0 ]; then
    echo "compiler errors"
    exit
fi
g++ -o dnsbl dnsbl.o /usr/lib/libresolv.a -lmilter -pthread
if [ $? -ne 0 ]; then
    echo "linker errors"
    exit
fi


#####################
# ensure the user is created
/usr/bin/getent passwd dnsbl || /usr/sbin/useradd -r -d /etc/dnsbl -M -c "dnsbl pseudo-user" -s /sbin/nologin dnsbl
# install the milter
DST=/etc/dnsbl
mkdir -p $DST
if [ -f /var/dnsbl/dnsbl.conf ]; then
    # move the conf files to the new location
    mv /var/dnsbl/*conf $DST
    rm /var/dnsbl/dnsbl # remove the old binary
    rmdir /var/dnsbl
fi
if [ ! -f $DST/dnsbl.conf ]; then
    cp dnsbl.conf $DST
fi
if [ ! -f $DST/html-tags.conf ]; then
    cp html-tags.conf $DST
fi
if [ ! -f $DST/tld.conf ]; then
    cp tld.conf $DST
fi

# make the directory for the socket
mkdir -p          /var/run/dnsbl
chown dnsbl:dnsbl /var/run/dnsbl
chmod 700         /var/run/dnsbl

# install the binaries
mv -f dnsbl /usr/sbin/dnsbl
cp dnsbl.rc /etc/rc.d/init.d/dnsbl
chmod 755 /etc/rc.d/init.d/dnsbl
/sbin/chkconfig --add dnsbl
/sbin/chkconfig --level 2345 dnsbl on
/etc/rc.d/init.d/dnsbl start
