#!/usr/bin/python2.2

# Make sure tabs expand to 8 spaces in vim
# vim: expandtab

# Copyright 2002 Raphaël Hertzog
# This file is distributed under the terms of the General Public License
# version 2 or (at your option) any later version.

import os, os.path, sys, email, common

from config import dir, odir, root

os.environ["PATH"] = "/bin:/sbin:/usr/bin:/usr/sbin"

parser = email.Parser.Parser()
msg = parser.parse(sys.stdin)

info = common.extract_info(msg)

subdir = "news"
nbmsg = 30
if info.has_key("static") or (len(sys.argv) >= 2 and sys.argv[1] == "static"):
    subdir = "static"
    nbmsg = 5

if info.has_key("package"):
    pkg = info["package"]
    hash = pkg[0]
    if pkg[:3] == "lib": hash = pkg[:4]
    srcdir = "%s/base/%s/%s" % (root, hash, pkg)
    if not os.path.isdir(srcdir):
        sys.exit("Invalid source package: %s" % pkg)
    outdir = srcdir + "/" + subdir
    htmldir = "%s/web/%s/%s/%s" % (root, hash, pkg, subdir)
    common.save_msg_in_dir(msg, outdir, nbmsg)
    common.generate_html_msg(outdir, htmldir, nbmsg)
    # Synchronize the XML document
    f = os.popen("%s/bin/update_news.py" % root, "w")
    f.write(pkg+"\n")
    f.close()
    # Regenerate the HTML page
    f = os.popen("%s/bin/generate_html.sh" % root, "w")
    f.write(pkg+"\n")
    f.close()
else:
    sys.exit("The mail must have a 'Package: <srcpkg>' pseudo-header ...")

