1/*
2 * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <Application.h>
8#include <Mime.h>
9
10#include <stdlib.h>
11#include <stdio.h>
12#include <string.h>
13
14
15#ifdef HAIKU_HOST_PLATFORM_SUNOS
16static const char *sProgramName = "mimeset";
17#else
18extern const char *__progname;
19static const char *sProgramName = __progname;
20#endif
21
22// options
23bool gFiles = true;
24bool gApps = false;
25int gForce = 0; // B_UPDATE_MIME_INFO_NO_FORCE;
26
27
28void
29usage(int status)
30{
31	printf("usage: %s [OPTION]... [PATH]...\n"
32		"  -all\t\tcombine default action and the -apps option\n"
33		"  -apps\t\tupdate 'app' and 'meta_mime' information\n"
34		"  -f\t\tforce updating, even if previously updated\n"
35		"  	\t  (will not overwrite the 'type' of a file)\n"
36		"  -F\t\tforce updating, even if previously updated\n"
37		"  	\t  (will overwrite the 'type' of a file)\n"
38		"  --help\tdisplay this help information\n"
39		"When PATH is @, file names are read from stdin\n\n",
40		sProgramName);
41
42	exit(status);
43}
44
45
46status_t
47process_file(const char *path)
48{
49	status_t status = B_OK;
50
51	BEntry entry(path);
52	if (!entry.Exists())
53		status = B_ENTRY_NOT_FOUND;
54
55	if (gFiles && status >= B_OK)
56		status = update_mime_info(path, true, true, gForce);
57	if (gApps && status >= B_OK)
58		status = create_app_meta_mime(path, true, true, gForce);
59
60	if (status < B_OK) {
61		fprintf(stderr, "%s: \"%s\": %s\n",
62			sProgramName, path, strerror(status));
63	}
64	return status;
65}
66
67
68int
69main(int argc, char **argv)
70{
71	// parse arguments
72
73	if (argc < 2)
74		usage(1);
75
76	while (*++argv) {
77		char *arg = *argv;
78		if (*arg != '-')
79			break;
80
81		if (!strcmp(arg, "-all"))
82			gApps = true;
83		else if (!strcmp(arg, "-apps")) {
84			gApps = true;
85			gFiles = false;
86		} else if (!strcmp(arg, "-f"))
87			gForce = 1; // B_UPDATE_MIME_INFO_FORCE_KEEP_TYPE;
88		else if (!strcmp(arg, "-F"))
89			gForce = 2; // B_UPDATE_MIME_INFO_FORCE_UPDATE_ALL;
90		else if (!strcmp(arg, "--help"))
91			usage(0);
92		else {
93			fprintf(stderr, "unknown  option \"%s\"\n", arg);
94			usage(1);
95		}
96	}
97
98	// process files
99
100	BApplication app("application/x-vnd.haiku.mimeset");
101
102	while (*argv) {
103		char *arg = *argv++;
104
105		if (!strcmp(arg, "@")) {
106			// read file names from stdin
107			char name[B_PATH_NAME_LENGTH];
108			while (fgets(name, sizeof(name), stdin) != NULL) {
109				name[strlen(name) - 1] = '\0';
110					// remove trailing '\n'
111				if (process_file(name) != B_OK)
112					exit(1);
113			}
114		} else {
115			if (process_file(arg) != B_OK)
116				exit(1);
117		}
118	}
119
120	return 0;
121}
122
123