#!/bin/sh

# If the blockages have not already been evaluted, do that now
if [ ! -f data/blocked_0 ]; then
    echo "Running python code to evaluate blockages..."
    ./scripts/evaluate-blockages.py
fi

# Do the actual math in python
if [ ! -f output ]; then
    echo "Running python code to compute stats..."
    ./scripts/get-stats.py > output
fi

echo "Tables:"
head -33 output

echo -n "Number of bridges that received more than 8 connections: "
grep '^Single: ' output | grep 'max=' | grep -v 'max=8' | wc -l

echo -n "Number of bridges that received more than 16 connections: "
grep '^Single: ' output | grep 'max=' | grep -v 'max=8' | grep -v 'max=16' | wc -l

echo -n "Number of bridges that received more than 24 connections: "
grep '^Single: ' output | grep 'max=' | grep -v 'max=8' | grep -v 'max=16' | grep -v 'max=24' | wc -l

echo ""

echo -n "Number of bridges with connection count mean more than 1 stddev away from 0: "
grep "^Single: Zero is " output | grep -v "^Single: Zero is 0." | wc -l

echo -n "Max number of stddevs from 0: "
grep "^Single: Zero is " output | grep -v "^Single: Zero is 0." | grep -Po '(?<=^Single: Zero is )(.*?)(?= standard deviations away from the mean)' | sort -r | head -1

echo ""

echo -n "Number of pairs of bridges that received more than 8 connections: "
grep '^Double: ' output | grep 'max=' | grep -v 'max=8' | wc -l

echo -n "Number of pairs of bridges that received more than 16 connections: "
grep '^Double: ' output | grep 'max=' | grep -v 'max=8' | grep -v 'max=16' | wc -l

echo -n "Number of pairs of bridges that received more than 24 connections: "
grep '^Double: ' output | grep 'max=' | grep -v 'max=8' | grep -v 'max=16' | grep -v 'max=24' | wc -l

echo -n "Number of pairs of bridges that received more than 32 connections: "
grep '^Double: ' output | grep 'max=' | grep -v 'max=8' | grep -v 'max=16' | grep -v 'max=24' | grep -v 'max=32' | wc -l

echo ""

echo -n "Number of pairs of bridges with connection count mean more than 1 stddev away from 0: "
grep "^Double: Zero is " output | grep -v "^Double: Zero is 0." | wc -l

echo -n "Max number of stddevs from 0: "
grep "^Double: Zero is " output | grep -v "^Double: Zero is 0." | grep -Po '(?<=^Double: Zero is )(.*?)(?= standard deviations away from the mean)' | sort -r | head -1
