1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de>
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include "package.h"
9
10#include <errno.h>
11#include <getopt.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
16
17extern const char* __progname;
18const char* kCommandName = __progname;
19
20
21static const char* kUsage =
22	"Usage: %s <command> <command args>\n"
23	"Creates, inspects, or extracts a Haiku package.\n"
24	"\n"
25	"Commands:\n"
26	"  add [ <options> ] <package> <entries>...\n"
27	"    Adds the specified entries <entries> to package file <package>.\n"
28	"\n"
29	"    -0 ... -9  - Use compression level 0 ... 9. 0 means no, 9 best "
30		"compression.\n"
31	"                 Defaults to 9.\n"
32	"    -C <dir>   - Change to directory <dir> before adding entries.\n"
33	"    -f         - Force adding, replacing already existing entries. "
34		"Without\n"
35	"                 this option adding will fail when encountering a "
36		"pre-exiting\n"
37	"                 entry (directories will be merged, though).\n"
38	"    -i <info>  - Use the package info file <info>. It will be added as\n"
39	"                 \".PackageInfo\", overriding a \".PackageInfo\" file,\n"
40	"                 existing.\n"
41	"    -q         - Be quiet (don't show any output except for errors).\n"
42	"    -v         - Be verbose (show more info about created package).\n"
43	"\n"
44	"  checksum [ <options> ] [ <package> ]\n"
45	"    Computes the checksum of package file <package>. If <package> is "
46		"omitted\n"
47	"    or \"-\", the file is read from stdin. This is only supported, if the "
48		"package\n"
49	"    is uncompressed.\n"
50	"\n"
51	"    -q         - Be quiet (don't show any output except for errors).\n"
52	"    -v         - Be verbose (show more info about created package).\n"
53	"    -z         - Use Zstd compression.\n"
54	"\n"
55	"  create [ <options> ] <package>\n"
56	"    Creates package file <package> from contents of current directory.\n"
57	"\n"
58	"    -0 ... -9  - Use compression level 0 ... 9. 0 means no, 9 best "
59		"compression.\n"
60	"                 Defaults to 9.\n"
61	"    -b         - Create an empty build package. Only the .PackageInfo "
62		"will\n"
63	"                 be added.\n"
64	"    -C <dir>   - Change to directory <dir> before adding entries.\n"
65	"    -i <info>  - Use the package info file <info>. It will be added as\n"
66	"                 \".PackageInfo\", overriding a \".PackageInfo\" file,\n"
67	"                 existing.\n"
68	"    -I <path>  - Set the package's installation path to <path>. This is\n"
69	"                 an option only for use in package building. It will "
70		"cause\n"
71	"                 the package .self link to point to <path>, which is "
72		"useful\n"
73	"                 to redirect a \"make install\". Only allowed with -b.\n"
74	"    -q         - Be quiet (don't show any output except for errors).\n"
75	"    -v         - Be verbose (show more info about created package).\n"
76	"\n"
77	"  dump [ <options> ] <package>\n"
78	"    Dumps the TOC section of package file <package>. For debugging only.\n"
79	"\n"
80	"  extract [ <options> ] <package> [ <entries>... ]\n"
81	"    Extracts the contents of package file <package>. If <entries> are\n"
82	"    specified, only those entries are extracted (directories "
83		"recursively).\n"
84	"\n"
85	"    -C <dir>   - Change to directory <dir> before extracting the "
86		"contents\n"
87	"                  of the archive.\n"
88	"    -i <info>  - Extract the .PackageInfo file to <info> instead.\n"
89	"\n"
90	"  info [ <options> ] <package>\n"
91	"    Prints individual meta information of package file <package>.\n"
92	"\n"
93	"    -f <format>, --format <format>\n"
94	"               - Print the given format string, performing the following\n"
95	"                 replacements:\n"
96	"                    %%fileName%%     - the package's canonical file name\n"
97	"                    %%name%%         - the package name\n"
98	"                    %%version%%      - the package version\n"
99	"                    %%%%             - %%\n"
100	"                    \\n             - new line\n"
101	"                    \\t             - tab\n"
102	"\n"
103	"  list [ <options> ] <package>\n"
104	"    Lists the contents of package file <package>.\n"
105	"\n"
106	"    -a         - Also list the file attributes.\n"
107	"    -i         - Only print the meta information, not the files.\n"
108	"    -p         - Only print a list of file paths.\n"
109	"\n"
110	"  recompress [ <options> ] <input package> <output package>\n"
111	"    Reads the package file <input package> and writes it to new package\n"
112	"    <output package> using the specified compression options. If the\n"
113	"    compression level 0 is specified (i.e. no compression), "
114		"<output package>\n"
115	"    can be \"-\", in which case the data are written to stdout.\n"
116	"    If the input files doesn't use compression <input package>\n"
117	"    can be \"-\", in which case the data are read from stdin.\n"
118	"\n"
119	"    -0 ... -9  - Use compression level 0 ... 9. 0 means no, 9 best "
120		"compression.\n"
121	"                 Defaults to 9.\n"
122	"    -q         - Be quiet (don't show any output except for errors).\n"
123	"    -v         - Be verbose (show more info about created package).\n"
124	"    -z         - Use Zstd compression.\n"
125	"\n"
126	"Common Options:\n"
127	"  -h, --help   - Print this usage info.\n"
128;
129
130
131void
132print_usage_and_exit(bool error)
133{
134    fprintf(error ? stderr : stdout, kUsage, kCommandName);
135    exit(error ? 1 : 0);
136}
137
138
139int
140main(int argc, const char* const* argv)
141{
142	if (argc < 2)
143		print_usage_and_exit(true);
144
145	const char* command = argv[1];
146	if (strcmp(command, "add") == 0)
147		return command_add(argc - 1, argv + 1);
148
149	if (strcmp(command, "checksum") == 0)
150		return command_checksum(argc - 1, argv + 1);
151
152	if (strcmp(command, "create") == 0)
153		return command_create(argc - 1, argv + 1);
154
155	if (strcmp(command, "dump") == 0)
156		return command_dump(argc - 1, argv + 1);
157
158	if (strcmp(command, "extract") == 0)
159		return command_extract(argc - 1, argv + 1);
160
161	if (strcmp(command, "list") == 0)
162		return command_list(argc - 1, argv + 1);
163
164	if (strcmp(command, "info") == 0)
165		return command_info(argc - 1, argv + 1);
166
167	if (strcmp(command, "recompress") == 0)
168		return command_recompress(argc - 1, argv + 1);
169
170	if (strcmp(command, "help") == 0)
171		print_usage_and_exit(false);
172	else
173		print_usage_and_exit(true);
174
175	// never gets here
176	return 0;
177}
178