#!/usr/bin/env python

import pysvn
import getopt
import sys
import string
import time

# Options by default
reverse = False
path = "." # Current directory
user = None
elapsed = False
MAX = 60
locale = "US-ASCII"

def first_line(text):
    end_of_line = text.find("\n")
    if end_of_line == -1:
        if len(text) < MAX:
            return text
        else:
            return text[0:MAX]
    else:
        if end_of_line < MAX:
            return text[0:end_of_line]
        else:
            return text[0:MAX]

def time2string(thetime):
    return time.strftime ("%Y-%B-%d %H:%M", time.localtime(thetime))

def time_ago(thetime):
    elapsed = time.time() - thetime
    years = int(elapsed / (86400 * 365.25))
    if years > 0:
        if years > 1:
            return "%s years" % years
        else:
            return "One year"
    months = int(elapsed / (86400 * 30.5))
    if months > 0:
        if months > 1:
            return "%s months" % months
        else:
            return "One month"
    weeks = int(elapsed / (86400 *7))
    if weeks > 0:
        if weeks > 1:
            return "%s weeks" % weeks
        else:
            return "One week"
    days = int(elapsed / 86400)
    if days > 0:
        if days > 1:
            return "%s days" % days
        else:
            return "One day"
    hours = int(elapsed / 3600)
    if hours > 0:
        if hours > 1:
            return "%s hours" % hours
        else:
            return "One hour"
    else:
        return "A very little time"
try:
    optlist, args = getopt.getopt (sys.argv[1:], "rp:l:e",
                                   ["reverse", "path=", "elapsed", "locale="])
    for option, value in optlist:
        if option == "--reverse" or option == "-r":
            reverse = True
        elif option == "--elapsed" or option == "-e":
            elapsed = True
        elif option == "--path" or option == "-p":
            path = value
        elif option == "--locale" or option == "-l":
            locale = value
        else:
            raise Exception ("Unknown option " + str(option))
except getopt.error, reason:
    raise Exception ("Usage: " + sys.argv[0] + ": " + str(reason))
if len(args) > 0:
    user = args[0]
client = pysvn.Client()
log = client.log(path)
if reverse:
    log.reverse()
for message in log:
    if user is None or user == message['author']:
        log_message = unicode(first_line(string.strip(message['message'])),
                              "UTF-8") # Subversion is always in UTF-8
        if not log_message:
            log_message = "EMPTY LOG MESSAGE"
        if user is not None:
            author = ""
        else:
            author = "by %s" % message['author']
        if not elapsed:
            date = time2string(message['date'])
        else:
            date = "%s ago" % time_ago(message['date'])
        print "%s (%i): \"%s\" %s" % (date,
                                      message['revision'].number,
                                      log_message.encode(locale, "replace"),
                                      author)
    

