#!/bin/sh
echo "This will install curl, nodeJS and yarn"
echo " "

#Check if java is installed
if [ -z "$(command -v java)" ]; then
    echo "Java not found"
    echo "Please install Java JDK >= 8 manually!"
    exit 1
else
    echo "Java found on your system"
fi

#Check if curl is installed (default for non empty string is -n)
if [ "$(command -v curl)" ]; then
  echo "Curl found on your system"
elif [ "$(command -v apt-get)" ]; then
  #Install curl debian/ubuntu
  sudo apt-get update
  sudo apt-get install curl
elif [ "$(command -v yum)" ]; then
  #Install curl RHEL/CentOS/Fedora
  yum install curl
elif [ "$(command -v zypper)" ]; then
  #Install curl OpenSUSE
  zypper install curl
elif [ "$(command -v pacman)" ]; then
  #Install curl ArchLinux
  pacman -Sy curl
else
    echo 'Curl not found'
    echo 'Please install curl manually!'
    exit 1
fi

#Install Node.JS
curl https://nodejs.org/dist/v20.12.2/node-v20.12.2-linux-x64.tar.xz > node.tar.xz
sudo mkdir -p /usr/local/lib/nodejs
sudo tar -xJvf node.tar.xz -C /usr/local/lib/nodejs
rm node.tar.xz

#Export node path
export PATH=/usr/local/lib/nodejs/node-v20.12.2-linux-x64/bin:$PATH
. ~/.profile

# Check if yarn is installed
if [ "$(command -v yarn)" ]; then
  echo 'yarn found on your system'
elif [ "$(command -v npm)" ]; then
  # Install yarn via npm
  sudo npm install --global yarn
else
  echo 'Could not find npm package manager.'
  echo 'Please install yarn manually!'
  exit 1
fi