#!/bin/sh

DIRECTORY=$(dirname $0)

if [ -f $DIRECTORY/cronosagent.conf ]
then
  # Read configuration from text file
  # Dot is equivalent of bash's source
  . $DIRECTORY/cronosagent.conf
fi

if [ -f /etc/config/cronosagent ]
then
  # Read configuration from UCI/LuCI on OpenWRT
  REPEATER_NUMBER=$(uci get cronosagent.@cronosagent[0].number)
  REPEATER_ADDRESS=$(uci get cronosagent.@cronosagent[0].repeaterAddress)
  SERVER_ADDRESS=$(uci get cronosagent.@cronosagent[0].serverAddress)
  SERVER_PASSWORD=$(uci get cronosagent.@cronosagent[0].serverPassword)
fi

if [ -n "$1" ]
then
  # 1 - print to standard output
  # 2 - print to system log
  # 6 - run as daemon and print to system log
  SERVICE_MODE=$1
fi

$DIRECTORY/cronosagent \
  --trap-port 8162 \
  --repeater-number ${REPEATER_NUMBER} \
  --repeater-address ${REPEATER_ADDRESS} \
  --server-password ${SERVER_PASSWORD} \
  --server-address ${SERVER_ADDRESS} \
  --service-mode ${SERVICE_MODE}