# script to publish a level
import urlparse, urllib, datetime, os.path, hashlib, sys, getpass, uuid, zipfile
import xml.etree.ElementTree as ET

# usage:
# python2.7 publish.py <rss_feed> <level_dir>
# The script expects a manifest.xml file in the level dir, that is a stripped-down item entry for the rss feed (i.e. minus the automatically generated parts). If no manifest exists a template will be generated,
# and if no rss feed exists a template for that will be generated as well.
# Both should be edited before publishing. You need to change the descriptions, names and urls. You can change the guid as well if you don't want a random one, but it should stay unchanged after you publish a
# level. 

# You will then have to upload the resulting .rss and .zip files to your web space.

RFC822format = "%a, %d %b %Y %H:%M:%S +0000"

# elementtree should really get a built-in pretty-print
def indent(elem, level=0):
    i = "\n" + level * "\t"
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "\t"
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

def main(*argv):
    utcnow = datetime.datetime.utcnow().strftime(RFC822format)

    rssPath = os.path.abspath(argv[1])