#!/bin/bash
#
# The aredn node url can look something like http://192.168.1.155, or
# something linke http://radiationMonitor.domain.com depending on
# whether your local network uses a domain name server.
#

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

# Changing the value overides the default data request interval hardcoded
# in the agent script.  '60' is the default. Value is in seconds.
POLLING_INTERVAL="60"


if [ "$HOSTNAME" == "raspi3" ]; then
  AGENT_NAME="[a]rednsigMirrorAgent.py"
  SOURCE_URL="http://192.168.1.30:7368/arednsig/"
elif [ "$HOSTNAME" == "redwall" ]; then
  AGENT_NAME="[a]rednsigMirrorAgent.py"
  SOURCE_URL="http://73.157.139.23:7361/arednsig/"
elif [ "$HOSTNAME" == "ka7jlo-web" ]; then
  AGENT_NAME="[a]rednsigAgent.py"
  SOURCE_URL="http://ka7jlo-sw-albany-nano-m3.local.mesh/cgi-bin/status"
else
  echo invalid host
  exit 1
fi

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

if [ -n "$PROCESS_ID" ]; then
  if [ "$1" != "-q" ]; then
    printf "arednsig agent running [%s]\n" $PROCESS_ID
  fi
else
  printf "starting up arednsig agent\n"
  cd $APP_PATH
  if [ "$1" != "" ]; then
    ./$AGENT_NAME $1 -u $SOURCE_URL -p $POLLING_INTERVAL
  else
    ./$AGENT_NAME -u $SOURCE_URL -p $POLLING_INTERVAL >> \
      $LOG_PATH/arednsigAgent.log 2>&1 &
  fi
fi