#!/bin/bash

# Author: Tomas Kirnak
# Copyright: NetCore j.s.a.
# Version: 2018-09-17-01

echo;

# check if root
user=$(whoami);
if [[ $user != "root" ]]; then
    echo 'This installer requires root privileges.';
    echo 'Please switch to root and run the installer again.';
    echo;
    exit 1;
fi;

# check which OS we are installing on
if cat /etc/*-release |& grep 'CentOS' &> /dev/null; then
    installer='https://unimus.net/download/linux/centos/install.sh';

elif cat /etc/*-release |& grep 'Raspbian' &> /dev/null; then
    installer='https://unimus.net/download/linux/rpi/install.sh';

elif cat /etc/*-release |& grep -P 'Debian|Ubuntu' &> /dev/null; then
    installer='https://unimus.net/download/linux/deb/install.sh';

else
    echo "We are sorry, but the installer currently doesn't support your OS.";
    echo 'Please check our wiki for generic Linux / Unix / Mac install instructions.';
    echo;
    exit 1;
fi;

# check available entropy on the system
bash <(curl -LsS 'https://unimus.net/download/linux/entropy/check_entropy.sh');

# inform the user what this installer will do
echo 'Welcome to the Unimus installer!';
echo;
echo 'This installer will perform the following steps:';
echo '1) Check for a compatible Java version';
echo '2) Download the latest version of Unimus';
echo '3) Download the service init/unit files';
echo '4) Configure Unimus to start at boot';
echo '5) Start Unimus';
echo;
echo 'If you are upgrading from a previous version, your current Unimus service will be stopped and restarted after the upgrade.';
echo;
echo 'If you experience any issues with this installer, or have any questions, please contact us.';
echo '(email, website live-chat, forums, create a support ticket, etc.)';
echo;
echo 'Press ENTER to continue, or Ctrl+C to exit.';

read -s;
echo;

# run appropriate installer
bash <(curl -LsS "$installer");

# exit with the installers exit code
exit $?;
