#!/bin/bash
# This script merge the xivo database into the asterisk database and
# then drops the xivo database and the xivo user.
#
# It must be executed as user postgres.

set -e
export PGOPTIONS='--client-min-messages=warning'

psql="psql -v ON_ERROR_STOP="

# merge xivo database into asterisk database
pg_dump xivo | $psql asterisk

# set the owner of all objects to asterisk
pg_dump -s asterisk | grep -E '^ALTER.*OWNER TO' | sed -e 's/OWNER TO.*;$/OWNER TO asterisk;/' | $psql asterisk

# drop database xivo and role xivo
$psql asterisk <<'EOF'
DROP OWNED BY xivo;

\connect postgres
DROP DATABASE xivo;
DROP ROLE xivo;

EOF
