main.cpp 10.2 KB
Newer Older
1
/* This file is part of the Chakra project
2

3 4 5 6 7 8 9 10 11
   Copyright (C) 2011 Lukas Appelhans <l.appelhans@gmx.de>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
*/
#include <kcmdlineargs.h>
#include <klocalizedstring.h>
12
#include <qcoreapplication.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <kaboutdata.h>
#include <kdebug.h>
#include "apm.h"

int main(int argc, char** argv)
{
    KAboutData aboutData("akabei", 0, ki18n("Akabei"),
                         QByteArray("0.0.1"),
                         ki18n("Chakra's CLI package manager"),
                         KAboutData::License_GPL,
                         ki18n("(C) 2011 Lukas Appelhans"),
                         ki18n("<a href=\"mailto:boom1992@chakra-project.org\">boom1992@chakra-project.org</a>"));
 
    aboutData.addAuthor(ki18n("Lukas Appelhans"), ki18n("Maintainer"), "boom1992@chakra-project.org");
Lukas Appelhans's avatar
Lukas Appelhans committed
27
    
28 29 30
    KCmdLineArgs::init(argc, argv, &aboutData);

    KCmdLineOptions options;
Lukas Appelhans's avatar
Lukas Appelhans committed
31
   
Lukas Appelhans's avatar
Lukas Appelhans committed
32
    //options.add("r");
Lukas Appelhans's avatar
Lukas Appelhans committed
33 34 35 36
    options.add("root <path>", ki18n("Set a different root dir"));
    options.add("b");
    options.add("dbpath <path>", ki18n("Set a different database dir"));
    options.add("cachedir <path>", ki18n("Set a different cache dir"));
Lukas Appelhans's avatar
Lukas Appelhans committed
37 38 39
    
    options.add("D", ki18n("Database operation"));
    
40 41
    //FIXME: For example with owns we should use "owns <File>", this needs different parsing later as we need to use getOption
    //Doesn't work really with -Qo stuff then :/s
Lukas Appelhans's avatar
Lukas Appelhans committed
42
    options.add("Q", ki18n("Query operation"));
Lukas Appelhans's avatar
Lukas Appelhans committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    options.add("c");
    options.add("changelog", ki18n("Shows changelog of the package"));
    options.add("d");
    options.add("deps", ki18n("Shows packages which got installed as dependencies"));
    options.add("e");
    options.add("explicit", ki18n("Shows packages which got installed explicitely"));
    options.add("g");
    options.add("groups", ki18n("Shows all packages in a package group"));
    options.add("i");
    options.add("info", ki18n("Shows package information"));
    options.add("k");
    options.add("check", ki18n("Check if the files of the package are available"));
    options.add("m");
    options.add("foreign", ki18n("Shows packages which were not found in the Sync-db(s)"));
    options.add("o");
    options.add("owns", ki18n("Returns the package which contains the file"));
    options.add("p");
    options.add("file", ki18n("Queries a package file instead of the database"));
    options.add("s");
    options.add("search", ki18n("Searches locally installed packages"));
    options.add("t");
    options.add("unrequired", ki18n("Returns packages who are not required by others"));
    options.add("u");
    options.add("upgrades", ki18n("Returns upgradable packages"));
    options.add("q");
    options.add("quiet", ki18n("Shows less information in queries and searches"));
Lukas Appelhans's avatar
Lukas Appelhans committed
69 70
    
    options.add("R", ki18n("Remove operation"));
Lukas Appelhans's avatar
Lukas Appelhans committed
71 72 73 74 75 76
    options.add("c");
    options.add("cascade", ki18n("Removes all packages and those depending on them"));
    options.add("d");
    options.add("nodeps", ki18n("Skip dependency check"));
    options.add("k");
    options.add("dbonly", ki18n("Only remove database entries"));
Lukas Appelhans's avatar
Lukas Appelhans committed
77 78
    options.add("n", ki18n("Also remove configuration files"), false);
    options.add("nosave", ki18n("Also remove configuration files"), false);
Lukas Appelhans's avatar
Lukas Appelhans committed
79 80 81 82
    options.add("s");
    options.add("recursive", ki18n("Remove dependencies as well. (-ss includes explicitely installed dependencies"));
    options.add("u");
    options.add("unneeded", ki18n("Removes not used packages"));
Lukas Appelhans's avatar
Lukas Appelhans committed
83
    
84
    options.add("S", ki18n("Sync operation"));
85 86
    options.add("asdeps", ki18n("Install all packages as non-explicit"));
    options.add("asexplicit", ki18n("Install all packages as explicit"));
Lukas Appelhans's avatar
Lukas Appelhans committed
87 88
    options.add("c");
    options.add("clean", ki18n("Remove old packages from cache"));
89
    options.add("cc", ki18n("Remove all packages from cache"));
Lukas Appelhans's avatar
Lukas Appelhans committed
90 91
    options.add("d");
    options.add("nodeps", ki18n("Skip dependency check"));
Lukas Appelhans's avatar
Lukas Appelhans committed
92 93 94 95 96 97 98 99 100 101 102 103
    options.add("f");
    options.add("force", ki18n("Force installation. Overwrite file conflicts"));
    options.add("g");
    options.add("groups", ki18n("Show all packages in a group"));
    options.add("i");
    options.add("info", ki18n("Show package information"));
    options.add("l");
    options.add("list", ki18n("List all packages of a specific repository"));
    options.add("s");
    options.add("search", ki18n("Searches repositories for a word"));
    options.add("w");
    options.add("downloadonly", ki18n("Downloads packages without installing or upgrading them"));
104 105 106
    options.add("needed", ki18n("Up-to-date packages are not reinstalled"));
    options.add("ignore <package>", ki18n("Ignores a certain package. Can be used more than once."));
    options.add("ignoregroup <group>", ki18n("Ignores upgrade of a group"));
Lukas Appelhans's avatar
Lukas Appelhans committed
107 108 109 110
    options.add("y");
    options.add("refresh", ki18n("Refresh database"));
    options.add("u");
    options.add("sysupgrade", ki18n("Update system"));
Lukas Appelhans's avatar
Lukas Appelhans committed
111 112
    
    options.add("U", ki18n("Upgrade operation"));
Lukas Appelhans's avatar
Lukas Appelhans committed
113 114
    
    options.add("+[arg1]", ki18n("An optional argument 'arg1'"));
115 116 117 118

    KCmdLineArgs::addCmdLineOptions(options);

    // Add options from other components
119
    //KUniqueApplication::addCmdLineOptions();
120
    
121 122 123 124
    //if (!KUniqueApplication::start()) {
    //   fprintf(stderr, "Akabei is already running!\n");
    //   return 0;
    //}
125
    
126
    QCoreApplication app(argc, argv);
127 128 129 130 131

    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    
    QList<APM::Operation> list;
    APM::OperationType type = APM::NoType;
132
    QMultiHash<APM::Operation, QString> opts;
133
    
134
    ///NOTE: The actual order the arguments get checked is later transferred into when they are going to be done
Lukas Appelhans's avatar
Lukas Appelhans committed
135
    if (args->isSet("D")) {
Lukas Appelhans's avatar
Lukas Appelhans committed
136
        type = APM::DatabaseOperationType;
Lukas Appelhans's avatar
Lukas Appelhans committed
137
    } else if (args->isSet("Q")) {
Lukas Appelhans's avatar
Lukas Appelhans committed
138
        type = APM::QueryOperationType;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        if (args->isSet("c"))
            list.append(APM::ShowChangelog);
        if (args->isSet("d"))
            list.append(APM::ShowInstalledAsDeps);
        if (args->isSet("e"))
            list.append(APM::ShowInstalledExplicitely);
        if (args->isSet("g"))
            list.append(APM::ShowPackagesOfGroup);
        if (args->isSet("i"))
            list.append(APM::ShowInformation);
        if (args->isSet("k"))
            list.append(APM::CheckFiles);
        if (args->isSet("m"))
            list.append(APM::ShowLocal);
        if (args->isSet("o"))
            list.append(APM::ShowOwner);
        if (args->isSet("p"))
            list.append(APM::QueryPackageFile);
        if (args->isSet("s"))
            list.append(APM::SearchLocal);
        if (args->isSet("t"))
            list.append(APM::ShowNotRequired);
        if (args->isSet("u"))
            list.append(APM::ShowUpgradeable);
        if (args->isSet("q"))
            list.append(APM::ShowLess);
Lukas Appelhans's avatar
Lukas Appelhans committed
165
    } else if (args->isSet("R")) {
Lukas Appelhans's avatar
Lukas Appelhans committed
166
        type = APM::RemoveOperationType;
Lisa's avatar
Lisa committed
167 168
        list.append(APM::Remove);
        
169 170 171 172 173 174 175 176 177 178 179 180
        if (args->isSet("c"))
            list.append(APM::Cascade);
        if (args->isSet("d"))
            list.append(APM::SkipDependencyCheck);
        if (args->isSet("k"))
            list.append(APM::DatabaseOnly);
        if (args->isSet("n"))
            list.append(APM::RemoveConfig);
        if (args->isSet("s"))
            list.append(APM::Recursive);
        if (args->isSet("u"))
            list.append(APM::Unneeded);
Lukas Appelhans's avatar
Lukas Appelhans committed
181
    } else if (args->isSet("S")) {
Lukas Appelhans's avatar
Lukas Appelhans committed
182
        type = APM::SyncOperationType;
Lisa's avatar
Lisa committed
183 184
        list.append(APM::Install);
        
185 186 187 188
        if (args->isSet("y"))
            list.append(APM::UpdateDatabases);
        if (args->isSet("u"))
            list.append(APM::UpdateSystem);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        if (args->isSet("asdeps"))
            list.append(APM::AsDeps);
        if (args->isSet("asexplicit"))
            list.append(APM::AsExplicit);
        if (args->isSet("c"))
            list.append(APM::RemoveOldPackages);
        if (args->isSet("cc"))
            list.append(APM::RemoveAllPackages);
        if (args->isSet("d"))
            list.append(APM::SkipDependencyCheck);
        if (args->isSet("f"))
            list.append(APM::Force);
        if (args->isSet("g"))
            list.append(APM::ShowPackagesOfGroup);
        if (args->isSet("i"))
            list.append(APM::ShowInformation);
        if (args->isSet("l"))
            list.append(APM::ListRepo);
        if (args->isSet("s"))
            list.append(APM::Search);
        if (args->isSet("w"))
            list.append(APM::DownloadOnly);
        if (args->isSet("needed"))
            list.append(APM::OnlyNeeded);
        QStringList ignore = args->getOptionList("ignore");
        if (!ignore.isEmpty()) {
            foreach (const QString &ign, ignore)
                opts.insert(APM::Ignore, ign);
        }
        QStringList ignoreGroup = args->getOptionList("ignoregroup");
        if (!ignoreGroup.isEmpty()) {
            foreach (const QString &ign, ignoreGroup)
                opts.insert(APM::IgnoreGroup, ign);
        }
Lukas Appelhans's avatar
Lukas Appelhans committed
223
    } else if (args->isSet("U")) {
Lukas Appelhans's avatar
Lukas Appelhans committed
224
        type = APM::UpgradeOperationType;
Lukas Appelhans's avatar
Lukas Appelhans committed
225 226 227 228 229 230 231 232 233 234
        if (args->isSet("asdeps"))
            list.append(APM::AsDeps);
        if (args->isSet("asexplicit"))
            list.append(APM::AsExplicit);
        if (args->isSet("d"))
            list.append(APM::SkipDependencyCheck);
        if (args->isSet("f"))
            list.append(APM::Force);
        if (args->isSet("w"))
            list.append(APM::DownloadOnly);
Lukas Appelhans's avatar
Lukas Appelhans committed
235
    } else {
Lisa's avatar
Lisa committed
236 237 238
        QTextStream err(stderr);
        err << "Please select an operation to do!\nCall akabei --help for explanation.\n";
        return -1;
239
    }
Lukas Appelhans's avatar
Lukas Appelhans committed
240 241
    QString root = args->getOption("root");
    opts.insert(APM::RootDir, root);
Lukas Appelhans's avatar
Lukas Appelhans committed
242 243
    
    QString dbs = args->getOption("b");
Lukas Appelhans's avatar
Lukas Appelhans committed
244
    opts.insert(APM::DBPath, dbs);
Lukas Appelhans's avatar
Lukas Appelhans committed
245 246
    
    QString cache = args->getOption("cachedir");
Lukas Appelhans's avatar
Lukas Appelhans committed
247
    opts.insert(APM::CacheDir, cache);
248
    
Lisa's avatar
Lisa committed
249 250 251 252 253 254 255 256 257
    /*
     * APM::Remove or APM::Install must remain only when they're alone
     * otherwise another operation has been requested
     */
    if (list.size() > 1) {
        list.removeOne(APM::Remove);
        list.removeOne(APM::Install);
    }
    
258 259
    QStringList rest;

Lukas Appelhans's avatar
Lukas Appelhans committed
260
    for (int i = 0; i < args->count(); i++) {
261 262
       rest << args->arg(i);
    }
263

264 265
    args->clear(); // Free up some memory
    
266
    APM apm(type, list, opts, rest);
267
    return app.exec();
268
}