1/*
2 * Copyright 2013-2014, 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 <getopt.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14#include "Command.h"
15#include "pkgman.h"
16#include "PackageManager.h"
17
18
19// TODO: internationalization!
20
21
22using namespace BPackageKit;
23using namespace BPackageKit::BPrivate;
24
25
26static const char* const kShortUsage =
27	"  %command%\n"
28	"    Synchronizes the installed packages with the repositories (even by downgrading).\n";
29
30static const char* const kLongUsage =
31	"Usage: %program% %command%\n"
32	"Synchronizes the installed packages with the repositories. The command\n"
33	"is similar to the \"update\" command, but more aggressive. It also\n"
34	"downgrades or removes packages, if necessary.\n"
35	"\n"
36	"Options:\n"
37	"  --debug <level>\n"
38	"    Print debug output. <level> should be between 0 (no debug output,\n"
39	"    the default) and 10 (most debug output).\n"
40	"  -H, --home\n"
41	"    Synchronizes the packages in the user's home directory. Default is\n"
42	"    to synchronize the packages in the system directory.\n"
43	"  -y\n"
44	"    Non-interactive mode. Automatically confirm changes, but fail when\n"
45	"    encountering problems.\n"
46	"\n";
47
48
49DEFINE_COMMAND(FullSyncCommand, "full-sync", kShortUsage, kLongUsage,
50	COMMAND_CATEGORY_PACKAGES)
51
52
53int
54FullSyncCommand::Execute(int argc, const char* const* argv)
55{
56	BPackageInstallationLocation location
57		= B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
58	bool interactive = true;
59
60	while (true) {
61		static struct option sLongOptions[] = {
62			{ "debug", required_argument, 0, OPTION_DEBUG },
63			{ "help", no_argument, 0, 'h' },
64			{ "home", no_argument, 0, 'H' },
65			{ 0, 0, 0, 0 }
66		};
67
68		opterr = 0; // don't print errors
69		int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
70		if (c == -1)
71			break;
72
73		if (fCommonOptions.HandleOption(c))
74			continue;
75
76		switch (c) {
77			case 'h':
78				PrintUsageAndExit(false);
79				break;
80
81			case 'H':
82				location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
83				break;
84
85			case 'y':
86				interactive = false;
87				break;
88
89			default:
90				PrintUsageAndExit(true);
91				break;
92		}
93	}
94
95	// no remaining arguments
96	if (optind < argc)
97		PrintUsageAndExit(true);
98
99	// perform the sync
100	PackageManager packageManager(location, interactive);
101	packageManager.SetDebugLevel(fCommonOptions.DebugLevel());
102	packageManager.FullSync();
103
104	return 0;
105}
106