#__author__ = 'suxoza'
import sys,os
try:
  #from dict_dir import A
  from optparse import OptionParser
except ImportError:
    sys.exit("no optparse module found")


class MyError(Exception):pass

#-i /var/www/eruditor/view -e .html -t b -s paliko
#-i /var/www/eruditor/view -e .html -t b -s paliko -r "saxli shaliko"

#-i /var/www/eruditor/view -e .html -t b -s "\"http://www.onlineolimpiada/\""
#-i /var/www/eruditor/view -e .html -t b -s "\"http://www.onlineolimpiada/\"" -r  "\"http://www.onlineolimpiada/new/\""

class Repl:
    def __init__(self,kwargs):
        try:
            self.input       = kwargs['input']
            self.search      = kwargs['search']
            self.replace     = kwargs['replace']
            self.type        = kwargs['type']
            self.end         = kwargs['end']

            self.iterator    = 0

            self.action = 'search_' if not self.replace else 'replace_'
        except KeyError:
            print("parameters error")

    def find(self):
        if not os.path.exists(self.input):raise MyError("input path not found!!!")
        self.ret_array = []
        self.iterator = 0
        for (a,b,c) in os.walk(self.input):
            for file in c:
              if not os.path.isfile(os.path.join(a,file)):continue
              if self.end:
                  if not file.endswith(self.end):continue
              file_name = os.path.join(a,file)

              getattr(self,self.action)(file_name)


        for x in self.ret_array:
            print(x)
        print('sum:',self.iterator)


    def replace_(self,file):
        iter,line = 0,0
        mode = 'r' if self.type == 's' else 'rb'
        mode_w = 'w' if self.type == 's' else 'wb'
        count_ = 0
        line_ = []
        ret = "".encode()
        with open(file,mode) as op:
          for x in op:
              line += 1
              if self.search.encode() in x:
                  ret += x.replace(self.search.encode(),self.replace.encode())
                  line_.append(line)
                  len_ = len(x.split(self.search.encode())) - 1
                  count_ += len_
                  self.iterator += len_
              else:
                  ret += x
        if count_:
           self.ret_array.append(dict(count = count_,file = file,line = line_))
           with open(file,mode_w) as w:
              w.write(ret)


    def search_(self,file):
        iter,line = 0,0
        mode = 'r' if self.type == 's' else 'rb'
        count_ = 0
        line_ = []
        with open(file,mode) as op:
          for x in op:
              line += 1
              if self.search.encode() in x:
                  line_.append(line)
                  len_ = len(x.split(self.search.encode())) - 1
                  count_ += len_
                  self.iterator += len_
        if count_:self.ret_array.append(dict(count = count_,file = file,line = line_))





if __name__ == "__main__":

    parser = OptionParser()
    parser.add_option("-i", "--input", dest="input",help="input file")#myfindfile
    parser.add_option("-s", "--search", dest="search",help="search string") # -s paliko
    parser.add_option("-r", "--replace", dest="replace",help="search string") # -r shaliko
    parser.add_option("-t", "--type", dest="type",help="binary or string",default="b") # -t s || b
    parser.add_option("-e", "--end", dest="end",help="endswith") # -e .py

    (options, args) = parser.parse_args()



    a = Repl(vars(options))
    a.find()
