#!/bin/sh
# Copyright (C) 2008 Vladimir V. Kamarzin <vvk@altlinux.org>
#
# synchronize dnswl.org lists for postfix
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

# we need libshell functions
. shell-error
. shell-quote

# work variables, may be overwritten in config
datadir=/var/lib/dnswl-sync
tmpdir=$datadir/tmp

# check that we have config
[ -r /etc/dnswl/dnswl-postfix.conf ] && . /etc/dnswl/dnswl-postfix.conf \
	|| fatal "cannot source config file /etc/dnswl/dnswl-postfix.conf"

# copy existent files to temp dir
cp $datadir/postfix-* $tmpdir/ && have_old=1 || message "failed to copy dnswl-files to temp dir, first run?"

[ x"$have_old" != x1 ] || {
	# restore original header name for traffic economy
	[ "$header_orig" = "$header_new" ] || \
		sed -i -e "s/$(quote_sed_regexp "$header_new")/$(quote_sed_regexp "$header_orig")/" \
		$tmpdir/postfix-dnswl-header

	# restore original action for traffic economy
	[ "$permit_orig" = "$permit_new" ] || \
		sed -i -e "s/$(quote_sed_regexp "$permit_new")/$(quote_sed_regexp "$permit_orig")/" \
			$tmpdir/postfix-dnswl-permit
}

# do sync
rsync -vaP $server::dnswl/postfix-* $tmpdir/ \
	|| fatal "failed to complete synchronization, aborting"

# change header name
[ "$header_orig" = "$header_new" ] || \
	sed -i -e "s/$(quote_sed_regexp "$header_orig")/$(quote_sed_regexp "$header_new")/" \
		$tmpdir/postfix-dnswl-header

# change action name
[ "$permit_orig" = "$permit_new" ] || \
	sed -i -e "s/$(quote_sed_regexp "$permit_orig")/$(quote_sed_regexp "$permit_new")/" \
		$tmpdir/postfix-dnswl-permit

# move new lists to work location
mv -f $tmpdir/postfix-* $datadir/

chown root:root $datadir/*
