#!/usr/bin/python

import sys
import os
import mmap
from binaryornot.check import is_binary

print("parameter1: "+sys.argv[1])
print("parameter2: "+sys.argv[2])

rootDir = str(sys.argv[1])
searchWord = str(sys.argv[2])

#To byte ARRAY
arr = bytes(searchWord, 'utf-8')
print(arr)


file = "applanga_settings.applanga"
file2 = "fruit.json"

def my_mmap(myfile):
    mfd = os.open(myfile, os.O_RDONLY)
    print("Opening file: "+file)
    print ("Searching for: "+ str(arr))
    #with open(file, "r") as f:
    mm = mmap.mmap(mfd, 0, prot=mmap.PROT_READ)
    a = mm.find(arr)
    if a == -1:
        print("Not Found\n")
    else:
        print("Found\n")

for rootDir, dirs, files in os.walk(rootDir):
    for file in files:
        #print("File: "+str(file))
        path = os.path.join(rootDir,file)
        #print("Filepath: "+ path)
        if is_binary(path):
            my_mmap(path)
