#!/usr/bin/env bash

set -e
set -u

TS=$( TZ=UTC date +"%FT%H%M%SZ" )

AUTHORITY_NAME=serge

STORE="/srv/bridges.torproject.org/from-authority-bridge-directories"
TARGET="/srv/bridges.torproject.org/from-${AUTHORITY_NAME}"
FN="from-${AUTHORITY_NAME}-$TS.tar.gz"

cd "$STORE"
t=$(tempfile -d ".")
trap "rm -f '$t'" EXIT
cat > "$t"
ln "$t" "$FN"
rm -f "$t"
trap - EXIT

l="from-${AUTHORITY_NAME}-latest.tar.gz"
rm -f "$l.new"
ln "$FN" "$l.new"
mv "$l.new" "$l"

cd "$TARGET"
# We're using chmod to make some of the resulting files readable to the bridgedb
# group, so both bridgedb and rdsys (which is in the group) are able to read the
# files.
tar xzf "$STORE/$FN" && chmod 640 cached-extrainfo*

~/bin/sync-to-colchicifolium
