#!/bin/bash

# If we don't already have the archive, download it
if [ ! -f data/bridge-pool-assignments-2021-02.tar.xz ]; then
    curl -Lo data/bridge-pool-assignments-2021-02.tar.xz https://collector.torproject.org/archive/bridge-pool-assignments/bridge-pool-assignments-2021-02.tar.xz
fi

# Check that we have the right archive
sha256sum -c data/bridge-pool-assignments.sha256 || exit 1

# If we haven't already extracted the archive, extract it
if [ ! -d data/bridge-pool-assignments-2021-02 ]; then
    cd data && tar xf bridge-pool-assignments-2021-02.tar.xz && cd ..
fi

# Extract obfs4 email bridges
for i in $(seq 1 21); do
    grep " email " data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* \
        | grep "obfs4" \
        | grep -Po '(?<=:)(.*?)(?= )'
done | sort | uniq > data/obfs4-email-bridges

# Get list of all bridges
for i in $(seq 1 21); do
    grep -v "bridge-pool-assignment" data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* \
        | grep -Po '(?<=:)(.*?)(?= )'
done | sort | uniq > data/all-bridges

# Count bridges in each category
all_bridges=$(cat data/all-bridges | wc -l)
obfs4_email_bridges=$(cat data/obfs4-email-bridges | wc -l)
email_bridges=$(for i in $(seq 1 21); do grep -v "bridge-pool-assignment" data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* | grep "email" | grep -Po '(?<=:)(.*?)(?= )';done | sort | uniq | wc -l)
https_bridges=$(for i in $(seq 1 21); do grep -v "bridge-pool-assignment" data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* | grep "https" | grep -Po '(?<=:)(.*?)(?= )';done | sort | uniq | wc -l)
moat_bridges=$(for i in $(seq 1 21); do grep -v "bridge-pool-assignment" data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* | grep "moat" | grep -Po '(?<=:)(.*?)(?= )';done | sort | uniq | wc -l)
unallocated_bridges=$(for i in $(seq 1 21); do grep -v "bridge-pool-assignment" data/bridge-pool-assignments-2021-02/$(printf %02d $i)/* | grep "unallocated" | grep -Po '(?<=:)(.*?)(?= )';done | sort | uniq | wc -l)

echo "Total number of bridges: ${all_bridges}"
echo "Number of obfs4 email bridges: ${obfs4_email_bridges}"
echo "Number of email bridges: ${email_bridges}"
echo "Number of HTTPS bridges: ${https_bridges}"
echo "Number of moat bridges: ${moat_bridges}"
echo "Number of unallocated bridges: ${unallocated_bridges}"
