#!/usr/bin/python
# 
#  substitute bugs with links
#
import string, re

bugz_pattern = re.compile('(bug *([0-9]+))', re.IGNORECASE)
bugs_pattern = re.compile('(bugscape *([0-9]+))', re.IGNORECASE)
# request from Kin Blas for blackflag ;)
bf_pattern = re.compile('(blackflag *([0-9]+))', re.IGNORECASE)
bugzilla_link = 'http://bugzilla.mozilla.org/show_bug.cgi?id='
bugscape_link = 'http://bugscape.mcom.com/show_bug.cgi?id='
blackflag_link = 'http://blackflag.mcom.com/show_bug.cgi?id='

def replace_bugs(line):
    """ replace the bug numbers with a link """
    if not len(line): return line
    line =  re.sub(bugs_pattern, r'<a href="%s\2">\1</a>' % bugscape_link, line)
    line =  re.sub(bugz_pattern, r'<a href="%s\2">\1</a>' % bugzilla_link, line)
    line =  re.sub(bf_pattern, r'<a href="%s\2">\1</a>' % blackflag_link, line)
    return line

if __name__ == '__main__':
    import sys
    print replace_bugs(string.join(sys.argv[1:]))
