/* asc-to-c.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2009 */

/*

Copyright (c) 2009 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/*
 * This tool is used to transform all the .asc files from
 * the include/sswf/scripts folder into a set of C++ files
 * that can then be compiled and used internally instead
 * of keeping the external files.
 *
 * Note: the list of files is given on the command line.
 * The tool itself does not know how to scan directories.
 *
 * The result is a set of strings and one array with the
 * list of filenames and string pointers.
 *
 * The result is printed in stdout.
 */

#include	<vector>
#include	<fstream>
#include	<iostream>

#include	"sswf/libsswf_as.h"

std::vector<char *>	filenames;

const char *		g_progname;

void usage()
{
	fprintf(stderr, "Usage: %s [-opts] filenames\n", g_progname);
	fprintf(stderr, "Where -opts is one or more of:\n");
	fprintf(stderr, "  -h      shows this help screen\n");
}

void header_to_cpp(const char *filename, int index)
{
	std::ifstream	f(filename);
	std::string	line;
	int		cnt;
	char		c;
	bool		newline;

	if(f.bad()) {
		fprintf(stderr, "error:%s: cannot open \"%s\" for reading.\n", g_progname, filename);
		exit(1);
	}

	std::cout << "const char *file" << index << " = // Source: " << filename << std::endl;

	for(cnt = 0, newline = true; !f.eof();) {
		if(newline) {
			++cnt;
			std::cout << "\t/*" << cnt << "*/ \"";
			newline = false;
		}
		f.get(c);
		if(c == '\n') {
			std::cout << "\\n\"" << std::endl;
			newline = true;
		}
		else if(c == '"') {
			std::cout << "\\" << c;
		}
		else {
			std::cout << c;
		}
	}
	if(!newline) {
		std::cout << "\"" << std::endl;
	}
	std::cout << ";" << std::endl;
}

int main(int argc, char *argv[])
{
	int		i;
	char		now[256], *eos;
	time_t		tnow;
	FILE		*f;

	g_progname = strrchr(argv[0], '/');
	if(g_progname == NULL) {
		g_progname = argv[0];
	}
	else {
		/* skip the / */
		++g_progname;
	}

	/* retrieve options */
	for(i = 1; i < argc; ++i) {
		if(argv[i][0] == '-') {
			switch(argv[i][1]) {
			case 'h':
				usage();
				break;

			default:
				fprintf(stderr, "error:%s: unknown option %s\n", g_progname, argv[i]);
				exit(1);

			}
		}
		else {
			filenames.push_back(argv[i]);
		}
	}

	/* header */
	std::cout << "/* AUTO-GENERATED FILE -- DO NOT EDIT */" << std::endl;
	tnow = time(NULL);
	ctime_r(&tnow, now);
	eos = strchr(now, '\r');
	if(eos) {
		*eos = '\0';
	}
	eos = strchr(now, '\n');
	if(eos) {
		*eos = '\0';
	}
	std::cout << "/* generated by " << g_progname << " on " << now << " */" << std::endl;
	std::cout << "namespace sswf {" << std::endl;
	std::cout << "namespace asc {" << std::endl;
	std::cout << "namespace details {" << std::endl;

/* private data */
	std::cout << "namespace {" << std::endl;

	/* first, save the .asc files */
	std::vector<char *>::iterator it(filenames.begin());
	for(i = 1; it != filenames.end(); ++i, ++it) {
		header_to_cpp(*it, i);
	}

	/* now create the database and save it too */
	// check that we are not about to destroy a user file
	f = fopen("sswf.rc", "r");
	if(f != NULL) {
		fclose(f);
		fprintf(stderr, "error:%s: an sswf.rc file exists in your current folder.\n", g_progname);
		exit(1);
	}
	// create a default sswf.rc file...
	f = fopen("sswf.rc", "w");
	fprintf(f, "version=" TO_STR(SSWF_VERSION) "\n");
	fprintf(f, "asc_path=include/sswf/scripts\n");
	fprintf(f, "asc_db=asc_packages.db\n");
	fclose(f);
	// creating the compiler is enough to create the database
	sswf::as::Compiler *compiler = sswf::as::Compiler::CreateCompiler(NULL);
	delete compiler;
	unlink("sswf.rc");
	/* read the database and save it in a file variable */
	header_to_cpp("asc_packages.db", i);
	unlink("asc_packages.db");

	std::cout << "}" << std::endl << std::endl;

/* the array used from outside to retrieve the data */
	std::cout << "extern const int files_count = " << i << ";" << std::endl;
	std::cout << "extern const char *files[] = {" << std::endl;
	it = filenames.begin();
	for(i = 1; it != filenames.end(); ++i, ++it) {
		std::cout << "\t\"" << *it << "\", file" << i << "," << std::endl;
	}
	// one more for the database
	std::cout << "\t\"asc_packages.db\", file" << i << std::endl;
	std::cout << "};" << std::endl << std::endl;

	/* closing */
	std::cout << "}" << std::endl; // details namespace
	std::cout << "}" << std::endl; // asc namespace
	std::cout << "}" << std::endl;	// sswf namespace

	return 0;
}


