#!/usr/bin/env python

# doc-find:
# Search for documentation related to the given strings.
# Relies on "locate" and assumes that the locate database is current.
#
# Copyright 2003 by Akkana Peck.
# You may use, distribute or modify this program under the terms of the GPL.

import sys, os, string, re

# Some filenames we consider to be documentation-related.
# If you add to this list, add only lowercase!
# We will lowercase the filenames being compared
# (to make comparisons case insensitive).
docfilenames = [
	  "changelog",
	  "readme",
	  "install",
	  "howto",
	  "authors",
	  "news",
	  "todo",
	  "config",
	  "sample",
	  "samples",
	  "example",
	  "examples",
	  "ref",
	  "guide",
	  "manual",
	  "quickstart",
	  "thanks",
	  "notes",
	  "features",
	  "faq",
	  "acknowledgement",
	  "bugs",
	  "problems"
]

def system_out (cmdstr) :
    retlist = []
    fp = os.popen(cmdstr)
    while 1:
        s = fp.readline()
        if not s : break
        retlist.append(s)
    fp.close()
    return retlist

# main()
for arg in sys.argv :
    #print string.split(arg, " \t./")

    files = system_out("locate " + arg + " | grep -w " + arg);

    for path in files :
        #print path

        # Special case for anything with "man", "doc", or "info" in the path:
        if ((string.find(path, "/man") >= 0)
               or (string.find(path, "/doc") >= 0)
               or (string.find(path, "/info") >= 0)) :
            print path,
            #print "Basename is", os.path.basename(path)
            continue

        # Now see if it matches any of the docfilenames:
        base = os.path.basename(path)
        for nam in docfilenames :
            if base == "" : continue

            # Non full word search would use this:
            #if (string.find(string.lower(base), nam) >= 0) :

            # Full word search:
            # Make a regexp to search for nam as full-word only
            #pat = "^" + nam + "$"
            if (re.compile(nam).search(base, 1)) :
                print path,
                base = ""
                continue