#!/bin/sh
echo off
SETUP_LOG=/opt/egurkha/agent/logs/node-monitor-setup.log
STATUS_LOG=/opt/egurkha/agent/logs/node-monitor-status.log
INSTALL_LOG=/opt/egurkha/agent/logs/node-monitor-install.log

printResult() {
  currentDate=$(date +"%d-%m-%Y %H:%M:%S %:z")
  echo $currentDate - $1 - $2 $3
  echo $currentDate - $1 - $2 $3 >>$INSTALL_LOG
}

printStatus() {
  awk -v STATUS=$1 -F= '
  { val= $0; sub("^[^=]+=", "", val) }
  $1 == "INSTALL_STATUS"   { val = STATUS} 
  { print $1 "=" val}  
' $STATUS_LOG >file.tmp && mv file.tmp $STATUS_LOG
}

{ # try
  cd /opt/egurkha/lib/apm/NodeJS/default
} || { # catch
  printResult "ERROR" "Unable to find egurkha or Node.js profiler code. "
  printStatus 1
  exit
}

{
  rm $INSTALL_LOG
} || {
  echo "$INSTALL_LOG is not found"
}

if [ -f "$STATUS_LOG" ]; then
  echo "$STATUS_LOG exists."
else
  echo "INSTALL_STATUS=0" >$STATUS_LOG
fi

profilePath=$(pwd)
if [ $profilePath != "/opt/egurkha/lib/apm/NodeJS/default" ]; then
  printResult "ERROR" "Unable to find Node.js profiler code."
  printStatus 1
  exit
fi

echo "The eG Node Monitor files has been extracted successfully." >$SETUP_LOG
echo "To monitoring Node.JS application, please follow the steps indicated in documentation." >>$SETUP_LOG

{ # try
  nodeVersion=$(node -v)
} || {
  echo "Node.js not found by default"
}

if [ -z "$nodeVersion" ]; then
  DIR="/usr/bin/nodejs/bin"
  if [ -d "$DIR" ]; then
    export PATH=$PATH:$DIR
  fi

  { # try
    nodeVersion=$(node -v)
  } || {
    echo "Node.js not found in /usr/bin/nodejs/bin"
  }
fi

if [ -z "$nodeVersion" ]; then
  DIR="/usr/bin/node/bin"
  if [ -d "$DIR" ]; then
    export PATH=$PATH:$DIR
  fi

  { # try
    nodeVersion=$(node -v)
  } || {
    echo "Node.js not found in /usr/bin/node/bin"
  }
fi

if [ -z "$nodeVersion" ]; then
  DIR="/usr/local/nodejs/bin"
  if [ -d "$DIR" ]; then
    export PATH=$PATH:$DIR
  fi

  { # try
    nodeVersion=$(node -v)
  } || {
    echo "Node.js not found in /usr/local/nodejs/bin"
  }
fi

if [ -z "$nodeVersion" ]; then
  DIR="/usr/local/node/bin"
  if [ -d "$DIR" ]; then
    export PATH=$PATH:$DIR
  fi

  { # try
    nodeVersion=$(node -v)
  } || {
    echo "Node.js not found in /usr/local/node/bin"
  }
fi

if [ -z "$nodeVersion" ]; then
  printResult "Error" "The Availability of Node.js:- Not able to find globally installed Node.js"
  printStatus 2
  exit
fi
printResult "INFO" "The Availability of Node.js:- Available/ $nodeVersion"

{ # try
  npmVersion=$(npm -v)
  if [ -z "$npmVersion" ]; then
    printResult "Error" "The Availability of NPM    :- Not able to find globally installed NPM"
    printStatus 3
    exit
  fi
  printResult "INFO" "The Availability of NPM    :- Available/ $npmVersion"
} || { # catch
  printResult "Error" "The Availability of NPM    :- Not able to find globally installed NPM"
  printStatus 3
  exit
}

{ # try
  moduleInstalled=$(node ./scripts/node_module_availability_checker.js --no-logs)
} || { # catch
  printResult "WARN" "Some native node modules is not compatible with this system, so going to rebuild the node-modules"
}

echo "Native Addon Availability Status: $moduleInstalled"
if [ $moduleInstalled != "PASS" ]; then
  printResult "WARN" "Some native node modules is not compatible with this system, so going to rebuild the node-modules"
  { # try
    gccVersion=$(gcc --version)
  }
  if [ -z "$gccVersion" ]; then
    printResult "WARN" "The Availability of GCC    :- Not Found"
  else
    printResult "INFO" "The Availability of GCC    :- Available/ $gccVersion"
  fi

  { # try
    pythonVersion=$(python3 --version)
  }
  if [ -z "$pythonVersion" ]; then
    { # try
      pythonVersion=$(python --version)
    }
  fi
  if [ -z "$pythonVersion" ]; then
    printResult "WARN" "The Availability of Python:- Not Found"
  else
    printResult "INFO" "The Availability of Python:- Available/ $pythonVersion"
  fi

  if [ -z "$pythonVersion" ] || [ -z "$gccVersion" ]; then
    printResult "WARN" "Some native addon complier is not available, so can't rebuild the node-modules"
  else
    { # try
      echo "Rebuild Log Starts---------------------->" >>$INSTALL_LOG
      node ./scripts/script_runner.js --cmd="npm rebuild" --log-path=$INSTALL_LOG
      echo "Rebuild Log ends------------------------>" >>$INSTALL_LOG
      printResult "INFO" "npm rebuild completed"
    } || { # catch
      printResult "WARN" "npm rebuild is faild"
    }

    { # try
      moduleInstalled=$(node ./scripts/node_module_availability_checker.js --no-logs)
    } || { # catch
      printResult "WARN" "Some node modules is not available...!"
    }

    echo "Module Installed Status: $moduleInstalled"
    if [ $moduleInstalled != "PASS" ]; then
      printResult "WARN" "Rebuild is failed, so going try to reach NPM repo to install node-modules"

      {
        wget --timeout=1 --tries=1 --quiet --spider https://registry.npmjs.org >nul 2>&1 && siteStaus=up || siteStaus=down
        #rm nul
      } || {
        printResult "WARN" "Error in wget command"
      }

      if [ $siteStaus == "up" ]; then
        printResult "INFO" "NPM repo is reachable, so going to install native modules."
        { # try
          echo "Going to install" >>$INSTALL_LOG
          node ./scripts/script_runner.js --cmd="npm install --production --no-audit --progress=false --prefer-offline" --log-path=$INSTALL_LOG
          printResult "INFO" "npm install completed"
        } || { # catch
          printResult "WARN" "npm install failed"
        }
      else
        printResult "WARN" "NPM repo is not reachable...!"
      fi
    fi
  fi
fi

node ./scripts/node_module_availability_checker --log-path=$INSTALL_LOG --status-path=$STATUS_LOG
exit
