#!/bin/bash

#set -x

dists=${1:-wheezy}

for y in 2011 2012 ; do
  for m in {1..12}; do
    if [ $m -lt 10 ] ; then m="0"$m ; fi
    line=$(wget -O - "http://snapshot.debian.org/archive/debian/?year=$y;month=$m" | grep "$y-$m-10" | head -1)
    date=$(echo $line | perl -0ne 'print "$1\n" while (/a href=\"(.*?)\">.*?<\/a>/igs)')
    url=http://snapshot.debian.org/archive/debian/$date/dists/$dists/main/binary-amd64/Packages.bz2
    mkdir -p archive/$date/dists/$dists/main/binary-amd64
    wget -c -O archive/$date/dists/$dists/main/binary-amd64/Packages.bz2 $url

    line=$(wget -O - "http://snapshot.debian.org/archive/debian/?year=$y;month=$m" | grep "$y-$m-25" | head -1)
    date=$(echo $line | perl -0ne 'print "$1\n" while (/a href=\"(.*?)\">.*?<\/a>/igs)')
    url=http://snapshot.debian.org/archive/debian/$date/dists/$dists/main/binary-amd64/Packages.bz2
    mkdir -p archive/$date/dists/$dists/main/binary-amd64
    wget -c -O archive/$date/dists/$dists/main/binary-amd64/Packages.bz2 $url
  done
done
