#!/bin/bash

# Download all current v3 directory status votes and the consensus document,
# then download the descriptors and extra info documents.

# Copyright (c) 2005, 2006, 2007, 2008 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

TZ=UTC
export TZ

DIRSERVERS=""
DIRSERVERS="$DIRSERVERS 86.59.21.38:80"		# tor26
DIRSERVERS="$DIRSERVERS 128.31.0.34:9031"	# moria1
DIRSERVERS="$DIRSERVERS 216.224.124.114:9030"	# ides
DIRSERVERS="$DIRSERVERS 80.190.246.100:80"	# gabelmoo
#DIRSERVERS="$DIRSERVERS 140.247.60.64:80"	# lefkada
DIRSERVERS="$DIRSERVERS 194.109.206.212:80"	# dizum
#DIRSERVERS="$DIRSERVERS 128.31.0.34:9032"	# moria2
DIRSERVERS="$DIRSERVERS 213.73.91.31:80"	# dannenberg
DIRSERVERS="$DIRSERVERS 208.83.223.34:443"	# urras
TIME=$(date "+%Y%m%d-%H%M%S")

. fetch-all-functions

consensus=""
tmpdir="consensus/tmp"
[ -d "$tmpdir" ] || mkdir -p "$tmpdir"
for dirserver in $DIRSERVERS; do
	wget -q -O "$tmpdir/$TIME-consensus" http://$dirserver/tor/status-vote/current/consensus
	if [ "$?" != 0 ]; then
		rm -f "$tmpdir/$TIME-consensus"
		continue
	fi

	freshconsensus="$tmpdir/$TIME-consensus"

	timestamp=$(awk '$1=="valid-after" {printf "%s-%s", $2, $3}' < "$freshconsensus")
	datedir=$(awk '$1=="valid-after" {printf "%s", $2}' < "$freshconsensus" | tr '-' '/')
	dir="consensus/$datedir"
	[ -d "$dir" ] || mkdir -p "$dir"


	consensus="$dir/$timestamp-consensus.bz2"
	if ! [ -e "$consensus" ]; then
		# the consensus is new, or at least we don't have it yet
		bzip2 "$freshconsensus"
		mv "$freshconsensus.bz2" "$consensus"
		break
	fi

	rm -f "$freshconsensus"
	echo "Consensus from $timestamp (gotten from $dirserver) already exists!" >&2
	# maybe there is a newer one on a different authority, so try again.
done

if [ "$consensus" = "" ]; then
	echo "No consensus available" 2>&1
	exit 1
fi


votes=$(bzcat $consensus | awk '$1 == "vote-digest" {print $2}')
for vote in $votes; do
	for dirserver in $DIRSERVERS; do
		wget -q -O "$dir/$TIME-vote-$vote" http://$dirserver/tor/status-vote/current/d/$vote
		if [ "$?" != 0 ]; then
			rm -f "$dir/$TIME-vote-$vote"
			continue
		fi
		break
	done
	if [ -e "$dir/$TIME-vote-$vote" ]; then
		voteridentity=$(awk '$1=="fingerprint" {print $2}' < "$dir/$TIME-vote-$vote")
		if [ -e "$dir/$timestamp-vote-$voteridentity-$vote.bz2" ]; then
			echo "Vote $vote from $voteridentity already exists!" >&2
			rm -f "$dir/$TIME-vote-$vote"
			continue;
		fi
		mv "$dir/$TIME-vote-$vote"  "$dir/$timestamp-vote-$voteridentity-$vote"
		bzip2 "$dir/$timestamp-vote-$voteridentity-$vote"
	else
		echo "Failed to get vote $vote!" >&2
	fi
done

digests=$( for i in ` bzcat $consensus | awk '$1 == "r" {printf "%s=\n", $4}' | sort -u `; do
		echo $i | \
		base64-decode | \
		perl -e 'undef $/; $a=<>; print unpack("H\*", $a),"\n";';
	done )
for digest in $digests; do
	fetch_digest "$digest" "server-descriptor"
done
