#!/bin/sh

if test $# = 0
then
	echo "We need a log file..."
	exit
fi
file="$1"
if [ ! -f "$file" ]
then
	file="$file"_log
fi
if [ ! -f "$file" ]
then
	echo "We have no log file $file"
	exit 1
fi

trap "rm -f /tmp/file1_$$ /tmp/file2_$$ /tmp/file3_$$; exit" 0 2 15

awk -F# '
/^------/ { next; }
NF == 5	{
		if (address[$5] == 0) {
			best[$5] = 999999
			name[$5] = $1
		}
		if (length ($1) > maxlen)
			maxlen = length($1)
		address[$5] ++
		if ($2 == 1) {
			avgtime[$5] = (avgtime[$5] * up[$5] + $3) \
						 / (1 + up[$5])
			up[$5] ++
			if ($3 < best[$5])
				best[$5] = $3
			if ($3 > worst[$5])
				worst[$5] = $3
		}
		else {
			down[$5] ++
		}
	}
END {
	for (i in address) 
	{
	    if (up[i] != 0)
	    {
		if (best[i] == 999999)
		{
			best[i] = 0
		}
		printf("%s:%d:%d:%f:%f:%f:%f:%s\n",\
		name[i], address[i], down[i], (100 * up[i]) / address[i], \
		best[i], worst[i], avgtime[i], i)
	    }
	    else
	    {
			printf("%s:%d:%d:%f:-:-:-:%s\n",\
				name[i], address[i], down[i], \
 				(100 * up[i]) / address[i], i)
	    }
	}
}
' "$file" | sort -t: +3nr +0 | sed -e "s/  :/ :/" > /tmp/file1_$$

# In order to get round the fact that some sites dont have
# gawk or nawk, and that old awk can't do:
#     while (getline name_list < list_file > 0
# we have the following hack. Yick Yuck.

# Convert blah_log into blah_list
list_file=`echo $file | awk '{
        if (index($0, "_") != 0)
                printf("%slist\n", substr($0, 1, index($0, "_")))
        else
                printf("%slist\n", $0)
        }'`

cat $list_file > /tmp/file2_$$
echo >> /tmp/file2_$$
echo "SPT EOF for list-file" >> /tmp/file2_$$
cat /tmp/file1_$$ >> /tmp/file2_$$
rm /tmp/file1_$$

# Now group them together into their DSA names, note that this will be sorted.
#Best DSA address first, all other addresses collected up, ...
cat /tmp/file2_$$ | sed -e "s/_/ /g" | awk -F: ' BEGIN {Part1 = 1
                             Num_Records = 0
                             num_dsas = 0
                            }
$0=="SPT EOF for list-file"  { Part1 = 0 }
                                               # Read in the blah_list file and
                                               # the managers name, mail and fone
$0!="SPT EOF for list-file" { if ( Part1 == 1 )
                              {
                                num_dsas++
                                parts = split($0, characteristics, "&")
                                if (parts == 4)
                                {
                                  man_dsa[num_dsas] = characteristics[1]
                                  man_name[num_dsas] = characteristics[2]
                                  man_post[num_dsas] = characteristics[3]
                                  man_phone[num_dsas] = characteristics[4]
                                }
                                else
                                {
                                  man_dsa[num_dsas] = characteristics[1]
                                  man_name[num_dsas] = "??????"
                                  man_post[num_dsas] = "??????"
                                  man_phone[num_dsas] = "??????"
                                }
                              }
                              else
                              {
                                Num_Records++              # Read in the procecessed log file
                                line[Num_Records] = $0     # for pretty printing
                              }
                            }
END {
	num_bits = split(line[1], bits, ":") 
	count = 1
	name[count] = bits[1]
	rest[count] = bits[2]
	for (j = 3; j <= num_bits; j++)
	{
		rest[count] = sprintf("%s:%s", rest[count], bits[j])
	}

	for (i = 2; i <= Num_Records; i++)
	{
		event = 0
		num_bits = split(line[i], bits, ":")
		for (j = 1; j <= count; j++)	# Scan through the current stored
		{				# list, if bits matches, then
   		   if (name[j] == bits[1])	# store the rest
		   {
		      event = 1
		      for (k = 2; k <= num_bits; k++)
		      {
		           rest[j] = sprintf("%s:%s", rest[j], bits[k])
	              }
		      break
		   }
		}
		if (event == 0)
		{
		      count++
		      name[count] = bits[1]
		      rest[count] = bits[2]
		      for (k = 3; k <= num_bits; k++)
                      {
	                 rest[count] = sprintf("%s:%s", rest[count], bits[k])
	              }
		}
	}
	
	printf("\\begin{tabular}{|r|r|l|r|r|r|c|} \\hline \\hline\n")

	split_counter = 1
	for (i = 1; i <= count; i++)
	{
	   if (split_counter == 10)
	   {
		print"\\end{tabular}"
		print"\n\\begin{tabular}{|r|r|l|r|r|r|c|} \\hline \\hline"
		split_counter = 0
	   }
	   split_counter++

	   num_bits = split(rest[i], bits, ":")

	   # Strip out c=GB, @o=, @ou=, @cn=, cn=
	   # from the name, and replace with ", "

	   dsa_name = name[i]
	   if (index(dsa_name, "@cn=") != 0)
	   {
	   	dsa_name=substr(dsa_name, 1, index(dsa_name, "@cn=")-1) ", " substr(dsa_name, index(dsa_name, "@cn=")+4)
	   }
	   if (index(dsa_name, "cn=") != 0)
	   {
		dsa_name=substr(dsa_name, index(dsa_name, "cn=")+3)
	   }
	   if (index(dsa_name, "c=") != 0)
	   {
	   	dsa_name=substr(dsa_name, index(dsa_name, "c=")+2)
	   }
	   if (index(dsa_name, "@o=") != 0)
	   {
	   	dsa_name=substr(dsa_name, 1, index(dsa_name, "@o=")-1) ", " substr(dsa_name, index(dsa_name, "@o=")+3)
	   }
	   printf("\\multicolumn{3}{|c|}{%s} & \n", dsa_name) 
	   # Now print out the name, post and fone of the manager.
	   fail = 1
	   for (j = 1; j < num_dsas; j++)
	   {
	   	if (index(name[i], man_dsa[j]))
		{
			printf("\\multicolumn{4}{|c|}{%s} \\\\ \\hline \n", man_name[j])
			printf("\\multicolumn{3}{|c}{%s} &\n", man_post[j])
			printf("\\multicolumn{4}{|c|}{%s} \\\\ \\hline \n", man_phone[j])
			fail = 0
			break
		}
	   }

	   if (fail == 1)
	   {
		printf("\\multicolumn{4}{|c|}{??????} \\\\ \\hline \n" )
	   }
	   printf("Binds & Fails & Success \\%% & Best& Worst & Ave & Network \\\\ \n")

	   for (j = 0; j < num_bits - 1; j = j + 7)
	   {
	      # Convert the address from TELEX numbers to understandable network names
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+01+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " Int-X.25"
	      }
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+02+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " Janet"
	      }
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+03+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " Internet"
	      }
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+04+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " Local Ether"
	      }
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+05+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " Telex 05 LoopBack. DONT USE."
	      }
	      if ((index(bits[int(j/7)*7 + 7], "TELEX") != 0) && (index(bits[int(j/7)*7 + 7], "+06+") != 0))
	      {
		 bits[int(j/7)*7 + 7] = " IXI"
	      }

	      # Discard everything after =...
	      if (index(bits[int(j/7)*7 + 7], "=") != 0)
	      {
		 bits[int(j/7)*7 + 7] = substr(bits[int(j/7)*7 + 7], 1, \
	      					index(bits[int(j/7)*7 + 7], "=")-1)
	      }
	      if (index(bits[int(j/7)*7 + 7], "+") != 0)
	      {
		 bits[int(j/7)*7 + 7] = substr(bits[int(j/7)*7 + 7], 1, \
	      					index(bits[int(j/7)*7 + 7], "+")-1 )
	      }

	      if (bits[int(j/7)*7 + 3] == 0)
	      {
		 printf("%3d & ", bits[int(j/7)*7 + 1]) 
		 printf("%3d & ", bits[int(j/7)*7 + 2])
		 printf("  %6.2f &    -  &    -  &   -  & ", bits[int(j/7)*7 + 3]) 
		 printf("%s \\\\ \n", bits[int(j/7)*7 + 7]) 
	      }
	      else
	      {
	         printf("%3d & ", bits[int(j/7)*7 + 1]) 
		 printf("%3d & ", bits[int(j/7)*7 + 2])
		 printf("  %6.2f & ", bits[int(j/7)*7 + 3])
		 printf("%5.1f & %5.1f & %5.2f & ", bits[int(j/7)*7 + 4], \
		 	bits[int(j/7)*7 + 5], bits[int(j/7)*7 + 6])
		 printf("%s \\\\ \n", bits[int(j/7)*7 + 7]) 
	      }
	   }
	   print "\\hline \\hline"
	}
	print "\\end{tabular}"
}'
