#!/usr/bin/env python3
#
#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 re, os, sys
from Patches import badcode

GPL_HEADER = '''
#
#    This file is part of Foobar.
#
#    Foobar 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.
#
#    Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.

'''
	
def fixer(name, savere):
	gpl_header = GPL_HEADER.replace("Foobar", name)
	with open("./temp.txt", mode="r") as f:
		files = f.read().split("\n")
	
	for file in files:
		if file == "":
			continue
		newfile = file.replace("youtube-dl", name).replace("youtube_dl", name)
		os.renames(file, newfile)
		
		#Get file text if this is a text file- if not, we're done with it
		try:
			with open(newfile, mode="r") as f:
				ans = f.read()
		except UnicodeDecodeError:
			continue
		#Don't waste time or add snowflake exceptions...
		if ans == "":
			continue
		#"Protect" github.com/rg3/youtube-dl URLs which point to things that can't be copied (e.g. issue reports),
		#Then convert the URLs as listed at the top of this file, then "unprotect" (i.e. youtu`be --> youtube)
		while True:
			resp = savere.search(ans)
			if resp == None:
				break
			ans = ans.replace(resp.group(0), resp.group(0).replace("youtube-dl", "youtu`be-dl"))
		ans = ans.replace("://github.com/rg3/youtube-dl", "://notabug.org/GPast/%s" % name)
		ans = ans.replace("://rg3.github.io/youtube-dl", "://notabug.org/GPast/%s/wiki" % name)
		ans = ans.replace("://yt-dl.org", "://notabug.org/GPast/%s/wiki" % name)
		ans = ans.replace("youtube-dl", name)
		ans = ans.replace("YOUTUBE-DL", name.upper())
		ans = ans.replace("youtube\-dl", name)
		ans = ans.replace("YOUTUBE\-DL", name.upper())
		ans = ans.replace("youtube_dl", name)
		ans = ans.replace("Youtube-dl", name.capitalize())
		ans = ans.replace("Youtube\-dl", name.capitalize())
		ans = ans.replace("Youtube-DL", name.capitalize())
		ans = ans.replace("Youtube\-DL", name.capitalize())
		ans = ans.replace("youtu`be", "youtube")
		f = open(newfile, mode="w")
		#Also, add GPL spiel at top (below key pseudo-comments!) if this is code
		if newfile.endswith(".py") or newfile.endswith(".sh"):
			while True:
				if ans[0] != "#":
					f.write("\n# Copyright (C) 2017 avideo authors (see AUTHORS)\n")
					f.write(gpl_header)
					f.write(ans)
					break
				seg, ans = ans.split("\n", maxsplit=1)
				f.write(seg+"\n")
		else:
			f.write(ans)
		f.close()


def wipe_code(src):
	ztdout = open("tmp-output", mode="w")
	
	for bcel in badcode.PATCHES:
		ztdout.write("\n\n./%s:\n" % bcel[0].upper())
		file, dic, n = src+bcel[0], bcel[1:], 0
		with open(file, mode="r") as f:
			edcode = f.read()
		
		for bc in dic:
			edsegs = edcode.split(bc[0])
			ztdout.write("%d: %d\n" % (n, len(edsegs)-1))
			edcode = bc[1].join(edsegs)
			n += 1
		
		with open(file, mode="w") as f:
			f.write(edcode)
	
	ztdout.close()
		





if __name__ == "__main__":
	wipe_code(sys.argv[1])
	fixer(sys.argv[2], re.compile("://github\\.com/rg3/youtube-dl/[a-z]+/[0-9]+"))