#!/bin/bash

# Check that dependencies are installed before going further
if [ -z "$(command -v curl)" ]; then
    echo "This script needs curl to be installed."
    exit 1
fi
if [ -z "$(command -v python3)" ]; then
    echo "This script needs python3 to be installed."
    exit 1
fi
./scripts/check-python-deps.py
if [ "$?" != 0 ]; then
    echo "This script needs numpy to be installed."
    exit 1
fi

fast=false
sequential=false

while [ -n "$1" ]; do
    if [ "$1" == "--fast" ]; then
        fast=true
    elif [ "$1" == "-s" ]; then
        sequential=true
    fi
    shift
done

# Get bridge data
if [ "$fast" == "true" ]; then
    echo "Repacking some pre-processed data for the next step..."
    ./scripts/repack-preprocessed-data.sh
else
    echo "Downloading data from the Tor Project..."
    ./scripts/download-bridge-data.sh

    if [ "$sequential" != "true" ]; then
        echo "Processing data from step 1 in 10 parallel steps..."
        echo "This will take a while (around an hour on my device)"
        echo "and require around 20 GB of free space while running."
        ./scripts/get-bridge-data.sh
    else
        echo "Processing data from step 1 sequentially..."
        echo "This will take quite a long time (around 12.5 hours on my device)"
        echo "and require a few GB of free space while running."
        ./scripts/get-bridge-data.sh -s
    fi
fi

# Get list of email-distributed bridges
./scripts/get-email-bridges.sh

# Clean up bridge data for the format we want
./scripts/clean-bridge-data.sh

# Evaluate blockages and get stats
./scripts/get-stats.sh
