xref: /haiku/src/bin/package/package.cpp (revision 6707be54030e82f4e1a3ad38cdce7d1522f61cb1)
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 
17 extern const char* __progname;
18 const char* kCommandName = __progname;
19 
20 
21 static 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 	"  create [ <options> ] <package>\n"
27 	"    Creates package file <package> from contents of current directory.\n"
28 	"\n"
29 	"    -C <dir>   - Change to directory <dir> before starting.\n"
30 	"    -i <info>  - Use the package info file <info>. It will be added as\n"
31 	"                 \".PackageInfo\", overriding a \".PackageInfo\" file,\n"
32 	"                 existing.\n"
33 	"    -q         - Be quiet (don't show any output except for errors).\n"
34 	"    -v         - Be verbose (show more info about created package).\n"
35 	"\n"
36 	"  dump [ <options> ] <package>\n"
37 	"    Dumps the TOC section of package file <package>. For debugging only.\n"
38 	"\n"
39 	"  extract [ <options> ] <package>\n"
40 	"    Extracts the contents of package file <package>.\n"
41 	"\n"
42 	"    -C <dir>   - Change to directory <dir> before extracting the "
43 		"contents\n"
44 	"                  of the archive.\n"
45 	"\n"
46 	"  list [ <options> ] <package>\n"
47 	"    Lists the contents of package file <package>.\n"
48 	"\n"
49 	"    -a         - Also list the file attributes.\n"
50 	"\n"
51 	"Common Options:\n"
52 	"  -h, --help   - Print this usage info.\n"
53 ;
54 
55 
56 void
57 print_usage_and_exit(bool error)
58 {
59     fprintf(error ? stderr : stdout, kUsage, kCommandName);
60     exit(error ? 1 : 0);
61 }
62 
63 
64 int
65 main(int argc, const char* const* argv)
66 {
67 	if (argc < 2)
68 		print_usage_and_exit(true);
69 
70 	const char* command = argv[1];
71 	if (strcmp(command, "create") == 0)
72 		return command_create(argc - 1, argv + 1);
73 
74 	if (strcmp(command, "dump") == 0)
75 		return command_dump(argc - 1, argv + 1);
76 
77 	if (strcmp(command, "extract") == 0)
78 		return command_extract(argc - 1, argv + 1);
79 
80 	if (strcmp(command, "list") == 0)
81 		return command_list(argc - 1, argv + 1);
82 
83 	if (strcmp(command, "help") == 0)
84 		print_usage_and_exit(false);
85 	else
86 		print_usage_and_exit(true);
87 
88 	// never gets here
89 	return 0;
90 }
91