#!/bin/sh
set -ev

# Remove service
rm -f /etc/init.d/pandora
rc-update -u

# Drop database and user
[ ! -e /run/openrc/started/postgres ] && service postgres start && STOP_POSTGRES=1
echo 'DROP DATABASE IF EXISTS pandora; DROP ROLE IF EXISTS pandora;' | lxc-attach -u 5432 -g 5432 postgres -- psql
[ ! -z ${STOP_POSTGRES} ] && service postgres stop

# Remove RabbitMQ vhost and user
[ ! -e /run/openrc/started/rabbitmq ] && service rabbitmq start && STOP_RABBITMQ=1
lxc-attach rabbitmq -- rabbitmqctl delete_vhost /pandora || true
lxc-attach rabbitmq -- rabbitmqctl delete_user pandora || true
[ ! -z ${STOP_RABBITMQ} ] && service rabbitmq stop

# Unregister application
vmmgr unregister-app pandora