#!/bin/sh
# Copyright 2023 Job Snijders <job@fastly.com>
#
# This script launches two instances of rpki-client primed with
# the same TALs and the same time for X.509 evaluation at the
# same time.
# The objective is to track how RRDP+RSYNC versus # RSYNC-only
# perspectives change over time. In an ideal situation both
# synchronisation mechanisms provide the exact same output.

LOC="syd"

set -e
UTS="$(date +%s)"

# This instance tries RRDP first, iff RRDP fails will try RSYNC
# This is the best current practise.
rpki-client -P "${UTS}"    -vjcm \
	-H rpki.afrinic.net -H rpki.apnic.net -H rpki.arin.net -H repository.lacnic.net -H rpki.ripe.net \
	-H rpki.sub.apnic.net -H rpki-rps.arin.net -H rsync.paas.rpki.ripe.net \
	-e /usr/local/bin/rsync \
	-d /var/cache/rrdprsync /var/db/rrdprsync 2>&1 \
	| ts | tee /root/rrdprsync.log  &

# This instance only synchronizes using RSYNC
# This is the bare minimum IETF standards mandate.
rpki-client -P "${UTS}" -R -vjcm \
	-H rpki.afrinic.net -H rpki.apnic.net -H rpki.arin.net -H repository.lacnic.net -H rpki.ripe.net \
	-H rpki.sub.apnic.net -H rpki-rps.arin.net -H rsync.paas.rpki.ripe.net \
	-e /usr/local/bin/rsync \
	-d /var/cache/rsynconly /var/db/rsynconly 2>&1 \
	| ts | tee /root/rsynconly.log &

# The below instance is *not* compliant with IETF standards,
# but interesting for research.
rpki-client -P "${UTS}"    -vjcm \
	-H rpki.afrinic.net -H rpki.apnic.net -H rpki.arin.net -H repository.lacnic.net -H rpki.ripe.net \
	-H rpki.sub.apnic.net -H rpki-rps.arin.net -H rsync.paas.rpki.ripe.net \
	-e /usr/bin/true \
	-d /var/cache/rrdponly /var/db/rrdponly 2>&1 \
	| ts | tee /root/rrdponly.log &

wait

HTDOCS="/var/www/htdocs"
TIMESTAMP="$(date '+%Y%m%dT%H%M%SZ')"
TODAY="$(date '+%Y/%m/%d')"

mkdir -p "${HTDOCS}/${TODAY}"
cd "${HTDOCS}/${TODAY}"

copyout() {
	cp /root/$1.log "${TIMESTAMP}-$1.${LOC}.log.txt"
	cp /var/db/$1/csv "${TIMESTAMP}-$1.${LOC}.csv"
	cp /var/db/$1/json "${TIMESTAMP}-$1.${LOC}.json"
	cp /var/db/$1/metrics "${TIMESTAMP}-$1.${LOC}.metrics"
	gzip "${TIMESTAMP}-$1.${LOC}.csv" &
	gzip "${TIMESTAMP}-$1.${LOC}.json" &
	gzip "${TIMESTAMP}-$1.${LOC}.metrics" &
}

copyout "rrdprsync"
copyout "rrdponly"
copyout "rsynconly"

# Make the threeway comparisons using comm(1)
RRDPRSYNC_SRT=$(mktemp)
RRDPONLY_SRT=$(mktemp)
RSYNCONLY_SRT=$(mktemp)
sed 1d /var/db/rrdprsync/csv | sed 's/,[0-9]*$//' | sort > ${RRDPRSYNC_SRT}
sed 1d /var/db/rrdponly/csv  | sed 's/,[0-9]*$//' | sort > ${RRDPONLY_SRT}
sed 1d /var/db/rsynconly/csv | sed 's/,[0-9]*$//' | sort > ${RSYNCONLY_SRT}
comm -3 ${RRDPRSYNC_SRT} ${RSYNCONLY_SRT} > "${TIMESTAMP}.${LOC}.comm-rrdprsync-rsynconly.txt" &
comm -3 ${RRDPRSYNC_SRT} ${RRDPONLY_SRT}  > "${TIMESTAMP}.${LOC}.comm-rrdprsync-rrdponly.txt"  &
comm -3 ${RRDPONLY_SRT}  ${RSYNCONLY_SRT} > "${TIMESTAMP}.${LOC}.comm-rrdponly-rsynconly.txt"  &
wait
rm ${RRDPONLY_SRT} ${RSYNCONLY_SRT} ${RRDPRSYNC_SRT}

sha256 ${TIMESTAMP}* >> SHA256
signify -S -e -s /root/$(hostname).sec -m SHA256 -x SHA256.sig
