#!/bin/bash

###########################################################
## Copyright (c) 2002-2026 Alexey Kuryakin daqgroup@mail.ru
###########################################################

###########################################################
## CRW-DAQ script to make DaqZmqSrv request-reply.       ##
###########################################################

###########################################################
source $(crwkit which crwlib_base.sh); # Use base library #
source $(crwkit which crwlib_file.sh); # Use file library #
###########################################################

readonly version="1.0";
readonly copyright="2026 Alexey Kuryakin daqgroup@mail.ru";

if [[ -n $VIRTUAL_ENV ]]; then
 readonly python="python3";
else
 readonly python="pyvenv python3";
fi;

function do_version(){
 echo -n "$scriptname version $version, ";
 echo -n "$($python --version), ";
 echo "$($python $scriptHOME/$scriptbase.py --version)";
};

function do_help(){
 do_version | sed 's/ version//g';
 echo "Copyright (c) $copyright"
 echo "Utility to send request to DaqZmqSrv & print reply.     ";
 echo "$scriptname is binding (wrapper) for $scriptbase.py.    ";
 echo "                                                        ";
 echo "Usage: $scriptname [-options] [endpoint] [@command]     ";
 echo "       default endpoint is tcp://localhost:4201         ";
 echo "                                                        ";
 echo "Options:                                                ";
 echo " --version    print version(s)                          ";
 echo " -h,-help     print help on $scriptname                 ";
 echo " --help       print help on $scriptbase.py              ";
 echo " --verb       set verbose mode                          ";
 echo " --timeout t  set timeout (t) ms                        ";
 echo " --timing     calculate response time                   ";
 echo "                                                        ";
 echo "Parameters:                                             ";
 echo "endpoint  optional network address as tcp://HOST:PORT   ";
 echo "          default endpoint is tcp://localhost:4201      ";
 echo "          HOST is hostname where crwdaq/DaqZmqSrv run   ";
 echo "          PORT number is (4200 + SessionNb), where      ";
 echo "          SessionNb is crwdaq session number            ";
 echo "@command  optional command (backslash-encoded) to send  ";
 echo "          if command is not set, uses stdin pipe stream ";
 echo "Examples:                                               ";
 echo "unix $scriptname --timing  \"@Ping\"                    ";
 echo "unix $scriptname \"@GetTags\\n/.*/\"                    ";
 echo "unix $scriptname                                        ";
 echo "echo -e \"@Ping\" | unix $scriptname --timing           ";
 echo "echo -e \"@GetTags\\n/.*/\" | unix $scriptname          ";
 echo "echo \"@Help\" | unix $scriptname --verb --timing --timeout 5000 tcp://localhost:4201";
};


################
# main function.
################

function main(){
 local -a cmds=();
 local -a args=();
 for arg in "$@"; do
  case $arg in
   --version)   do_version; return 0; ;;
   -h|-help)    do_help; return 0; ;;
   --help)      $python $scriptHOME/$scriptbase.py --help; return 0; ;;
   @*)          cmds+=("$arg"); ;;
   *)           args+=("$arg"); ;;
  esac;
 done;
 if [[ -n $cmds ]]; then
  echo -e "$cmds" | $python $scriptHOME/$scriptbase.py "${args[@]}";
 else
                    $python $scriptHOME/$scriptbase.py "${args[@]}";
 fi;
};

main "$@";

#############
# END OF FILE
#############
