#!/bin/bash
source /usr/local/lib/hostsharing/hsdb
source /usr/local/lib/hostsharing/pac

if [ -z "$1" ]; then
        echo "ERROR: target missing" >&2
        echo "SAMPLE: postfix-rebuild-aliases /etc/postfix-mailin/aliases.db" >&2
        exit 1
fi
target=`echo $1 | sed 's/\.db$//'`

hive=`hostname -s`

HSDB "SELECT name || ' ' || target FROM emailalias JOIN packet ON (packet.packet_id=emailalias.pac_id) NATURAL JOIN hive WHERE hive_name='$hive'" |
 sed -r -e 's/^[[:space:]]+//;' | sort -u >$target

test -f $target.db && rm $target.db
postmap $target
rm $target
