Fix interpreter reference in run script
[ric-app/mc.git] / sidecars / listener / run_replay.sh
1 #!/usr/bin/env ksh
2 # Do NOT use bash; it cannot handle constructing variables in while loops.
3
4
5 # vim: ts=4 sw=4 noet:
6 #----------------------------------------------------------------------------------
7 #
8 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
9 #
10 #   Licensed under the Apache License, Version 2.0 (the "License");
11 #   you may not use this file except in compliance with the License.
12 #   You may obtain a copy of the License at
13 #
14 #          http://www.apache.org/licenses/LICENSE-2.0
15 #
16 #   Unless required by applicable law or agreed to in writing, software
17 #   distributed under the License is distributed on an "AS IS" BASIS,
18 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 #   See the License for the specific language governing permissions and
20 #   limitations under the License.
21 #
22 #---------------------------------------------------------------------------------
23
24
25 # ----------------------------------------------------------------------
26 # Mnemonic:     run_replay.sh
27 # Abstract: Simple script to make starting the replay binary easier.
28 #                       Defaults:
29 #                               /var/lib/mc/listener  -- directory for fifos
30 #
31 #                       The input file can be supplied with -f and if omitted then
32 #                       standard intput is assumed. This should allow the following
33 #                       when run in a docker container:
34 #                               docker run --rm -i -v /tmp/replay_fifos:/var/lib/mc/listener run_replay.sh <data-file
35 #
36 # Date:         20 November 2019
37 # Author:       E. Scott Daniels
38 # ----------------------------------------------------------------------
39
40
41 fifo_dir=/var/lib/mc/listener
42 data=""                                                         # stdin by default
43 pre_open=0
44 mtype_list=""
45 gate=""
46 delay=0
47
48 while [[ $1 == -* ]]
49 do
50         case $1 in 
51                 -f) data="$2"; shift;;
52                 -d)     fifo_dir=$2; shift;;
53                 -D) delay=$2; shift;;
54                 -g) gate="$2"; shift;;
55                 -m)     mtype_list="$2"; shift;;
56                 -p) pre_open=1;;
57
58                 *)      echo "$1 is not a recognised option"
59                         echo "usage: $0 [-d fifo-dir] [-D seconds] [-f data-file] [-g gate-file] [-m mtype-list] [-p]"
60                         echo "   -p causes FIFOs to be pre-allocated"
61                         echo "   -m supplies a comma separated list of message types to preopen FIFOs for"
62                         echo "      if -p is given and -m is omitted, the input file is examined to determine message types"
63                         echo "   -D seconds will cause a delay of the specified number of seconds before starting the replay"
64                         echo "   -g file  will cause the script to wait for file to appear before starting the replay"
65                         echo "      if both -D and -g are used, the delay happens after the gate file is found"
66                         exit 1
67                         ;;
68         esac
69
70         shift
71 done
72
73 if (( pre_open )) 
74 then
75         if [[ -z $mtype_list ]]
76         then
77                 if [[ -z $data ]]
78                 then
79                         echo "error: cannot determine a mtype list from stdin:"
80                         echo "  -p given with out a list (-m) and input file set to default to stdin (missing -f)"
81                         exit 1
82                 fi
83
84                 rdc_extract $data 0 | sort -u | while read t
85                 do
86                         mtype_list="$mtype_list$t "
87                 done
88         fi
89
90         (
91                 cd $fifo_dir
92                 count=0
93                 for t in ${mtype_list//,/ }
94                 do
95                         name=$( printf "MT_%09d" t )
96                         echo "making FIFO: $name"
97                         mkfifo -m 664 $name 2>/dev/null         # if these are there, don't natter on about them
98                         (( count++ ))
99                 done
100
101                 ls MT_* | wc -l | read found
102                 if (( count != found ))
103                 then
104                         echo "warn:  after pre-create, expected $count FIFOs, but found only $found"
105                 fi
106         )
107 fi
108
109 if [[ -n $data ]]
110 then
111         if [[ ! -r $data ]]
112         then
113                 echo "abort: cannot find data file: $data"
114                 exit 1
115         fi
116
117         data="-f $data"
118 fi
119
120 if [[ -n $gate ]]
121 then
122         echo "waiting for gate file to appear: $gate"
123         while true
124         do
125                 if [[ -e $gate ]]
126                 then
127                         break
128                 fi
129                 sleep 1
130         done
131 fi
132
133 if (( delay ))
134 then
135         echo "pausing $delay seconds before starting rdc_display..."
136         sleep $delay
137 fi
138
139 echo "starting rdc_replay"
140 rdc_replay -d $fifo_dir $data
141