/*
 * Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
 *
 * Author:     Iceyer <me@iceyer.net>
 *
 * Maintainer: Iceyer <me@iceyer.net>
 *
 * 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 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QCoreApplication>
#include <DLog>

#include "licensehelper.h"

using namespace Dtk::Core;

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    app.setOrganizationName("deepin");
    app.setApplicationName("deepin-activator-cli");
    DLogManager::registerFileAppender();
//    DLogManager::registerConsoleAppender();

    QCommandLineParser parser;
    parser.setApplicationDescription("deepin activator cli");
    parser.addHelpOption();
    parser.addVersionOption();

    QCommandLineOption infoOption(QStringList() << "s" << "state");
    QCommandLineOption exportOption(QStringList() << "e" << "export",
                                    QCoreApplication::translate("main", "Export license file"),
                                    QCoreApplication::translate("main", "info file"));
    QCommandLineOption importOption(QStringList() << "i" << "import",
                                    QCoreApplication::translate("main", "Import license file"),
                                    QCoreApplication::translate("main", "license file"));
    parser.addOption(importOption);
    parser.addOption(exportOption);
    parser.addOption(infoOption);
    parser.process(app);

    LicenseHelper lc;

    if (parser.isSet(infoOption)) {
        if (Authorized != lc.licenseState()) {
            printf("\n");
            printf("Your system is not activated. Please activate as soon as possible for normal use.");
            printf("\n");
        }
        exit(0);
    }

    if (parser.isSet(exportOption)) {
        QString exportPath = parser.value(exportOption);
        if (exportPath.isEmpty()) {
            exportPath = lc.systemMachinecode() + ".info";
        }
        lc.exportInformation(exportPath);
        printf("You can get the computer information file at %s", exportPath.toStdString().c_str());
        printf("\n");
        exit(0);
    }

    if (!parser.value(importOption).isEmpty()) {
        if ((0 != lc.importLicenseFile(parser.value(importOption))) ||
                (Authorized != lc.licenseState())) {
            printf("Failed to activate, your activation file is invalid.");
            printf("\n");
            printf("Please contact your sales representative or support specialist.");
            printf("\n");
            exit(0);
        };
        exit(0);
    }

    parser.showHelp();
}

