#!/bin/bash

set -e 		# Exit immediately if a command exits with a nonzero exit status
set -u 		# Treat unset variables as an error
#set -x 	# Print commands and their arguments as they are executed (debugging)

bloxbatch=$LOGICBLOX_HOME/bin/bloxbatch

database=$1
analysis=`cat $database/.doop-meta/analysis`

function size()
{
	$bloxbatch -db $database -popCount $1 | awk '{ print $2 }'
}

function print2()
{
	printf "%-40s %'d\n" "$1" "$2"
}

function print3()
{
	printf "%-40s %'d (insens) / %'d (sens)\n" "$1" "$2" "$3"
}

function printcolor()
{
	printf "$3%-25s${C_RESET}$4 %'d%s${C_RESET}\n" "$1" "$2"
}

#print3 "var points-to" $(size Stats:Simple:InsensVarPointsTo) $(size VarPointsTo)
#print3 "array index points-to" $(size Stats:Simple:InsensArrayIndexPointsTo) $(size ArrayIndexPointsTo)
#print3 "call graph edges" $(size Stats:Simple:InsensCallGraphEdge) $(size CallGraphEdge)
# print3 "native call graph edges" $(size Stats:Simple:InsensNativeCallGraphEdge) $(size NativeCallGraphEdge)
#print3 "instance field points-to" $(size Stats:Simple:InsensInstanceFieldPointsTo) $(size InstanceFieldPointsTo)
#print3 "reachable methods" $(size Reachable) $(size ReachableContext)
#print3 "reachable variables" $(size Stats:Simple:InsensReachableVar) $(size Stats:Simple:ReachableVar)
#print3 "static field points-to" $(size Stats:Simple:InsensStaticFieldPointsTo) $(size StaticFieldPointsTo)

#print2 "virtual call sites (statically)" $(size VirtualMethodInvocation:In)
#print2 "reachable virtual call sites" $(size Stats:Simple:ReachableVirtualMethodInvocation)
#print2 "polymorphic virtual call sites" $(size Stats:Simple:PolymorphicCallSite)
#print2 "null virtual call sites" $(size Stats:Simple:NullVirtualMethodInvocation)
#print2 "app virtual call sites (statically)" $(size Stats:Simple:Application:VirtualMethodInvocation)
#print2 "app reachable virtual call sites" $(size Stats:Simple:Application:ReachableVirtualMethodInvocation)
#print2 "app polymorphic virtual call sites" $(size Stats:Simple:Application:PolymorphicCallSite)
#print2 "app null virtual call sites" $(size Stats:Simple:Application:NullVirtualMethodInvocation)
#print2 "casts (statically)" $(size AssignCast)
#print2 "reachable casts" $(size Stats:Simple:ReachableCast)
#print2 "reachable casts that may fail" $(size Stats:Simple:PotentiallyFailingCast)
#print2 "app casts (statically)" $(size Stats:Simple:Application:Cast)
#print2 "app reachable casts" $(size Stats:Simple:Application:ReachableCast)
#print2 "app reachable casts that may fail" $(size Stats:Simple:Application:PotentiallyFailingCast)

# print2 "array type" $(size ArrayType)
# print2 "direct superclass" $(size DirectSuperclass)
# print2 "direct superinterface" $(size DirectSuperinterface)
# print2 "heap allocations" $(size HeapAllocationRef)
# print2 "method implemented" $(size MethodImplemented)
# print2 "method lookup" $(size MethodLookup)
# print2 "superclass" $(size Superclass)
# print2 "superinterface" $(size Superinterface)
# print2 "variables" $(size VarRef)

printcolor "#may-fail casts" $(size Stats:Simple:PotentiallyFailingCast) "${C_YELLOW}" "${C_GREEN}"
printcolor "#poly calls" $(size Stats:Simple:PolymorphicCallSite) "${C_YELLOW}" "${C_GREEN}"
printcolor "#reach methods" $(size Reachable) "${C_YELLOW}" "${C_GREEN}"
printcolor "#call edges" $(size Stats:Simple:InsensCallGraphEdge) "${C_YELLOW}" "${C_GREEN}"
