#!/bin/sh
set -x

[ "x$1" == "x" ] && { echo "usage: $0 <tapistrace-now.log>"; exit 1; }

# Strace output format:
# pid   timestamp         traced call
# 74    12:10:55.994657   write(...

TAPI_LOG_FILE=$1

PID_TAPISRV=$(tail -1 $TAPI_LOG_FILE | cut -d ' ' -f 1)


# Show only AT commands _from_ tapisrv
cat $TAPI_LOG_FILE | \
grep '\"\\x41\\x54' | \
sed -e \
"s/\($PID_TAPISRV\)\s*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}.[0-9]\{6\}\)\s*\(.*\)/\2  tapisrv  \3/g" \
 -e "s/\(tapisrv\).*\"\([^\"]*\)\".*/\1 \\\\\"\2\\\\\"\\\\\n/g" -e "s/\x/\\\\x/g" \
  | xargs echo -e | sed -e 's/^\s//g' > atcmds_from_tapisrv.txt


# Show only writes and read to and from mux devices fd in {4,5,6,7,8,9,10,11}
#  4 /dev/mux0
#  5 /dev/mux1
#  6 /dev/mux2
#  7 /dev/mux3
#  8 /dev/mux4
#  9 /dev/mux5
# 10 /dev/mux6
# 11 /dev/mux7

cat $TAPI_LOG_FILE | \
egrep '(read|write)\((4|5|6|7|8|9|10|11),' | \
sed -e \
"s/\($PID_TAPISRV\)\s*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}.[0-9]\{6\}\)\s*\(.*\)/\2  tapisrv  \3/g" \
 -e "s/\(tapisrv\)\s*\([a-z]*\)(\([0-9]*\),.*\"\([^\"]*\)\".*/\1 \3 \2 \\\\\"\4\\\\\"\\\\\n/g" -e "s/\x/\\\\x/g" \
 | xargs echo -e | sed -e 's/^\s//g' -e "/^[^0-9]/s/^/                                    /g" \
 -e 's/read/ read/' \
 -e 's/tapisrv 4/tapisrv mux0/' \
 -e 's/tapisrv 5/tapisrv mux1/' \
 -e 's/tapisrv 6/tapisrv mux2/' \
 -e 's/tapisrv 7/tapisrv mux3/' \
 -e 's/tapisrv 8/tapisrv mux4/' \
 -e 's/tapisrv 9/tapisrv mux5/' \
 -e 's/tapisrv 10/tapisrv mux6/' \
 -e 's/tapisrv 11/tapisrv mux7/' \
 | dos2unix > mux_atcmds_tapisrv.txt

