"""
Remembers the original posting dates of stories.

Requires a file in the server that is writable by the webserver. (Default file
is py['datadir']/DATES).

The basic idea for this plugin comes from Richard Chamberlain's rembermberdates.py
that I couldn't make work.  So I wrote this one.
"""
__author__ = "Sam Clegg <sam@superduper.net>"
__version__ = "0.1"

import os, time
import cPickle as pickle
from Pyblosxom import tools

dates = {}
update = 0

def verify_installation(request):
    return 1

def cb_start(args):
    global dates
    config = args['request'].getConfiguration()
    file = os.path.join(config['datadir'], 'DATES')
    if os.path.exists(file):
        f = open(file)
        dates = pickle.load(f)
        f.close()

def cb_end(args):
    if update:
        config = args['request'].getConfiguration()
        file = os.path.join(config['datadir'], 'DATES')
        f = open(file, 'w')
        pickle.dump(dates, f, 1)
        f.close()

def cb_filestat(args):
    global update
    request = args["request"]
    stattuple = args["mtime"]
    filename = args["filename"]
    if filename in dates:
        stattuple = list(stattuple)
        stattuple[8] = dates[filename]
    else:
        dates[filename] = stattime
        update = 1
    args["mtime"] = stattuple
    return args
