#!/bin/bash

# Run as root or insert `sudo -E` before `bash`:
#
# wget -qO- https://deb.splynx.com/setup | bash -
#
export DEBIAN_FRONTEND=noninteractive

p_status() {
    echo
    echo "# $1"
    echo
}

err() {
    echo 'Error, exiting'
    exit 1
}

exec_cmd_noerror() {
    echo "$1"
    bash -c "$1"
}

exec_cmd() {
    exec_cmd_noerror "$1" || err
}

p_status 'Adding NodeSource repository'

if [ -x /usr/bin/curl ]; then
    exec_cmd 'curl -sL https://deb.nodesource.com/setup_10.x | bash -'
else
    exec_cmd 'wget -qO- https://deb.nodesource.com/setup_10.x | bash -'
fi

RELEASE=$(lsb_release -c -s)
DIST=''

ubuntu=("trusty" "xenial")
debian=("jessie" "stretch")

if [[ " ${ubuntu[*]} " == *" $RELEASE "* ]]; then
DIST='ubuntu'
fi

if [[ " ${debian[*]} " == *" $RELEASE "* ]]; then
DIST='debian'
fi

if [[ "$DIST" == "" ]]; then
echo "Please use supported linux release: Ubuntu 16.04 (server, 64bit), or Debian 9."
err;
fi


p_status 'Adding the Splynx signing key to your keyring...'

if [ -x /usr/bin/curl ]; then
    exec_cmd 'curl -s https://splynx.com/splynx.gpg.key | apt-key add -'
else
    exec_cmd 'wget -qO- https://splynx.com/splynx.gpg.key | apt-key add -'
fi

p_status 'Creating apt sources list file for the Splynx repo...'

exec_cmd "echo 'deb https://deb.splynx.com/ splynx main' > /etc/apt/sources.list.d/splynx.list"

p_status 'Running `apt-get update` for you...'

exec_cmd 'apt-get update'

unset DEBIAN_FRONTEND


