/*
    open source routing machine
    Copyright (C) Dennis Luxen, others 2010

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
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 General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
 */

#include "UUID.h"

#cmakedefine01 HAS64BITS
#cmakedefine MD5PREPARE "${MD5PREPARE}"
#cmakedefine MD5RTREE "${MD5RTREE}"
#cmakedefine MD5NODEINFO "${MD5NODEINFO}"
#cmakedefine MD5GRAPH "${MD5GRAPH}"
#cmakedefine MD5OBJECTS "${MD5OBJECTS}"

UUID::UUID() : magic_number(1297240911) {
    md5_prepare[32] = md5_tree[32] = md5_nodeinfo[32] = md5_graph[32] =
        md5_objects[32] = '\0';

 	boost::uuids::name_generator gen(named_uuid);
 	std::string temp_string(__DATE__);
 	temp_string += __TIME__;

 	std::copy(MD5PREPARE, MD5PREPARE+strlen(MD5PREPARE), md5_prepare);
 	temp_string += md5_prepare;
 	std::copy(MD5RTREE, MD5RTREE+32, md5_tree);
 	temp_string += md5_tree;
 	std::copy(MD5NODEINFO, MD5NODEINFO+32, md5_nodeinfo);
 	temp_string += md5_nodeinfo;
 	std::copy(MD5GRAPH, MD5GRAPH+32, md5_graph);
 	temp_string += md5_graph;
 	std::copy(MD5OBJECTS, MD5OBJECTS+32, md5_objects);
 	temp_string += md5_objects;

	named_uuid = gen(temp_string);
	has_64_bits = HAS64BITS;
}

UUID::~UUID() {

}

const boost::uuids::uuid & UUID::GetUUID() const {
	return named_uuid;
}

bool UUID::IsMagicNumberOK() const {
	return 1297240911 == magic_number;
}

bool UUID::TestGraphUtil(const UUID & other) const {
    if(!other.IsMagicNumberOK()) {
        throw OSRMException("hsgr input file misses magic number. Check or reprocess the file");
    }
	return std::equal(md5_graph, md5_graph+32, other.md5_graph);
}

bool UUID::TestPrepare(const UUID & other) const {
    if(!other.IsMagicNumberOK()) {
        throw OSRMException("extracted input file misses magic number. Check or reprocess the file");
    }
	return std::equal(md5_prepare, md5_prepare+32, other.md5_prepare);
}

bool UUID::TestRTree(const UUID & other) const {
    if(!other.IsMagicNumberOK()) {
        throw OSRMException("r-tree input file misses magic number. Check or reprocess the file");
    }
	return std::equal(md5_tree, md5_tree+32, other.md5_tree);
}

bool UUID::TestNodeInfo(const UUID & other) const {
    if(!other.IsMagicNumberOK()) {
        throw OSRMException("nodes file misses magic number. Check or reprocess the file");
    }
	return std::equal(md5_nodeinfo, md5_nodeinfo+32, other.md5_nodeinfo);
}

bool UUID::TestQueryObjects(const UUID & other) const {
    if(!other.IsMagicNumberOK()) {
        throw OSRMException("missing magic number. Check or reprocess the file");
    }
	return std::equal(md5_objects, md5_objects+32, other.md5_objects);
}