#!/usr/bin/env python
#******************************************************************************\
#* Copyright (C) 2003-2004 Martin Blais <blais@furius.ca>
#*
#* This program is free software; you can redistribute it and/or modify
#* it under the terms of the GNU General Public License as published by
#* the Free Software Foundation; either version 2 of the License, or
#* (at your option) any later version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program; if not, write to the Free Software
#* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#*
#*****************************************************************************/

"""find-decrypted [<options>] [<dir> [<dir> ...]]

Looks for forgotten decrypted files in the given directories.  This script can
be run to insure that there are not decrypted files left in a directory where
there are encrypted files.  Output is the name of the files that are decrypted
and that probably should not be there.

I typically use this on my doubly encrypted files: my emacs setup can read and
write encrypted files without backups, but not for doubly-encrypted files.
Sometimes I forget decrypted files in those directories.  Not a good thing.
"""

__version__ = "Revision: 1.3 "
__author__ = "Martin Blais <blais@iro.umontreal.ca>"


import sys, os
from os.path import *


def finddec(dn, oss, rec):

    for dn, dirs, files in os.walk(dn):
        for fn in files:
            base, ext = splitext(fn)
            if ext in ['.gpg', '.asc']:
                decfn = join(dn, base)
                if exists(decfn) and not isdir(decfn):
                    ##print >> oss, '%s  (%s)' % (decfn, join(dn, fn))
                    print >> oss, '%s' % decfn
        if not rec:
            del dirs[:]


def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip(), version=__version__)
    parser.add_option('-l', '--local', '--non-recursive', action='store_true',
                      help="searches recursively in the given directories")
    opts, args = parser.parse_args()

    if not args:
        args = ['.']

    for dn in args:
        finddec(dn, sys.stdout, not opts.local)

if __name__ == '__main__':
    main()
