
#import train
import sys
import getopt
from datetime import datetime as dt
def Usage():
	print("[*] Usage: python main.py [option(s)] [file(s)/folder]")
	print("[*] The options are:")
	print("\t-h --help\t\t: Display this infomation")
	print("\t-T --train\t\t: Train the model with specific dataset")
	print("\t-t --test\t\t: Test the given APK file")
	print("\t-f --file\t\t: Given a file to test")
	print("\t-F --folder\t\t: Given a folder to test")
	print("\t-d --dataset\t\t: Given dataset to train the model")
	print("[*] Example:")
	print("\tpython main.py -T -d [dataset/malign] [/home/tantran/desktop/dataset/malign]")
	print("\tpython main.py -t -f [ben1.apk] [/home/tantran/desktop/test/ben1.apk]")
	print("\tpython main.py -t -F [testsamples] [/home/tantran/desktop/test/testsamples]")
	print("[*] Note:")
	print("\tUse ben1.apk or test/ben1.apk if is the same directory")
	print("\tUse home/tantran/desktop/test/ben1.apk if is another directory")
	print("\tYou can input more than 1 file in test process, separate it using ',' without space. Example: apkfile1,apkfile2")

def TESTFUNC(TEST_FILE, TEST_FOLDER):
	if TEST_FILE == "" and TEST_FOLDER == "":
		print("[*] Have you miss something? Please give file or folder to test!!")
		sys.exit(2)
	if TEST_FILE != "" and TEST_FOLDER != "":
		print("[*] Oh! Just give one. File or folder please!!")
		sys.exit(2)
	print("[*] Start analyze")
	import test
	start_time = dt.now()
	if TEST_FILE != "":
		test.TESTFILE(TEST_FILE)
	if TEST_FOLDER != "":
		test.TESTFOLDER(TEST_FOLDER)
	print("[*] Time for analysis: {0}".format(dt.now() - start_time))
	print("[*] Finished analyze")

def TRAINFUNC(DATASET):
	print("[*] I am so sorry! This feature is current no support!! Comeback later bro!")
	print("[*] Use my trained model, enough to help you!")
	print("[*] If you want to train the model again, please contact me for more information")
	print("[*] Email: trannhattan310399@gmail.com")

def main(argv):
	options = "hTtf:F:d:"
	long_options = ["help", "train", "test", "file=", "folder=", "dataset="]
	TRAIN = 0
	TEST = 0
	TEST_FILE = ""
	TEST_FOLDER = ""
	DATASET = ""
	try:
		opts, args = getopt.getopt(argv, options, long_options)
	except getopt.GetoptError:
		Usage()
		sys.exit(2)
	for arg, val in opts:
		if arg in ("-h", "--help"):
			Usage()
			sys.exit(2)
		if arg in ("-T", "--train"):
			TRAIN = 1
		if arg in ("-t", "--test"):
			TEST = 1
		if arg in ("-f", "--file"):
			TEST_FILE = val
		if arg in ("-F", "--folder"):
			TEST_FOLDER = val
		if arg in ("-d", "--dataset"):
			DATASET = val
	if TEST == TRAIN:
		Usage()
		sys.exit(2)
	if TEST == 1:
		TESTFUNC(TEST_FILE, TEST_FOLDER)
	elif TRAIN == 1:
		TRAINFUNC(DATASET)
		
if __name__ == "__main__":
	if len(sys.argv) == 1:
		Usage()
		sys.exit(2)
	main(sys.argv[1:])
