#!/bin/bash
# Starts up the node power agent as a background process
# and redirects output to a log file.

APP_PATH="/home/$USER/bin"
LOG_PATH="/home/$USER/log"

AGENT_NAME="[n]pwAgent.py"

PROCESS_ID="$(ps x | awk -v a=$AGENT_NAME '$7 ~ a {print $1}')"

if [ -n "$PROCESS_ID" ]; then
  if [ "$1" != "-q" ]; then
    printf "node power agent running [%s]\n" $PROCESS_ID
  fi
else
  printf "starting up node agent\n"
  cd $APP_PATH
  if [ "$1" != "" ]; then
    ./$AGENT_NAME $1
  else
    ./$AGENT_NAME >> $LOG_PATH/npwAgent.log 2>&1 &
  fi
fi