#!/usr/bin/env python

import os, sys, time

if len(sys.argv) > 2:
    rootdir = sys.argv[1]
    webroot = sys.argv[2]
else:
    rootdir = "/home/danilo/blogce"
    webroot = "http://danilo.segan.org/blog/"

def recurse_dirs(root, dir = '', level = 0):
    xml = ""

    files = os.listdir(os.path.join(root, dir))
    for file in files:
        fullpath = os.path.join(root, dir, file)

        loc = os.path.join(webroot, dir, file)
        lastmod = time.strftime("%Y-%m-%d", time.gmtime(os.path.getmtime(fullpath)))
        if level > 1:
            changed = "weekly"
        else:
            changed = "daily"
        priority = 1.0 - 1.0 / (level + 2.0)

        if os.path.isdir(fullpath):
            xml += """
            <url>
              <loc>%s</loc>
              <lastmod>%s</lastmod>
              <changefreq>%s</changefreq>
              <priority>%0.1f</priority>
            </url>""" % (loc, lastmod, changed, priority)

            xml += recurse_dirs(root, os.path.join(dir, file), level+1)
        elif file[-4:].lower() == ".txt":
            loc = os.path.join(webroot, dir, file[:-4])
            changed = "monthly"
            xml += """
            <url>
              <loc>%s</loc>
              <lastmod>%s</lastmod>
              <changefreq>%s</changefreq>
              <priority>%0.1f</priority>
            </url>""" % (loc, lastmod, changed, priority)

    return xml


xml = """<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">"""
xml += """
            <url>
              <loc>%s</loc>
              <lastmod>%s</lastmod>
              <changefreq>daily</changefreq>
              <priority>1.0</priority>
            </url>""" % (webroot, time.strftime("%Y-%m-%d"))
xml += recurse_dirs(rootdir)
xml += """\n</urlset>"""        

print "Content-type: text/xml\n"
print xml
