/*
 * TreeRePair is an implementation of the Re-pair for Trees algorithm.
 *
 * Copyright (C) 2010  Roy Mennicke
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 3 as
 * published by the Free Software Foundation.
 *
 * 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 General Public License Version 3 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not,
 * see <http://www.gnu.org/licenses/gpl-3.0.txt>.
 */
#include "TreeDePairAlgorithm.h"

#include "TreeRePairVersion.h"
#include "Helper.h"

/**
 * prints the help screen to the standard output
 */
static void printHelp() {
	string blank = "  ";

	cout << endl;
	cout << "TreeRePair is an implementation of the Re-pair for Trees algorithm." << endl;
	cout << "This tool decompresses trees which were compressed by TreeRePair." << endl;
	cout << endl;
	cout << "Copyright (c) 2010 Roy Mennicke, Build date: " << VERSION << endl;
	cout << endl;
	cout << "This is free software.  You may redistribute copies of it under the terms of" << endl;
	cout << "the GNU General Public License Version 3 which is available at the webpage" << endl;
	cout << "<http://www.gnu.org/licenses/gpl-3.0.txt>. There is NO WARRANTY." << endl;
	cout << endl;
	cout << "Usage: TreeDePair [-v] [-p] input_file" << endl;
	cout << "Parameters:" << endl;
	cout << blank << "input_file" << endl;
	// TODO fix file name extension
	cout << blank << blank << "input_file must be a valid TreeRePair result file (with extension" << endl;
	cout << blank << blank << "\"rep\")." << endl;
	cout << blank << "-p" << endl;
	cout << blank << blank << "Shows progress during execution." << endl;
	cout << blank << "-v" << endl;
	cout << blank << blank << "Enables verbose mode which prints out various additional" << endl;
	cout << blank << blank << "informations." << endl;
}

int main (int argc, char* args[]) {

	if (argc == 1) {
		cout << "Please specify a valid input file, i.e., a file generated by " << endl;
		cout << "a run of TreeRePair (with file extension .rep)." << endl;
		printHelp();
		exit(1);
	}

	char * pFilePath = 0;
	for (int i = 1; i < argc; i++) {
		char * arg = args[i];
		// char * mode = 0;
		if (arg[0] == '-') {
			switch(arg[1]) {
				case 'v': // -v (verbose)
					TreeRePairMode::getInstance()->setVerboseMode(true);
					break;
				case 'p': // -p (progress)
					TreeRePairMode::getInstance()->setShowProgress(true);
					break;
				case 't': // -textual
					TreeRePairMode::getInstance()->setUseTextualGrammar(true);
					break;
				case '\0':
					cout << "Unknown switch." << endl;
				default:
					cout << "Unknown switch: " << arg << endl;

					printHelp();
					exit(0);
					break;
			}
		} else {
			pFilePath = arg;
		}
	}

	TreeRePairMode::getInstance()->setAlgMode(TreeRePairMode::DECOMPRESS);

	if (pFilePath == 0) {
		cout << "Please specify a valid input file, i.e., a file generated by " << endl;
		cout << "a run of TreeRePair (with file extension .rep)." << endl;
	} else if (!Helper::fileExists(pFilePath)) {
		cout << "The specified input file does not exist." << endl;
	} else {
		TreeDePairAlgorithm * pAlg = new TreeDePairAlgorithm(pFilePath);
		delete pAlg;
	}

	/*if (TreeRePairMode::getInstance()->isDebugMode()) {
		getchar();
	}*/

	return 0;
}
