#!/bin/sh
#
# Script to install node JS, ERS and PM2 package, and then start ERS.
#

# Get the needed parameters
ERS_VER=1.0.0
OS=linux
ARCH=x86
VER=4.1.1
NODE_JS=http://nodejs.org/dist/v$VER/node-v$VER-$OS-$ARCH.tar.gz

# Download and extract needed node JS
curl $NODE_JS | tar xzf - --strip-components=1
if [ $? != 0 ]; then echo "Could not download node binary: $NODE_JS!"; exit 1; fi

# Install dependencies
echo 'Installing ERS...'
bin/npm install http://tarballs.evostream.com/ers/1.0/ers-$ERS_VER.tgz
if [ $? != 0 ]; then echo 'Could not install ERS!'; exit 1; fi

echo 'Installing pm2...'
bin/npm install pm2 -g
if [ $? != 0 ]; then echo 'Could not install pm2!'; exit 1; fi

# Change to where ERS is
cd node_modules/ers

# Start ERS
echo 'Starting ERS...'
PATH="$PATH:../../bin" pm2 start ers.js
if [ $? != 0 ]; then echo 'Could not start ERS!'; exit 1; fi

exit 0
