#!/bin/sh

set -e


if [ `id -u` -ne 0 ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

systemctl start cassandra

if [ ! -d /var/lib/thp/thehive/index/global ]; then
    echo "It seems this is the first time you start thehive"
    echo "Please wait..."
    sleep 60s
    cqlsh -e "UPDATE system.local SET cluster_name='thp' where key='local';"
    nodetool flush
    sed -i -E "s/^cluster_name:.*/cluster_name: 'thp'/" /etc/cassandra/cassandra.yaml
    systemctl restart cassandra
fi

systemctl start thehive

if [ ! -d /var/lib/thp/thehive/index/global ]; then
    echo "The fist start is very long (several minutes)"
    echo "You will have to reload the page"
    echo "Default User: admin"
    echo "Default Password: secret"
fi

echo "Opening Web UI http://127.0.0.1:9000"
xdg-open "http://127.0.0.1:9000" 2>/dev/null >/dev/null &
