#!/usr/bin/env bash

NSM_VERSION="${1:-0.1.2}"
echo "- NSM_VERSION=$NSM_VERSION"

NSM_PACKAGE_HOST="34.224.169.119"
NSM_PACKAGE_PORT="8888"
NSM_PACKAGE_URL="http://$NSM_PACKAGE_HOST:$NSM_PACKAGE_PORT/$NSM_VERSION/nodus-server-manager-$NSM_VERSION.tgz"
NSM_INSTALL_DIR="/usr/local/src"
NSM_PATH="$NSM_INSTALL_DIR/nodus-server-manager"
NSM_USER="nodus"
SYSTEMD_DIR="/etc/systemd/system"

# Exit on Error
set -e

# Variables
PAUSE=2

# Ensure script is running as 'root' or 'sudo'
if [ "$EUID" -ne 0 ]; then
    echo "Please run as root (or sudo)..."
    exit
fi

#### STEP 1: Install NodeJS
echo "STEP 1: Installing NodeJS..."

# Ensure nodejs is installed
NODE=`which node`
if [ -z "$NODE" ]; then

    echo "- Adding 'NODEJS' package repository..."
    curl --silent --location https://rpm.nodesource.com/setup_7.x | bash -

    echo "- Installing build tools..."
    yum -y install gcc-c++ make

    echo "- Installing NodeJS..."
    yum -y install nodejs
fi

# Capture the NODEJS version installed
NODE_VERSION=`node --version`

echo
echo "*** NodeJS ($NODE_VERSION) has been installed sucessfully. ***"
echo
sleep $PAUSE

### STEP 2: Create 'nodus' user account
echo "STEP 2: Creating 'nodus' user account..."

if id "$NSM_USER" >/dev/null 2>&1; then
    echo
    echo "   'nodus' user already exists..."
    echo
else
    # Add system user account
    echo "- Adding 'nodus' system user account..."

    # Add the 'nodus' user as a system account with no login and autogenerated password
#    adduser -r nodus
    useradd -r nodus

    echo
    echo "*** Successfully created 'nodus' user account. ***"
    echo
fi

sleep $PAUSE

### STEP 3: Install 'nodus-server-manager' source code...

echo "STEP 3: Installing 'nodus-server-manager' source code..."

echo "- Creating temporary directory..."
TMP_DIR=`mktemp -d`

echo "- Changing to temp directory '$TMP_DIR'..."
cd $TMP_DIR

echo "- Downloading 'nodus-server-manager' release from '$NSM_PACKAGE_URL'..."
curl -lO "$NSM_PACKAGE_URL"

echo "- Creating 'nodus-server-manager' install directory '$NSM_INSTALL_DIR'..."
mkdir -p "$NSM_INSTALL_DIR"

echo "- Extracting 'nodus-server-manager' release..."
tar -xvf nodus-server-manager-$NSM_VERSION.tgz --directory "$NSM_INSTALL_DIR"

echo "- Ensuring 'nodus-server-manager' directory exists..."
if [ ! -d "$NSM_INSTALL_DIR" ]; then
    echo "ERROR: 'nodus-server-manager' path not found: $NSM_PATH..."
    exit 1;
fi

echo
echo "*** 'nodus-server-manager' source has been successfully installed to "$NSM_PATH". ***"
echo
sleep $PAUSE


### STEP 4: Install 'nodus-server-manager' Dependencies
echo "STEP 4: Installing 'nodus-server-manager' Dependencies..."

echo "- Navigating to 'nodus-server-manager' source directory..."
cd $NSM_PATH

echo "- Installing dependencies..."
./install-deps.sh

echo
echo "*** Successfully installed 'nodus-server-manager' dependencies. ***"
echo
sleep $PAUSE

### STEP 6: Install 'nsm-agent' init service..."
echo "STEP 6: Installing 'nsm-agent' system service..."

echo "- Copying 'nsm-agent.service' init script to '$SYSTEMD_DIR'..."
cp "$NSM_PATH/init-scripts/nsm-agent.service" "$SYSTEMD_DIR"

echo "- Enabling 'systemd' init script..."
systemctl enable nsm-agent.service

echo "- Starting 'nsm-agent' service..."
systemctl start nsm-agent.service

echo
echo "*** Successfully enabled and started the 'nsm-agent' service. ***"
echo
echo "  TYPE: 'systemctl status nsm-agent.service' to ensure the service is operational..."

