16ef57ae2SIngo Weinhold /*
26ef57ae2SIngo Weinhold * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
36ef57ae2SIngo Weinhold * Distributed under the terms of the MIT License.
46ef57ae2SIngo Weinhold *
56ef57ae2SIngo Weinhold * Authors:
66ef57ae2SIngo Weinhold * Ingo Weinhold <ingo_weinhold@gmx.de>
76ef57ae2SIngo Weinhold */
86ef57ae2SIngo Weinhold
96ef57ae2SIngo Weinhold
106ef57ae2SIngo Weinhold #include <getopt.h>
116ef57ae2SIngo Weinhold #include <stdio.h>
126ef57ae2SIngo Weinhold #include <stdlib.h>
136ef57ae2SIngo Weinhold
146ef57ae2SIngo Weinhold #include "Command.h"
156ef57ae2SIngo Weinhold #include "pkgman.h"
166ef57ae2SIngo Weinhold #include "PackageManager.h"
176ef57ae2SIngo Weinhold
186ef57ae2SIngo Weinhold
196ef57ae2SIngo Weinhold // TODO: internationalization!
206ef57ae2SIngo Weinhold
216ef57ae2SIngo Weinhold
226ef57ae2SIngo Weinhold using namespace BPackageKit;
236ef57ae2SIngo Weinhold using namespace BPackageKit::BPrivate;
246ef57ae2SIngo Weinhold
256ef57ae2SIngo Weinhold
266ef57ae2SIngo Weinhold static const char* const kShortUsage =
276ef57ae2SIngo Weinhold " %command%\n"
28*e88ab494SAugustin Cavalier " Synchronizes the installed packages with the repositories (even by downgrading).\n";
296ef57ae2SIngo Weinhold
306ef57ae2SIngo Weinhold static const char* const kLongUsage =
316ef57ae2SIngo Weinhold "Usage: %program% %command%\n"
326ef57ae2SIngo Weinhold "Synchronizes the installed packages with the repositories. The command\n"
336ef57ae2SIngo Weinhold "is similar to the \"update\" command, but more aggressive. It also\n"
346ef57ae2SIngo Weinhold "downgrades or removes packages, if necessary.\n"
356ef57ae2SIngo Weinhold "\n"
366ef57ae2SIngo Weinhold "Options:\n"
37dd15db95SIngo Weinhold " --debug <level>\n"
38dd15db95SIngo Weinhold " Print debug output. <level> should be between 0 (no debug output,\n"
39dd15db95SIngo Weinhold " the default) and 10 (most debug output).\n"
406ef57ae2SIngo Weinhold " -H, --home\n"
416ef57ae2SIngo Weinhold " Synchronizes the packages in the user's home directory. Default is\n"
426ef57ae2SIngo Weinhold " to synchronize the packages in the system directory.\n"
436ef57ae2SIngo Weinhold " -y\n"
446ef57ae2SIngo Weinhold " Non-interactive mode. Automatically confirm changes, but fail when\n"
456ef57ae2SIngo Weinhold " encountering problems.\n"
466ef57ae2SIngo Weinhold "\n";
476ef57ae2SIngo Weinhold
486ef57ae2SIngo Weinhold
496ef57ae2SIngo Weinhold DEFINE_COMMAND(FullSyncCommand, "full-sync", kShortUsage, kLongUsage,
5052a61976SAndrew Lindesay COMMAND_CATEGORY_PACKAGES)
516ef57ae2SIngo Weinhold
526ef57ae2SIngo Weinhold
536ef57ae2SIngo Weinhold int
Execute(int argc,const char * const * argv)546ef57ae2SIngo Weinhold FullSyncCommand::Execute(int argc, const char* const* argv)
556ef57ae2SIngo Weinhold {
566ef57ae2SIngo Weinhold BPackageInstallationLocation location
576ef57ae2SIngo Weinhold = B_PACKAGE_INSTALLATION_LOCATION_SYSTEM;
586ef57ae2SIngo Weinhold bool interactive = true;
596ef57ae2SIngo Weinhold
606ef57ae2SIngo Weinhold while (true) {
616ef57ae2SIngo Weinhold static struct option sLongOptions[] = {
62dd15db95SIngo Weinhold { "debug", required_argument, 0, OPTION_DEBUG },
636ef57ae2SIngo Weinhold { "help", no_argument, 0, 'h' },
646ef57ae2SIngo Weinhold { "home", no_argument, 0, 'H' },
656ef57ae2SIngo Weinhold { 0, 0, 0, 0 }
666ef57ae2SIngo Weinhold };
676ef57ae2SIngo Weinhold
686ef57ae2SIngo Weinhold opterr = 0; // don't print errors
696ef57ae2SIngo Weinhold int c = getopt_long(argc, (char**)argv, "hHy", sLongOptions, NULL);
706ef57ae2SIngo Weinhold if (c == -1)
716ef57ae2SIngo Weinhold break;
726ef57ae2SIngo Weinhold
73dd15db95SIngo Weinhold if (fCommonOptions.HandleOption(c))
74dd15db95SIngo Weinhold continue;
75dd15db95SIngo Weinhold
766ef57ae2SIngo Weinhold switch (c) {
776ef57ae2SIngo Weinhold case 'h':
786ef57ae2SIngo Weinhold PrintUsageAndExit(false);
796ef57ae2SIngo Weinhold break;
806ef57ae2SIngo Weinhold
816ef57ae2SIngo Weinhold case 'H':
826ef57ae2SIngo Weinhold location = B_PACKAGE_INSTALLATION_LOCATION_HOME;
836ef57ae2SIngo Weinhold break;
846ef57ae2SIngo Weinhold
856ef57ae2SIngo Weinhold case 'y':
866ef57ae2SIngo Weinhold interactive = false;
876ef57ae2SIngo Weinhold break;
886ef57ae2SIngo Weinhold
896ef57ae2SIngo Weinhold default:
906ef57ae2SIngo Weinhold PrintUsageAndExit(true);
916ef57ae2SIngo Weinhold break;
926ef57ae2SIngo Weinhold }
936ef57ae2SIngo Weinhold }
946ef57ae2SIngo Weinhold
956ef57ae2SIngo Weinhold // no remaining arguments
966ef57ae2SIngo Weinhold if (optind < argc)
976ef57ae2SIngo Weinhold PrintUsageAndExit(true);
986ef57ae2SIngo Weinhold
996ef57ae2SIngo Weinhold // perform the sync
1006ef57ae2SIngo Weinhold PackageManager packageManager(location, interactive);
101dd15db95SIngo Weinhold packageManager.SetDebugLevel(fCommonOptions.DebugLevel());
1026ef57ae2SIngo Weinhold packageManager.FullSync();
1036ef57ae2SIngo Weinhold
1046ef57ae2SIngo Weinhold return 0;
1056ef57ae2SIngo Weinhold }
106