summaryrefslogtreecommitdiff
path: root/include/version.cpp
blob: 57c3d95fe92e2a3e8c35f4dcd3bad5c9cfde0cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* Build bumper
 *
 * (C) 2003-2011 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>

int main(int argc, char *argv[])
{
	if (argc < 3)
	{
		std::cout << "Syntax: " << argv[0] << " <src/version.sh> <version.h>" << std::endl;
		return 1;
	}

	std::fstream fd;

	fd.clear();
	fd.open(argv[1], std::ios::in);
	if (!fd.is_open())
	{
		std::cout << "Error: Unable to open src/version.sh for reading: " << argv[1] << std::endl;
		return 1;
	}

	std::string filebuf;
	std::list<std::pair<std::string, std::string> > versions;
	while (getline(fd, filebuf))
	{
		if (!filebuf.find("VERSION_"))
		{
			size_t eq = filebuf.find('=');

			std::string type = filebuf.substr(8, 5);
			std::string value = filebuf.substr(eq + 2, filebuf.length() - eq - 3);
			versions.push_back(std::make_pair(type, value));
		}
	}

	fd.close();

	fd.clear();
	fd.open(argv[2], std::ios::in);

	std::string version_build = "#define VERSION_BUILD	1";
	std::string build = "#define BUILD	1";
	std::string version_extra;
	if (fd.is_open())
	{
		while (getline(fd, filebuf))
		{
			if (!filebuf.find("#define VERSION_BUILD"))
				version_build = filebuf;
			else if (!filebuf.find("#define VERSION_EXTRA"))
			{
				size_t q = filebuf.find('"');

				version_extra = filebuf.substr(q + 1, filebuf.length() - q - 2);
			}
			else if (!filebuf.find("#define BUILD"))
			{
				size_t tab = filebuf.find('	');

				int ibuild = atoi(filebuf.substr(tab + 1).c_str()) + 1;

				std::stringstream ss;
				ss << "#define BUILD	" << ibuild;
				build = ss.str();
			}
		}

		fd.close();
	}

	fd.clear();
	fd.open(argv[2], std::ios::out);

	if (!fd.is_open())
	{
		std::cout << "Error: Unable to include/version.h for writing: " << argv[2] << std::endl;
		return 1;
	}

	fd << "/* This file is automatically generated by version.cpp - do not edit it! */" << std::endl;

	for (std::list<std::pair<std::string, std::string> >::iterator it = versions.begin(), it_end = versions.end(); it != it_end; ++it)
	{
		if (it->first == "EXTRA")
			fd << "#define VERSION_EXTRA \"" << (!version_extra.empty() ? version_extra : "") << (version_extra.find(it->second) == std::string::npos ? it->second  : "") << "\"" << std::endl;
		else
			fd << "#define VERSION_" << it->first << " " << it->second << std::endl;
	}

	fd << version_build << std::endl;
	fd << build << std::endl;

	fd.close();

	return 0;
}