#!/bin/sh

set -e

if [ `id -u` -ne 0 ]; then
    echo "ERROR you need to be root to use this script"
    exit 1
fi

echo "\n Starting PostgreSQL service"

systemctl start postgresql
if ! systemctl is-active --quiet postgresql; then
    echo "ERROR: PostgreSQL failed to start" 1>&2
    systemctl --no-pager -l status postgresql
    exit 1
fi

echo "\n Creating Database"

runuser -u postgres -- /usr/share/bloodhound/create-database-bloodhound

echo "\n Starting neo4j"
if ! neo4j status; then
    neo4j start
fi

echo "\n Setup the new password of neo4j"
echo "\n IMPORTANT: Once you have setup the new password, please update /etc/bhapi/bhapi.json with the new password before running bloodhound"

sleep 5s

xdg-open "http://localhost:7474/"
