#!/bin/bash
#/////////////////////////////////////////////////////////////////////////
#//
#// (c) University of Southampton IT Innovation Centre, 2018
#//
#// Copyright in this software belongs to University of Southampton
#// IT Innovation Centre of Gamma House, Enterprise Road,
#// Chilworth Science Park, Southampton, SO16 7NS, UK.
#//
#// This software may not be used, sold, licensed, transferred, copied
#// or reproduced in whole or in part in any manner or form or in or
#// on any media by any person other than in accordance with the terms
#// of the Licence Agreement supplied with the software, or otherwise
#// without the prior written consent of the copyright owners.
#//
#// This software is distributed WITHOUT ANY WARRANTY, without even the
#// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#// PURPOSE, except where stated in the Licence Agreement supplied with
#// the software.
#//
#//      Created By :            Michael Boniface, Nikolay Sanchev
#//      Created Date :          13/12/2018
#//      Created for Project :   FLAME
#//
#/////////////////////////////////////////////////////////////////////////

# status of neo4j can be found using: journalctl -e -u neo4j
# Directories: 
#/etc/neo4j/neo4j.conf
#/var/lib/neo4j/data
#/var/log/neo4j
#/var/lib/neo4j/metrics
#/var/lib/neo4j/import
#/usr/bin
#/usr/share/neo4j/lib
#/var/lib/neo4j/plugins
#
#admin tool is neo4j-admin
#
# bolt 7687
# http 7474 including the browser on localhost:7474
# https 7473

sudo apt update -o Acquire::CompressionTypes::Order::=gz
sudo apt install wget openjdk-8-jdk -y

# configure apt
wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add -
echo 'deb https://debian.neo4j.org/repo stable/' | sudo tee -a /etc/apt/sources.list.d/neo4j.list
apt-get update

# add neo4j user with sudo permissions
useradd --comment 'neo4j' --create-home neo4j --shell /bin/bash
echo "neo4j ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# install neo4j as neo4j user
# when installed as root the systemctl service fails to start as it expects to be run as neo4j user
# there's some directories owned as root that neo4j wants to write to but cannot
# the following set of commands install everything as neo4j with some run as sudo.
# Expected to only have to run the install as neo4j however it does not work unless all of these are
# run as neo4j. hence the weird su neo4j -c "sudo..."

echo "----->Installing neo4j"
su neo4j -c "sudo apt-get install neo4j=1:3.4.0 -y"

su neo4j -c "sed -i s/\#dbms.connectors.default_listen_address=0.0.0.0/dbms.connectors.default_listen_address=0.0.0.0/g /etc/neo4j/neo4j.conf"
su neo4j -c "sed -i s/\#dbms.connector.bolt.listen_address=:7687/dbms.connector.bolt.listen_address=0.0.0.0:7687/g /etc/neo4j/neo4j.conf"

# set initial password before starting 
su neo4j -c "neo4j-admin set-initial-password admin"

# install service
su neo4j -c "sudo systemctl enable neo4j"
su neo4j -c "sudo systemctl start neo4j"

### waiting for the service to start
echo "----->Waiting for neo4j service to start"
end="$((SECONDS+60))"
while true; do
    nc -w 2 localhost 7687 && break
    [[ "${SECONDS}" -ge "${end}" ]] && exit 1
    sleep 1
done
