#!/bin/bash
# This script runs the same sequence as the CircleCI build

if [ $(uname) == "Darwin" ]; then
  if which brew >/dev/null; then
    echo "Brew is already installed"
  else
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  fi
fi

# install nodejs
if which node >/dev/null; then
  echo "Nodejs is already installed"
else
  if [ $(uname) == "Darwin" ]; then
    brew install node
  else
    curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
    sudo apt-get install -y nodejs
  fi
fi

# install python
if which python3 >/dev/null; then
  echo "Python3 is already installed"
else
  if [ $(uname) == "Darwin" ]; then
    brew install python3
  else
    sudo apt-get -y install python3-dev python3-pip python3-setuptools build-essential
  fi
fi

# install pip modules
if python3 -m pip show spacy socketIO-client >/dev/null; then
  echo "Pip modules already installed"
else
  python3 -m pip install -U socketIO-client
  python3 -m pip install -U spacy
  python3 -m spacy.en.download
fi

# install npm modules
if [ -d ./node_modules ]; then
  echo "Npm modules already installed"
else
  npm install
fi

# install neo4j
if which neo4j >/dev/null; then
  echo "Neo4j is already installed"
else
  if [ $(uname) == "Darwin" ]; then
    brew install neo4j
  else
    wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add -
    echo 'deb http://debian.neo4j.org/repo stable/' | sudo tee /etc/apt/sources.list.d/neo4j.list
    sudo apt-get update
    sudo apt-get -y install neo4j
  fi
fi

# start neo4j
UP=$(pgrep neo4j | wc -l);
if [ "$UP" -eq 0 ];
then
  if [ $(uname) == "Darwin" ]; then
    neo4j start
  else
    service neo4j start
  fi
else
  echo "Neo4j is already started"
fi

# copy keys file if not already exist
# BIN_DIR=`pwd`/bin
# $BIN_DIR/copy-config
