#Copyright (C) 2017 avideo authors (see AUTHORS)
#
#    This file is part of AVideo.
#
#    AVideo is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    AVideo is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with AVideo.  If not, see <http://www.gnu.org/licenses/>.

import os

#Reader Function- used to read fragments from external files, for the sake of cleanliness
def rd(name, lines=1):
	with open(name, mode="r") as f:
		text = f.read().split("\n", maxsplit=lines)
	return text

files, PATCHES = {}, []
for dirname in ("Patches/Perm/", "Patches/Bug/"):
	if not os.path.exists(dirname):
		continue
	for fileX in sorted(os.listdir(dirname)):
		if fileX.startswith("g"):
			break
		else:
			comment, link, scab = rd(dirname+fileX, 2)
			file, link = comment[1:].split(":", maxsplit=1)[0], link[4:]
			if link == "":
				link = rd(dirname+"g"+fileX)[1]
			else:
				link = link.replace("\\n", "\n").replace("\\t", "\t").replace("\z", "")
			if file in files.keys():
				PATCHES[files[file]].append((scab, link))
			else:
				files[file] = len(PATCHES)
				PATCHES.append([file, (scab, link)])

#The youtube-dl or whatever --> youtu`be-dl or whatever patches in namae.txt

f = open("Patches/namae.txt", mode="r")
segs = f.read().split("\n\n")[1:]
f.close()
	
for seg in segs:
	seglets = seg.split("\n")
	SAFECODES = [seglets[0][:-1]]
	for seglet in seglets[1:]:
		if seglet != "":
			SAFECODES.append((seglet, seglet.replace("youtube-dl", "youtu`be-dl")))
	PATCHES.append(SAFECODES)