#!/bin/bash

set -e -u -o pipefail

# TODO where should these files be?
CYCLE_DB=/staging/team-probing.update-prep/cycle.db
TARGET_DIR=/staging/team-probing.update-prep/targets
DONOTPROBE=/etc/ark/badips.prefixes.aggr.asfinder
PREFIX_DIR=/data/routing/routeviews-prefix2as
PREFIX_LOG=$PREFIX_DIR/pfx2as-creation.log
CHUNK_SIZE=1500

mkdir -p $TARGET_DIR

if [ ! -e $CYCLE_DB ]; then
    echo "Creating new cycle database $CYCLE_DB"
    sqlite3 $CYCLE_DB <<- EOF
		CREATE TABLE Cycles (
		cycle_id     INTEGER NOT NULL PRIMARY KEY,
		start_date   TEXT NOT NULL  -- YYYYMMDD in UTC
		);
	EOF
fi

while true; do
    echo "Waiting for existing target files to be consumed"

    # wait for some of the existing target files to be consumed. There are
    # around 8000 files and it currently takes about an hour to consume 1600
    # files with ~260 ark monitors. As monitors get added then it will take
    # less time.

    # generating takes about 25 minutes so need to do it ahead of time
    while [ "$(find $TARGET_DIR -type f | wc -l)" -gt 3000 ]; do
        sleep 600
    done

    # get most recent prefixes file
    prefix_file=$PREFIX_DIR/$(tail -n 1 $PREFIX_LOG | cut -f 3)

    # get next cycle number
    next_cycle=$(sqlite3 $CYCLE_DB "SELECT MAX(cycle_id) + 1 FROM Cycles")
    if [ -z "$next_cycle" ]; then
        next_cycle=0
    fi

    # generate the target files for the new cycle
    echo "Generating targets for cycle $next_cycle"
    echo "$DONOTPROBE" "$prefix_file" "$CHUNK_SIZE" "$TARGET_DIR" "$next_cycle"
    /usr/bin/ark-team-probing-targets-generator \
        "$DONOTPROBE" \
        "$prefix_file" \
        "$CHUNK_SIZE" \
        "$TARGET_DIR" \
        "$next_cycle"

    # update the current cycle number
    sqlite3 $CYCLE_DB <<- EOF
		INSERT INTO Cycles VALUES ($next_cycle, $(date -u +%Y%m%d))
	EOF
done
