#! /usr/bin/python


import urllib
import urllib2
import BeautifulSoup
import os.path
import stat
import time
from datetime import datetime

MIRROR="http://widehat.opensuse.org/repositories/home:/mithro/"

def getindex(distro, version):
	url = "%s/%s/%s" % (MIRROR, distro, version)
	try:
		data = urllib2.urlopen(url).read()
	except urllib2.URLError, e:
		print "Unable to download %s, got %s" % (url, e)
		return {}

	tree = BeautifulSoup.BeautifulSoup(data)

	r = {}
	for link in tree.findAll('a'):
		if not link['href'].endswith('.deb'):
			continue
		date, stime, size = link.nextSibling.strip().split()

		dt = datetime(*time.strptime("%s %s" % (date, stime), "%d-%b-%Y %H:%M")[:-3])

		r[link['href']] = dt
	return r

def datefile(file):
	return datetime.fromtimestamp(os.stat(file)[stat.ST_MTIME])

def almostequal(d1, d2):
	for bit in "year", "month", "day", "hour", "minute":
		if getattr(d1, bit) != getattr(d2, bit):
			return False
	return True

import sys
def reportdownload(block, blocksize, total):
	sys.stdout.write('.')
	sys.stdout.flush()
	if (block+1) % 50 == 0:
		count = str(block*blocksize/1024)
		of    = str(total/1024)
		space = " "*(len(of)-len(count))
		print " %s%s of %s kb" % (space, count, of)
		print " ",
	if block == int(total/blocksize)+1:
		print
		print " Total %s kb downloaded" % (total/1024) 

def getfiles(codename):
	distro, dontget = mapping[codename]

	if distro is None:
		return

	distropath = os.path.join("incoming", distro)
	if not os.path.exists(distropath):
		os.mkdir(distropath)

	for section in 'all', 'amd64', 'i386':
		files = getindex(distro, section)

		sectionpath = os.path.join(distropath, section)
		if not os.path.exists(sectionpath):
			os.mkdir(sectionpath)

		for filename, date in files.items():
			fileremote = os.path.join(section, filename)
			filepath   = os.path.join(sectionpath, filename)

			get = True
			for filter in dontget:
				if filter in filename:
					get = False
					break
			for filter in mapping['all']:
				if filter in filename:
					if "tpserver" in filename:
						continue
					get = False
					break

			if not get:
				packagename = os.path.basename(filename).split("_")[0]
				os.system("reprepro -A %s remove %s %s > /dev/null 2>&1" % (section, codename, packagename))
				print "Skipping file", fileremote
				continue

			if not os.path.exists(filepath) or not almostequal(datefile(filepath), date):
				print "Downloading %s" % fileremote
				print " ",
				urllib.urlretrieve("%s/%s/%s" % (MIRROR, distro, fileremote), filepath, reportdownload)
	
				stamp = time.mktime(date.timetuple())

				os.utime(filepath, (stamp, stamp))

				packagename = os.path.basename(filename).split("_")[0]
				os.system("reprepro -A %s remove %s %s" % (section, codename, packagename))
				os.system("reprepro includedeb %s %s" % (codename, filepath))
			else:
				print "Already got %s" % filepath

mapping = {
	'all'   : ["guile", "mzscheme", "drscheme"],
	'etch'  :    ('Debian_4.0',   []),
	'lenny' :    ('Debian_5.0',   []),
	'sid'   :    (None, []),
	'gutsy' :    ('xUbuntu_7.10', []),
	'hardy' :    ('xUbuntu_8.04', ["devil","freeimage","zzip","xerces"]),
	'intrepid' : ('xUbuntu_8.10', [] ),
	'jaunty' :   ('xUbuntu_9.04', [] ),
}

if __name__ == "__main__":
	if len(sys.argv) > 2:
		MIRROR=sys.argv[2]
	getfiles(sys.argv[1])
