#!/bin/bash
source ./scripts/make/constants
source /etc/os-release

printf "Installing MMPM GUI Dependencies\n"

log_notice

log_action "Installing MMPM GUI Dependencies"


if [[ ! $(command -v nginx) ]]; then
    printf " -- NGINX command not found, installing"

    [[ $NAME == *Arch* ]] && log_and_eval "sudo pacman -Sy nginx-mainline --noconfirm"
    [[ $NAME == *Ubuntu* || $NAME == *Raspbian* ]] && log_and_eval "sudo apt install nginx-full -y"

else
    printf " -- NGINX already installed"
fi

_done_

if [[ ! $(command -v curl) ]]; then
    printf " -- curl command not found, installing"

    [[ $NAME == *Arch* ]] && log_and_eval "sudo pacman -Sy curl --noconfirm"
    [[ $NAME == *Ubuntu* || $NAME == *Raspbian* ]] && log_and_eval "sudo apt install curl -y"
else
    printf " -- curl already installed"
fi

_done_

NODE_VERSION=$(node -v | cut -c 2,3 | tr -d "\n")

if [[ ! $(command -v node) ]]; then
    printf " -- NodeJS command not found, installing"
    log_and_eval "curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -"
    _done_

else
    printf " -- NodeJS already installed"
fi

_done_

if [[ $NODE_VERSION -le 10 ]]; then

    printf " -- NodeJS version is out of date, installing version 12"

    log_and_eval "curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -"

    [[ $NAME == *Arch* ]] && log_and_eval "sudo pacman -Sy nodejs --noconfirm"
    [[ $NAME == *Ubuntu* || $NAME == *Raspbian* ]] && log_and_eval "sudo apt install nodejs -y"
else
    printf " -- found suitable version of NodeJS"
fi
_done_

log_action "MMPM GUI Dependencies Installed"

printf "Installed MMPM GUI Dependencies\n"
