#include <QtGlobal>
#include <QCoreApplication>
#include <QByteArray>

#include <QProcess>
#include <QCommandLineParser>
#include <QStandardPaths>
#include <QSettings>
#include <QDir>
#include <QDebug>

#include "message_handler.h"
#include "utils_internal.h"
#include "sync_command.h"

int main(int argc, char **argv) {

    QByteArray const value = qgetenv("LC_ALL");
    if (value.isEmpty() || value != "C") {
        qputenv("LC_ALL", "C");
    }

    openlog("ATB-UPDATE-SYNC", LOG_PERROR | LOG_PID | LOG_CONS, LOG_USER);

    QCoreApplication a(argc, argv);
    QCoreApplication::setApplicationName("ATBUpdateSync");
    QCoreApplication::setApplicationVersion(APP_VERSION);

    QDebug debug = qCritical();

    if (!messageHandlerInstalled()) { // change internal qt-QDebug-handling
        atbInstallMessageHandler(nullptr);
        //atbInstallMessageHandler(atbDebugOutput);
        setDebugLevel(LOG_NOTICE);
    }

    if (internal::customerRepoExists() == false) {
        qCritical().noquote() << internal::NO_CUSTOMER_REPOSITORY;
        return internal::NO_CUSTOMER_REPOSITORY_CODE;
    }

    QString const crd = internal::customerRepoDir();
    QString const etcInRepo = QDir::cleanPath(crd + QDir::separator() + "etc/");
    QString const optInRepo = QDir::cleanPath(crd + QDir::separator() + "opt/");

    if (!QDir(etcInRepo).exists()) {
        qCritical().noquote() << internal::NO_ETC_CUSTOMER_REPOSITORY;
        return internal::NO_ETC_CUSTOMER_REPOSITORY_CODE;
    }

    if (!QDir(optInRepo).exists()) {
        qCritical().noquote() << internal::NO_OPT_CUSTOMER_REPOSITORY;
        return internal::NO_OPT_CUSTOMER_REPOSITORY_CODE;
    }

#if 0
    error codes for rsync:
        https://stackoverflow.com/questions/20737204/comprehensive-list-of-rsync-error-codes

     0     Success
     1     Syntax or usage error
     2     Protocol incompatibility
     3     Errors selecting input/output files, dirs
     4     Requested action not supported: an attempt was made to manipulate 64-bit
           files on a platform that cannot support them; or an option was specified
           that is supported by the client and not by the server.
     5     Error starting client-server protocol
     6     Daemon unable to append to log-file
    10     Error in socket I/O
    11     Error in file I/O
    12     Error in rsync protocol data stream
    13     Errors with program diagnostics
    14     Error in IPC code
    20     Received SIGUSR1 or SIGINT
    21     Some error returned by waitpid()
    22     Error allocating core memory buffers
    23     Partial transfer due to error
    24     Partial transfer due to vanished source files
    25     The --max-delete limit stopped deletions
    30     Timeout in data send/receive
    35     Timeout waiting for daemon connection
#endif


    QStringList options({"-v", "--recursive", "--progress", "--checksum",
                         "--exclude=.*", "--include=*.bin", "--include=*.json",
                         "--include=*.ini"});
    int ret = SyncCommand().exec("rsync", options << etcInRepo << "/etc");

    if (ret == 0) {
       ret = SyncCommand().exec("rsync", options << optInRepo << "/opt");
    }
    return (ret > 0) ? -ret : ret;
}