xref: /haiku/src/tools/create_repository_config/create_repository_config.cpp (revision 7bdeef54a24d3417300f251af891df962b638b9b)
1 /*
2  * Copyright 2013-2016, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  * 		Alexander von Gluck IV <kallisti5@unixzen.com>
8  */
9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <package/RepositoryConfig.h>
16 #include <package/RepositoryInfo.h>
17 
18 
19 #define DIE(error, ...)								\
20 	do {											\
21 		fprintf(stderr, "Error: " __VA_ARGS__);		\
22 		fprintf(stderr, ": %s\n", strerror(error));	\
23 		exit(1);									\
24 	} while (false)
25 
26 #define DIE_ON_ERROR(error, ...)		\
27 	do {								\
28 		status_t _error = error;		\
29 		if (error != B_OK)				\
30 			DIE(_error, __VA_ARGS__);	\
31 	} while (false)
32 
33 
34 static const char* sProgramName = "create_repository_config";
35 
36 
37 void
38 print_usage_and_exit(bool error)
39 {
40 	fprintf(error ? stderr : stdout,
41 		"Usage: %s <URL> <repository info> <repository config>\n"
42 		"Creates a repository config file from a given repository info and\n"
43 		"the base URL (the directory in which the 'repo', 'repo.info',\n"
44 		"and 'repo.sha256' files can be found).\n",
45 		sProgramName);
46 	exit(error ? 1 : 0);
47 }
48 
49 
50 int
51 main(int argc, const char* const* argv)
52 {
53 	if (argc != 4) {
54 		if (argc == 2
55 			&& (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
56 				print_usage_and_exit(false);
57 		}
58 		print_usage_and_exit(true);
59 	}
60 
61 	int argi = 1;
62 	const char* url = argv[argi++];
63 	const char* infoPath = argv[argi++];
64 	const char* configPath = argv[argi++];
65 
66 	// read the info
67 	BPackageKit::BRepositoryInfo repoInfo;
68 	DIE_ON_ERROR(repoInfo.SetTo(infoPath),
69 		"failed to read repository info file \"%s\"", infoPath);
70 
71 	// init and write the config
72 	BPackageKit::BRepositoryConfig repoConfig;
73 	repoConfig.SetName(repoInfo.Name());
74 	repoConfig.SetBaseURL(url);
75 	repoConfig.SetPriority(repoInfo.Priority());
76 	DIE_ON_ERROR(repoConfig.Store(configPath),
77 		"failed to write repository config file \"%s\"", configPath);
78 
79 	return 0;
80 }
81