// Lua Decrypter By Kettle_
#include <windows.h>
#include <iostream>
#include <string>

#include "Bootil/Bootil.h"

using namespace Bootil;
using namespace std;

int main()
{
	char result[MAX_PATH];
	GetModuleFileName(NULL, result, MAX_PATH);

	String::List FileList;
	std::string Folder = string(result).substr(0, string(result).find_last_of("\\") + 1);

	std::cout << Folder << "\n";

	File::GetFilesInFolder(Folder, FileList, false);

	int decryptcount = 0;
	BOOTIL_FOREACH_CONST(filename, FileList, String::List)
	{
		std::string file = *filename;
		if (file.substr(file.find_last_of(".") + 1) != "lua") {
			continue;
		}

		AutoBuffer readbuffer;
		File::Read(Folder + "/" + *filename, readbuffer);

		AutoBuffer writebuffer;

		std::cout << "Decrypting: " << *filename << "\n";
		Compression::LZMA::Extract(readbuffer.GetBase(4), readbuffer.GetSize()-4, writebuffer);
		File::Write(Folder + "/" + *filename, writebuffer);

		decryptcount++;
	}

	if (decryptcount < 1) {
		std::cout << "No lua files to decrypt!\n";
	}
	else{
		std::cout << "Decrypted " << decryptcount << " files\n";
	}

	std::cin.get();
}