#!/bin/bash
$SIMPATH/bin/fairmq-shmmonitor --cleanup

if [ $# -ge 1 ]; then
  _nbmoni=$1

  if [ $# -ge 4 ]; then
    _filename=""
    _dirname=""
    _hostname=$4
  elif [ $# -ge 2 ]; then
    _filename=$2
    _hostname=""
    if [ $# -eq 3 ]; then
      _dirname=$3
    else
      _dirname=""
    fi
  else
    echo 'Starting connection to local stream'
    echo ' for other usages, please supply at least a filename.'
    echo 'Possible usages are:'
    echo 'startMQSamplerPsdMonitor2021.sh'
    echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes>'
    echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> <full filename pattern list>'
    echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> <filename pattern> <folder_path>'
    echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> "" "" <hostname(s) list>'
    _filename=""
    _dirname=""
    _hostname="localhost"
  fi
else
  echo 'Starting connection to local stream with 1 monitor process'
  echo ' for other usages, please supply at least a filename.'
  echo 'Possible usages are:'
  echo 'startMQSamplerPsdMonitor2021.sh'
  echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes>'
  echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> <full filename pattern list>'
  echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> <filename pattern> <folder_path>'
  echo 'startMQSamplerPsdMonitor2021.sh <Nb Monitor processes> "" "" <hostname(s) list>'
  _filename=""
  _dirname=""
  _hostname="localhost"
  _nbmoni=1
fi

_paramfile=/mnt/scratch/users/ploizeau/mcbm2020/cbmroot_pal/macro/beamtime/mcbm2020/mPsdPar.par
##_paramfile=$VMCWORKDIR/macro/beamtime/mcbm2021/mPsdPar.par

SAMPLER="MultiTsaSampler"
SAMPLER+=" --id sampler1"
SAMPLER+=" --max-timeslices 0"
SAMPLER+=" --severity info"
#SAMPLER+=" --flib-port 10"
if [ "$_hostname" != "" ]; then
  SAMPLER+=" --flib-host $_hostname"
elif [ "$_filename" != "" ]; then
  SAMPLER+=" --filename $_filename"
  if [ "$_dirname" != "" ]; then
    SAMPLER+=" --dirname $_dirname"
  fi
fi
SAMPLER+=" --high-water-mark 1000"
SAMPLER+=" --send-ts-per-sysid 1"
SAMPLER+=" --channel-config name=psdcomponent,type=push,method=bind,address=tcp://127.0.0.1:11555"
SAMPLER+=" --transport shmem"
#SAMPLER+=" --transport zeromq"
#SAMPLER+=" --transport nanomsg"
xterm -l -geometry 80x23+0+0 -hold -e @CMAKE_BINARY_DIR@/bin/MQ/source/$SAMPLER &

_iMoni=0
((_oubfreqts = $_nbmoni*100 ))
while (( _iMoni < _nbmoni )); do
  (( _yOffset=100*_iMoni ))
  (( _iMoni += 1 ))
  MONITOR="PsdMonitorMcbm2018"

  MONITOR+=" --id mon$_iMoni"
  MONITOR+=" --severity info"
  MONITOR+=" --HistEvoSz 600"
  MONITOR+=" --HistChrgArgs 500 0 5000"
  MONITOR+=" --HistAmplArgs 100 0 500"
  MONITOR+=" --HistZlArgs   100 0 5000"
  MONITOR+=" --PubFreqTs $_oubfreqts"
  MONITOR+=" --channel-config name=psdcomponent,type=pull,method=connect,address=tcp://127.0.0.1:11555"
  MONITOR+=" --transport shmem"
  #MONITOR+=" --transport zeromq"
  #MONITOR+=" --transport nanomsg"
  MONITOR+=" --channel-config name=parameters,type=req,method=connect,transport=zeromq,address=tcp://127.0.0.1:11005,rateLogging=0"
  MONITOR+=" --channel-config name=histogram-in,type=pub,method=connect,transport=zeromq,address=tcp://127.0.0.1:11666"
  MONITOR+=" --channel-config name=histo-conf,type=pub,method=connect,transport=zeromq,address=tcp://127.0.0.1:11667"
  MONITOR+=" --channel-config name=canvas-conf,type=pub,method=connect,transport=zeromq,address=tcp://127.0.0.1:11668"
  xterm -l -geometry 80x23+500+$_yOffset -hold -e @CMAKE_BINARY_DIR@/bin/MQ/monitor/$MONITOR &
done

PARAMETERSERVER="parmq-server"
PARAMETERSERVER+=" --id parmq-server"
PARAMETERSERVER+=" --severity info"
PARAMETERSERVER+=" --channel-name parameters"
PARAMETERSERVER+=" --channel-config name=parameters,type=rep,method=bind,transport=zeromq,address=tcp://127.0.0.1:11005,rateLogging=0"
PARAMETERSERVER+=" --first-input-name $_paramfile"
PARAMETERSERVER+=" --first-input-type ASCII"
PARAMETERSERVER+=" --libs-to-load=libCbmFlibMcbm2018" # doesn't work due to runtime problem
xterm -geometry 80x23+1000+0 -hold -e @CMAKE_BINARY_DIR@/bin/MQ/parmq/$PARAMETERSERVER &

HISTSERVER="MqHistoServer"
HISTSERVER+=" --id server1"
HISTSERVER+=" --channel-config name=histogram-in,type=sub,method=bind,transport=zeromq,address=tcp://127.0.0.1:11666"
HISTSERVER+=" --channel-config name=histo-conf,type=sub,method=bind,transport=zeromq,address=tcp://127.0.0.1:11667"
HISTSERVER+=" --channel-config name=canvas-conf,type=sub,method=bind,transport=zeromq,address=tcp://127.0.0.1:11668"
xterm -geometry 800x230+1500+0 -hold -e @CMAKE_BINARY_DIR@/bin/MQ/histogramServer/$HISTSERVER &
