#(C) 2017 Paul Kramme

import zlib
import sys
import os

class color:
	HEADER = '\033[95m'
	OKBLUE = '\033[94m'
	OKGREEN = '\033[92m'
	WARNING = '\033[93m'
	FAIL = '\033[91m'
	ENDC = '\033[0m'
	BOLD = '\033[1m'
	UNDERLINE = '\033[4m'


def split(string, splitters):
	final = [string]
	for x in splitters:
		for i,s in enumerate(final):
			if x in s and x != s:
				left, right = s.split(x, 1)
				final[i] = left
				final.insert(i + 1, x)
				final.insert(i + 2, right)
	return final


def crc(filepath):
	previous = 0
	try:
		for line in open(filepath,"rb"):
			previous = zlib.crc32(line, previous)
	except OSError:
		print("CRC ERROR: OSERROR")
	return "%X"%(previous & 0xFFFFFFFF)


def scandirectory(walk_dir, scanfile, verbose = False):
	try:
		current_scan = []
		for root, subdirs, files in os.walk(walk_dir):
			#current_scan.extend([f"{root}\n"])
			for filename in files:
				if filename == "media-validation-tool" or filename == "media.csv" or filename == "media-validation-tool.py" or filename == "." or filename == "..":
					break
				else:
					#file_path = os.path.join(root, filename)
					if os.name == "posix":
						file_path = root + "/" + filename
					elif os.name == "nt":
						file_path = root + "\\" + filename
					else:
						pass
					checksum = crc(os.path.join(root, filename))
					current_scan.extend([file_path + "," + checksum + ",\n"])
		with open(scanfile, "w") as current_scan_file:
			current_scan_file.writelines(current_scan)			
	except FileNotFoundError:
		if verbose == True:
			print(color.FAIL + "SCAN ERROR: FILE NOT FOUND" + color.ENDC)

def validate(path = ".", verifile = "./media.csv"):
	print("Validating " + path)
	f = open(verifile, "r")
	verifileslist = f.readlines()
	scandirectory(".", "media.csv.1")
	fold = open("media.csv", "r")
	fnew = open("media.csv.1", "r")
	foldarray = fold.readlines()
	fnewarray = fnew.readlines()
	ffalsehash = []
	for line in fnewarray:
		splitline = split(line, ",")
		if line in foldarray:
			print(color.OKGREEN + "SUCCESS " + color.ENDC + splitline[0])
		else:
			break
			print(color.FAIL + "WRONG HASH " + color.ENDC + splitline[0])
			ffalsehash.extend(line)
	for line in foldarray:
		splitline = split(line, ",")
		if not line in fnewarray:
			if line not in ffalsehash:
				print(color.FAIL + "MISSING OR HASH FAIL " + color.ENDC + splitline[0])
			else:
				pass

def create(path = ".", verifile = "./media.csv"):
	print("Creating media file...")
	scandirectory(".", "media.csv")
	
def main():
	print("MEDIA VALIDATION TOOL")
	print("(C) 2017 Paul Kramme")
	if len(sys.argv) < 2:
		validate()
		input()
	else:
		create()
	
	

if __name__ == "__main__":
	main()


@@ -1882,23 +1882,11 @@
 ()%0A%09
-ffalsehash = %5B%5D
+%22%22%22
 %0A%09fo
@@ -2135,16 +2135,55 @@
 d(line)%0A
+%09%22%22%22%0A%09errorcount = 0%0A%09successcount = 0%0A
 %09for lin
@@ -2264,39 +2264,109 @@
 %0A%09%09%09
-if line not in ffalsehash
+print(color.FAIL + %22ERROR   %22 + color.ENDC + splitline%5B0%5D)%0A%09%09%09errorcount = errorcount + 1%0A%09%09else
 :%0A%09%09%09
-%09
 prin
@@ -2377,36 +2377,26 @@
 lor.
-FAIL + %22MISSING OR HASH FAIL
+OKGREEN + %22SUCCESS
  %22 +
@@ -2430,22 +2430,131 @@
 %0A%09%09%09
-else:%0A%09%09%09%09pass
+successcount = successcount + 1%0A%09print(color.BOLD, errorcount, %22errors in%22, successcount + errorcount, %22files%22, color.ENDC)
 %0A%0Ade
@@ -2784,16 +2784,16 @@
 e()%0A
-
 %09%09input(
 )%0A%09e
@@ -2788,16 +2788,44 @@
 %09%09input(
+%22Press ENTER to continue...%22
 )%0A%09else:

Remove double scanfile looping