#!/bin/sh

# Tar up dumped consensuses, statuses, descriptors etc from per-month folders
# into per-month tarballs.

# Copyright (c) 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.

set -e
set -x
set -u

usage() {
	echo "Usage: $0 <year> <month>" >&2
	echo "       $0 last            (does last month)" >&2
	exit 1
}

if [ -z "${1:-}" ]; then
	usage
fi

if [ "$1" = "last" ]; then
	year=`date --date="last month" +'%Y'`
	month=`date --date="last month" +'%m'`
elif [ -z "${2:-}" ]; then
	usage
else
	year="$1"
	month="$2"
fi

if [ "$year" -lt 2000 ] || [ "$year" -gt 2020 ] ||
   [ "$month" -lt 1 ] || [ "$month" -gt 12 ] ||
   [ "`echo -n $month | wc -c`" != 2 ]; then
	usage
fi


this_year=`date --utc +'%Y'`
this_month=`date --utc +'%m'`

if [ "`date -d $this_year-$this_month-01 +%s`" -le "`date -d $year-$month-01 +%s`" ]; then
	echo "Date in the future or current month?" >&2
	exit 1
fi





for file in \
	"extra-infos-$year-$month.tar.bz2"		\
	"server-descriptors-$year-$month.tar.bz2"	\
	"consensuses-$year-$month.tar.bz2"		\
	"statuses-$year-$month.tar.bz2"			\
	; do
	if [ -e "$file" ]; then
		echo "$file already exists" >&2
		exit 1
	fi
done

for dir in \
	"extra-infos-$year-$month"		\
	"server-descriptors-$year-$month"	\
	"consensus/$year/$month"		\
	"status/$year/$month"			\
	; do
	if ! [ -d "$dir" ]; then
		echo "$dir not found" >&2
		exit 1
	fi
done

for dir in \
	"consensuses-$year-$month"		\
	"statuses-$year-$month"			\
	; do
	if [ -e "$dir" ]; then
		echo "$dir already exists" >&2
		exit 1
	fi
done

for kind in consensus status; do
	mv "$kind"/$year/$month "$kind"es-$year-$month
	find "$kind"es-$year-$month -type f -name '*.bz2' -print0 | xargs -0 bunzip2 -v
	tar cjvf "$kind"es-$year-$month.tar.bz2 "$kind"es-$year-$month
	rm -rf "$kind"es-$year-$month
done

for kind in extra-infos server-descriptors; do
	tar cjvf "$kind"-$year-$month.tar.bz2 "$kind"-$year-$month
	rm -rf "$kind"-$year-$month
done



[ -d Archive ] || mkdir Archive

for kind in consensus status; do
	t="$kind"es-$year-$month.tar.bz2
	! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
done

for kind in extra-infos server-descriptors; do
	t="$kind"-$year-$month.tar.bz2
	! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
done
