/*
    PIP - Platform Independent Primitives
    Resources compiller
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "generator.h"
#include "parser.h"
#include "picli.h"
#include "piiostream.h"
#include "pitranslator.h"

using namespace PICoutManipulators;


void usage() {
	piCout << Bold << "PIP Resources Compiler";
	piCout << Cyan << "Version" << Bold << PIPVersion() << NewLine;
	piCout << Green << Bold << "Usage:" << Default << "\"pip_rc [-hl] [-n <name>] -i <in_file> -o <out_file>\"" << NewLine;
	piCout << Green << Bold << "Details:";
	piCout << "-h --help            " << Green << "- display this message and exit";
	piCout << "-n --name <name>     " << Green << "- name of initialize function, by default is <out_file> base name";
	piCout << "-i --input <in_file> " << Green << "- resources description file";
	piCout << "-o --out <out_file>  " << Green << "- output .cpp file";
	piCout << "-l --list            " << Green << "- print readed files from description and exit";
	piCout << "-s --silent          " << Green << "- no console output";
}


int main(int argc, char * argv[]) {
	PICLI cli(argc, argv);
	cli.addArgument("input", true);
	cli.addArgument("out", true);
	cli.addArgument("name", true);
	cli.addArgument("help");
	cli.addArgument("list");
	cli.addArgument("silent");

	if (cli.hasArgument("silent")) piDebug = false;
	if (cli.hasArgument("help") || !cli.hasArgument("input") || (!cli.hasArgument("out") && !cli.hasArgument("list"))) {
		usage();
		return 0;
	}

	PIVector<ParserSection> files = parse(cli.argumentValue("input"));
	if (files.isEmpty()) {
		piCerr << "Error: resources description file is empty"_tr("pip_rc");
		return 0;
	}
	if (cli.hasArgument("list")) {
		for (const auto & s: files) {
			for (const auto & e: s.files) {
				piCout << e.path;
			}
		}
		return 0;
	}

	PIString out_file  = cli.argumentValue("out");
	PIString init_name = cli.argumentValue("name");
	init_name          = initName(init_name.isEmpty() ? out_file : init_name);
	PIFile outf;
	if (!out_file.isEmpty()) {
		if (outf.open(out_file, PIIODevice::ReadWrite)) {
			outf.clear();
		} else {
			piCerr << "Error: can`t open output file \"%1\""_tr("pip_rc").arg(out_file);
			return 1;
		}
		PIIOTextStream ts(&outf);
		ts << "// Generated by \"PIP Resources Compiler\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n");
		ts << "// Execute command:\n";
		for (const auto & _a: cli.rawArguments())
			ts << "//   \"" << _a << "\"\n";
		ts << "\n";
		if (!generate(init_name, outf, files)) {
			piCerr << "Error: generate fail"_tr("pip_rc");
			return 1;
		}
	}

	return 0;
}
