#!/bin/bash

split_long_lines() {
    awk '{
	sep=index(substr($0, 70), "0000100:100000:")
	if (sep == 0) {
		printf "%s\n", $0;
	} else {
                printf "%s\n%s\n", substr($0, 1, 70+sep-2), substr($0, 70+sep-1)
        }
    }'
}

process() {
    tlo=$1
    thi=$2
    awk  -F: -v tlo="$tlo" -v thi="$thi" '{
	t=$4; 
	if (t < tlo || t > thi)
	    next;

	file=substr($8, 2); line=$9; where=$10;

	sep=index(where, ") ");
	if (sep == 0)
	    next;

	fn=substr(where, 1, sep - 1); 
	msg=substr(where, sep + 2);

	if (fn == "ptlrpc_server_handle_request()") {
	    xid=$18
	    client=$19
	    op=$20

	    if (msg == "Handling RPC pname") {
		printf "S %s %s %s %s\n", t, client, xid, op;
		next;
	    }

	    
	    if (msg == "Handled RPC pname") {
                s1 = index(op, " Request");
                opcode = substr(op, 0, s1);
		printf "E %s %s %s %s\n", t, client, xid, opcode;
	        s1 = index(op, "us (");
	        if (s1 == 0)
		    next;
	        s2 = index(op, "us total) trans");
	        if (s2 == 0)
		    next;
                st = substr(op, s1+4, s2 - (s1+4));
                printf "I %s %s %s %s %s\n", t, client, xid, opcode, st;
		next;
	    }
	    next;
	}
    }'
}

tlo="0"
thi="2000000000"

while getopts s:e: c; do
   case $c in
   s) tlo=$OPTARG;;
   e) thi=$OPTARG;;
   *) ;;
   esac
done
shift `expr $OPTIND - 1`
split_long_lines | process $tlo $thi
