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