#!/bin/sh

set -e

PORT=8080
URL="http://127.0.0.1:${PORT}"

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

if grep -q '"secret": "neo4j"' /etc/bhapi/bhapi.json; then
    echo "\n It seems it's the first time you run bloodhound"
    echo "\n Please run bloodhound-setup first"
    exit 0
fi

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 Starting neo4j"
if ! neo4j status; then
    neo4j start
fi

## Start Bloodhound
echo "\n Bloodhound will start"
echo "\n IMPORTANT: It will take time, please wait...\n"

runuser -u _bloodhound -- /usr/lib/bloodhound/bin/bhapi
