#!/usr/bin/env python
 
import os
import sys
import optparse
import commands
import getpass
 
 
extentions = ['doc','docm','docx','dotx','dotm','efx','eml','jpg','jpeg','msg','ost','pdf','PFB','pps','ppt',
              'pptx','pptm','potx','potm','ppsx','ppsm','pst','tiff','xls','xlsx','xlsm','xltx','xltm','xlw',
              'zip']
 
syntax = """
"""
 
try:
    parser = optparse.OptionParser(usage=syntax)
    parser.add_option("-v", "--stdout",dest = "stdout",action = "store_true",help = "Print files found to STDOUT")
    parser.add_option("-e", "--ext",dest = "extentions",action = "store",help = "Use specified extentions (Comma Sperated)")
    files = optparse.OptionGroup(parser,'File Information')
    files.add_option("-s", "--src",dest = "src_path",action = "store",help = "Source directory to scan")
    files.add_option("-d", "--dst",dest = "dst_path",action = "store",help = "destination directory to save files")
    files.add_option("-o", "--output",dest = "output_path",action = "store",help = "destination file for script output")
    parser.add_option_group(files)
    (options, args) = parser.parse_args()
 
    if getpass.getuser() != "root":
        print "You must be root to run this!"
        sys.exit()
 
    if options.extentions:
        extentions = list()
        for e in options.extentions.split(","):
            extentions.append(e)
 
    if len(args)> 0 or not options.src_path or not options.dst_path:
        print parser.print_help()
        sys.exit()
 
    if options.output_path:
        output = open(options.output_path,"w")
 
    for root, dirs, files in os.walk(options.src_path):
        for f in files:
            explode = str(f).split(".")
            if len(explode) > 1:
                extention = explode[len(explode)-1]
                if extention in extentions:
                    fullpath = os.path.join(root, f)
                    try:
                        os.stat(options.dst_path)
                    except:
                        os.makedirs(options.dst_path,mode=0755)
                    (status,routput) = commands.getstatusoutput('rsync -aR "' + fullpath + '" ' + options.dst_path)
                    if status == 0:
                        if options.output_path:
                            output.write(fullpath + "\n")
 
                        if options.stdout:
                            print fullpath
    if options.output_path:
        output.close()
 
except KeyboardInterrupt:
    print "\nexiting ..."
    sys.exit()