#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import wxoptparse
import optparse, sys, subprocess

parser = optparse.OptionParser()
parser.add_option('-f', metavar='FOLDER', dest='folder', default=".",
    help='Folder where to begin the search')
parser.add_option('--name', metavar='NAME', dest='pattern', default="*",
     help='Search for this file pattern (use * and ?)')
parser.add_option('--print', dest='bPrint', action='store_true', default=True,
     help='Show the files found')
parser.add_option('--type', dest='type', type="choice", choices=('f', 'd', 'l'),
    help="File type: f, d, l for file, directory or link, respectively")
parser.add_option('--exec', dest='execute',
    help='Execute: example, "gzip {} \\;"')
parser.add_option('--user', dest='user',
    help='Files for user x')
parser.add_option('--group', dest="group",
    help='Files with group x')
parser.add_option('--mtime', dest="hoursago", type="int",
    help='File was modified n hours ago.')

(options, args) = parser.parse_args()

parser.commandLineLst = [x.replace('--', '-') for x in parser.commandLineLst if x != '-f']
parser.commandLineLst.insert(0, 'find')
if options.execute:
    for nIndex, param in enumerate(parser.commandLineLst):
        if param == '-exec':
            parser.commandLineLst[nIndex + 1] = parser.commandLineLst[nIndex + 1][1:-1]
            print "Special handling for exec '%s'" % (parser.commandLineLst[nIndex + 1])
            break
        
cmdLine = " ".join(parser.commandLineLst)
print cmdLine
try:
    retcode = subprocess.call(cmdLine, shell=True)
    if retcode < 0:
        print >>sys.stderr, "Child was terminated by signal", -retcode
    else:
        print >>sys.stderr, "Child returned", retcode
except OSError, e:
    print >>sys.stderr, "Execution failed:", e
