#!/bin/bash

PreInstallOnLinux()
{
  #Extract information on system
  . /etc/os-release
  if echo $ID-$VERSION_ID | grep -iq "debian"; then
	  DISTRO=debian
      apt-get update &>/dev/null
      apt-get install unzip -y &>/dev/null
      apt-get install wget -y &>/dev/null
  fi

  if echo $ID-$VERSION_ID | grep -iq "ubuntu"; then
      DISTRO=ubuntu
	  apt-get update &>/dev/null
      apt-get install unzip -y &>/dev/null
      apt-get install wget -y &>/dev/null
  fi

  if echo $ID-$VERSION_ID | grep -iq "centos"; then
      DISTRO=centos
	  yum update &>/dev/null
      yum install unzip -y &>/dev/null
      yum install wget -y  &>/dev/null
  fi
}

InstallPackage()
{
    # Download linux-diskcheck.zip to /tmp
    wget -P /tmp https://gitlab.cloudclusters.net/applications/diskmonitor-deploy/-/raw/main/linux-diskcheck.zip &>/dev/null
    unzip -d /tmp /tmp/linux-diskcheck.zip &>/dev/null
    cd /tmp/linux-diskcheck; /bin/bash /tmp/linux-diskcheck/linux-setup.sh &>/dev/null
    cd /root &>/dev/null
    rm /tmp/linux-diskcheck /tmp/linux-diskcheck.zip -rf &>/dev/null
}

PreInstallOnLinux
InstallPackage

# Check if the installation was successful
if systemctl list-units --type=service --all | grep -q diskinfocheck.service && [ -x /bin/diskinfocheck ]; then
    echo "Disk monitoring service is now active and ready."
    exit 0
else
    echo "Disk monitoring service installation failed."
    exit 1
fi
