#include "update.h"
#include "worker.h"
#include "utils.h"
#include "update_dc_event.h"
#include "mainwindow.h"

#include <QCoreApplication>
#include <QApplication>
#include <QFile>
#include <QTemporaryFile>
#include <QDebug>
#include <QTextStream>
#include <QRegularExpression>
#include <QRegExp>
#include <QApplication>

#if defined (Q_OS_UNIX) || defined (Q_OS_LINUX)
#include "unistd.h"
#endif

#include "plugins/interfaces.h"

#include <QSharedMemory>
#include <QScopedPointer>
#include <QDir>
#include <QThread>
#include <QDateTime>
#include <QPluginLoader>
#include <QMap>

#define UPDATE_OPKG                 (1)
#define UPDATE_DC                   (1)
#define UPDATE_PRINTER_TEMPLATES    (1)
#define UPDATE_CASH_TEMPLATE        (1)
#define UPDATE_CONF_TEMPLATE        (1)
#define UPDATE_DEVICE_TEMPLATE      (1)

static const QMap<QString, int> baudrateMap = {
  {"1200"   ,   0}, {"9600"   ,   1}, {"19200"  ,   2}, {"38400"  ,   3},
  {"57600"  ,   4}, {"115200" ,   5}
};

QPluginLoader Update::pluginLoader;

hwinf *Update::loadDCPlugin(QDir const &plugInDir, QString const &fname) {
    hwinf *hw = nullptr;
    if (plugInDir.exists()) {
        QString pluginLibName(fname);
        pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
        QFileInfo info(pluginLibName);
        if (info.exists()) {
            pluginLibName = plugInDir.absoluteFilePath(pluginLibName);
            pluginLoader.setFileName(pluginLibName);
            // static QPluginLoader pluginLoader(pluginLibName);
            if (!pluginLoader.load()) {
                qCritical() << "in directory" << plugInDir.absolutePath();
                qCritical() << "cannot load plugin" << pluginLoader.fileName();
                qCritical() << pluginLoader.errorString();
                exit(-1);
            }
            if (!pluginLoader.isLoaded()) {
                qCritical() << pluginLoader.errorString();
                exit(-2);
            }
            QObject *plugin = pluginLoader.instance();
            if (!plugin) {
                qCritical() << "cannot start instance";
                exit(-3);
            }
            if (! (hw = qobject_cast<hwinf *>(plugin))) {
                qCritical() << "cannot cast plugin" << plugin << "to hwinf";
                exit(-4);
            }
        } else {
            qCritical() << pluginLibName << "does not exist";
            exit(-5);
        }
    } else {
        qCritical() << "plugins directory" << plugInDir.absolutePath()
                    << "does not exist";
        exit(-6);
    }
    return hw;
}

bool Update::unloadDCPlugin() {
    if (pluginLoader.unload()) {
        qCritical() << "unloaded plugin" << pluginLoader.fileName();
        // Note: will re-instantiate the library !
        // QObject *rootObject = pluginLoader.instance();
        // if (rootObject) {
        //    qCritical() << "reloaded plugin: root object again available";
        //    return false;
        // }
        // qCritical()unloaded plugin: root object gone";
        return true;
    }
    return false;
}

Update::Update(hwinf *hw,
               Worker *worker,
               QString customerRepository,
               QString customerNrStr,
               QString branchName,
               QString pluginName,
               QString workingDir,
               bool dryRun,
               QObject *parent,
               char const *serialInterface,
               char const *baudrate)
    : QObject(parent)
    , m_hw(hw)
    , m_worker(worker)
    , m_serialInterface(serialInterface)
    , m_baudrate(baudrate)
    , m_customerRepository(customerRepository)
    , m_customerNrStr(customerNrStr)
    , m_branchName(branchName)
    , m_pluginName(pluginName)
    , m_workingDir(workingDir)
    , m_dryRun(dryRun) {

    qInfo() << "UPDATE: m_serialInterface    ..." << m_serialInterface;
    qInfo() << "UPDATE: m_baudrate           ..." << m_baudrate;
    qInfo() << "UPDATE: m_customerRepository ..." << m_customerRepository;
    qInfo() << "UPDATE: m_customerNr ..........." << m_customerNrStr;
    qInfo() << "UPDATE: m_branchName ..........." << m_branchName;
    qInfo() << "UPDATE: m_pluginName ..........." << m_pluginName;
    qInfo() << "UPDATE: m_workingDirectory ....." << m_workingDir;
}

Update::~Update() {
}

Update::DownloadResult Update::sendStatus(int ret) const {
    switch (ret) {                      // return values of dc are:
    case 0:                             // 0: no answer by now
        return DownloadResult::NOP;     // 1: error
    case 10:                            // 10: success
        return DownloadResult::OK;
    default:;
    }
    return DownloadResult::ERROR;
}

Update::DownloadResult Update::sendNextAddress(int bNum) const {
    // sends address only if blockNumber is one of 0, 1024, 2048, 3072, 4096
    int noAnswerCount = 0;
    int errorCount = 0;
    if ( bNum==0 || bNum==1024 || bNum==2048 || bNum==3072 || bNum==4096 ) {
        // qDebug() << "addr-block" << bNum << "...";
        while (noAnswerCount <= 250) {
            m_hw->bl_sendAddress(bNum);
            QThread::msleep(100);
            DownloadResult const res = sendStatus(m_hw->bl_wasSendingAddOK());
            if (res != DownloadResult::NOP) {
                if (res == DownloadResult::ERROR) {
                    if (++errorCount >= 10) {
                        qCritical() << "addr-block" << bNum << "...FAILED";
                        return res;
                    }
                } else { // res == DownloadResult::OK
                    // qInfo() << "addr-block" << bNum << "...OK";
                    return res;
                }
            } else {
                noAnswerCount += 1; // no answer by now
            }
        }
        // wait max. about 3 seconds
        return DownloadResult::TIMEOUT;
    }
    // blockNumber is not one of 0, 1024, 2048, 3072, 4096 -> do nothing
    return DownloadResult::NOP;
}

Update::DownloadResult Update::sendNextDataBlock(QByteArray const &binary,
                                                 int bNum) const {
    uint8_t local[66];
    int const bAddr = bNum * 64;
    int noAnswerCount = 0;
    int errorCount = 0;

    memcpy(local, binary.constData() + bAddr, 64);
    local[64] = local[65] = 0x00;

    // QByteArray b((const char *)(&local[0]), 64);
    // qCritical() << "SNDB" << bNum << b.size() << b.toHex();

    while (noAnswerCount <= 250) {
        m_hw->bl_sendDataBlock(64, local);
        QThread::msleep(10);
        DownloadResult const res = sendStatus(m_hw->bl_wasSendingDataOK());
        if (res != DownloadResult::NOP) {
            if (res == DownloadResult::ERROR) {
                if (++errorCount >= 10) {
                    qCritical() << "data for block" << bNum << "...FAILED";
                    return res;
                }
            } else {
                // qInfo() << "data for block" << bNum << "OK";
                return res;
            }
        } else {
            noAnswerCount += 1; // no answer by now
        }
    }
    // wait max. about 3 seconds
    return DownloadResult::TIMEOUT;
}

Update::DownloadResult Update::dc_downloadBinary(QByteArray const &b) const {
    int const nBlocks = (((b.size())%64)==0) ? (b.size()/64) : (b.size()/64)+1;

    // fill lst block of data to be sent with 0xFF
    QByteArray ba = b.leftJustified(nBlocks*64, (char)(0xFF));

    qInfo() << "total number of bytes to send to dc" << ba.size();
    qInfo() << "total number of blocks to send to dc" << nBlocks;

    int bNum = 0;
    DownloadResult res = DownloadResult::OK;
    fprintf(stderr, "\n64-byte block %04d ", bNum);
    while (res != DownloadResult::ERROR &&  bNum < nBlocks) {
        if ((res = sendNextAddress(bNum)) != DownloadResult::ERROR) {
            if ((res = sendNextDataBlock(ba, bNum)) != DownloadResult::ERROR) {
                bNum += 1;
                fprintf(stderr, ".");
                if ((bNum % 80) == 0) {
                    fprintf(stderr, "\n64-byte block %04d ", bNum);
                }
            }
        }
    }
    fprintf(stderr, "\nlast 64-byte block %04d\n", bNum);

    int const rest = ba.size() % 64;
    int const offset = ba.size() - rest;
    char const *startAddress = ba.constData() + offset;

    if (rest > 0) {
        // SHOULD NEVER HAPPEN !!!
        uint8_t local[66];
        memset(local, 0xFF, sizeof(local));
        memcpy(local, startAddress, rest);
        qCritical() << "ERROR SEND REMAINING" << rest << "BYTES";
        m_hw->bl_sendDataBlock(64, local);
    }

    m_hw->bl_sendLastBlock();
    qInfo() << "last result" << (int)sendStatus(m_hw->bl_wasSendingDataOK());
    return res;
}

bool Update::startBootloader() const { // deprecated
    return false;
#if 0
    int nStartTry = 5;
    while (--nStartTry >= 0) {
        m_hw->bl_startBL();
        QThread::msleep(500);
        int nCheckTry = 10;
        while (--nCheckTry >= 0) {
            m_hw->bl_checkBL();
            QThread::msleep(500);
            if (m_hw->bl_isUp()) {
                qInfo() << "starting bootloader...OK";
                return true;
            } else {
                qCritical() << "bootloader not up ("
                            << nStartTry << "," << nCheckTry << ")" << QThread::currentThread();
            }
        }
    }
    qCritical() << "starting bootloader...FAILED" << QThread::currentThread();
    return false;
#endif
}

bool Update::stopBootloader() const {
    // stop bootloader: this MUST work -> otherwise the PSA has to be restarted
    // manually
    emit m_worker->showErrorMessage("dc update", "stopping bootloader...");

    int nTryFinalize = 1; // could do this in an endless loop

    do {
        // in principle, any value except BL_STOP will do, as we want to detect
        // change to BL_STOP
        m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::BL_CHECK);

        QApplication::postEvent(
            m_worker->mainWindow(),
            new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_STOP, nTryFinalize));

        QThread::sleep(1);

        int const cntLimit = 20;
        int cnt = 0;
        while (++cnt < cntLimit &&
            m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_STOP) {
            // wait until bl_stopBL() has been sent
            QThread::msleep(500);
        }

        QApplication::postEvent(
            m_worker->mainWindow(),
            new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_CHECK, nTryFinalize));
        QThread::sleep(1);

        QApplication::postEvent(
            m_worker->mainWindow(),
            new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_IS_UP, nTryFinalize));
        QThread::sleep(1);

        cnt = 0;
        while (++cnt < cntLimit &&
            m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_DOWN) {
            // wait until  done
            QThread::msleep(200);
        }

    } while (++nTryFinalize <= MainWindow::BL_STOP_COUNT &&
             m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_DOWN);

    return (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_DOWN);
}

// br is a index into a table, used for historical reasons.
bool Update::openSerial(int br, QString baudrate, QString comPort) const {
    qDebug() << "opening serial" << br << baudrate << comPort << "...";
    if (m_hw->dc_openSerial(br, baudrate, comPort, 1) == true) { // 1 for connect
        Utils::printInfoMsg(
            QString("OPENING SERIAL %1").arg(br)
                  + " " + baudrate + " " + comPort + "...OK");

        // m_hw->dc_autoRequest(true);
        m_hw->dc_autoRequest(false);
        QThread::sleep(1);

        Utils::printInfoMsg(QString("IS PORT OPEN %1").arg(m_hw->dc_isPortOpen()));
        return true;
    }

    Utils::printCriticalErrorMsg(
        QString("OPENING SERIAL %1").arg(br)
              + " " + baudrate + " " + comPort + "...FAILED");
    return false;
}

void Update::closeSerial() const {
    qInfo() << "CLOSED SERIAL" << m_baudrate << m_serialInterface;
    m_hw->dc_closeSerial();
}

bool Update::isSerialOpen() const {
    return m_hw->dc_isPortOpen();
}

bool Update::resetDeviceController() const { // deprecated
    return false;
#if 0
    qDebug() << "resetting device controller...";
    m_hw->bl_rebootDC();
    // wait maximally 3 seconds, before starting bootloader
    qInfo() << "resetting device controller...OK";
    return true;
#endif
}

QByteArray Update::loadBinaryDCFile(QString filename) const {
    qDebug() << "loading dc binary" << filename << "...";

    QFile file(filename); // closed in destructor call
    if (!file.exists()) {
        qCritical() << file.fileName() << "does not exist";
        return QByteArray();
    }
    if (!file.open(QIODevice::ReadOnly)) {
        qCritical() << "cannot open file" << file.fileName();
        return QByteArray();
    }
    qInfo() << "loading dc binary" << filename << "...OK";
    return file.readAll();
}

bool Update::downloadBinaryToDC(QString const &bFile) const {
    qDebug() << "sending" << bFile << "to dc...";
    QByteArray const dcBinary = loadBinaryDCFile(bFile);
    if (dcBinary.size() > 0) {
        if (dc_downloadBinary(dcBinary) != DownloadResult::OK) {
            qCritical() << "sending" << bFile << "to dc...FAILED";
            return false;
        } else {
            qInfo() << "sending" << bFile << "to dc...OK";
        }
    } else {
        qCritical() << "sending" << bFile << "to dc...FAILED";
        qCritical() << "loading binary" << bFile << "FAILED";
        return false;
    }
    return true;
}

/*

 ///////////////////////////////////////////////////////////////////////////////
 //
 //                        USING THE DC BOOTLOADER
 //
 ///////////////////////////////////////////////////////////////////////////////

 1  :   bl_reboot()     // send to application, want DC2 to reset (in order to
                        // start the bootloader)
                        //
                        // NOTE: this function is NOT reliable !!! Sometimes it
                        // simply does not work, in which case bl_startBL,
                        // bl_checkBL and bl_isUp do not work as well.
                        // Alas, there is no feedback if bl_reboot worked!
                        //
                        // NOTE: this function can be called only once per
                        // minute, because once called again, the controller
                        // performs some self-checks consuming some time.
                        //
                        // NOTE: after a successful bl_reboot(), the device is
                        // waiting about 4 seconds in the bootloader. To stay in
                        // the bootloader, we have to send the command
                        // bl_startBL(), which is kind of a misnomer, as it
                        // should be bl_doNotLeaveBL().
                        //
 2  :   bl_startBL():   // send within 4s after DC power-on, otherwise
                        // bootloader is left.
                        //
                        // NOTE: a running bootloader is a MUST for the download
                        // process of a device controller firmware as it does
                        // the actual writing of the memory (the bl_reboot()
                        // from above erases the available memory).
                        //
 3  :   bl_check():     // send command to verify if bl is up
                        //
                        // NOTE: this command is kind of a request that we want
                        // to check if the bootloader is up. The device
                        // (actually the bootloader) responds with its version.
                        //
 4  :   bl_isUp():      // returns true if bl is up and running
                        //
                        // NOTE: we know what the bootloader version actually is
                        // as the bootloader does not change. By comparing the
                        // string received in the previous step with this known
                        // version string we know if the bootloader is up.
                        //
                        // NOTE FOR ALL PREVIOUS STEPS: execute them in their
                        // own slots each to be sure to receive any possible
                        // responds from the device.
                        //
 5  :   bl_sendAddress(blockNumber)
                        // send start address, nr of 64-byte block, start with 0
                        // will be sent only for following block-numbers:
                        // 0, 1024, 2048, 3072 and 4096, so basically every
                        // 64kByte.
                        // for other addresses nothing happens

 6  :   bl_wasSendingAddOK()
                        // return val:  0: no response by now
                        //              1: error
                        //             10: OK

 7  :   bl_sendDataBlock()
                        // send 64 byte from bin file

 8  :   bl_sendLastBlock()
                        // send this command after all data are transferred

 9  :   bl_wasSendingDataOK()
                        // return val:  0: no response by now
                        //              1: error
                        //             10: OK

 10 :   bl_stopBL()     // leave bl and start (the new) application
                        //
                        // NOTE: this function MUST work under all conditions.
                        // Alas, there is no direct result for this command, so
                        // the only way of knowing it was successful is to ask
                        // the device if the bootloader is still running.
                        // There is no problem to repeat this command until the
                        // bootloader is really not running anymore.
 */
bool Update::updateBinary(char const *fileToSendToDC) {
    qInfo() << "UPDATING DEVICE CONTROLLER FIRMWARE BINARY" << fileToSendToDC;
    QFile fn(fileToSendToDC);
    bool r;
    if ((r = fn.exists()) == true) {
        QFileInfo fi(fn);
        if ((r = updateDC(fileToSendToDC)) == true) {
            Utils::printInfoMsg(
                QString("      UPDATING BINARY ") + fi.fileName()
                      + QString(" (size=%1").arg(fi.size()) + ") DONE");
        } else {
            Utils::printCriticalErrorMsg(
                QString("      UPDATING BINARY ") + fi.fileName()
                      + QString(" (size=%1").arg(fi.size()) + ") FAILED");
        }
    } else {
        Utils::printCriticalErrorMsg(
            QString(fileToSendToDC) + " DOES NOT EXIST -> NO UPDATE OF DC FIRMWARE");
    }
    return r;
}

bool Update::updateDC(QString bFile) const {
    qDebug() << "IN UPDATEDC: UPDATING DC: FILE TO SEND" << bFile;

    m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::NONE);

    QApplication::postEvent(            // step 1: reset device controller
        m_worker->mainWindow(),
        new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::DC_REBOOT, 1));
    QThread::sleep(1);

    for (int i=1; i <= MainWindow::BL_START_COUNT; ++i) {
        QApplication::postEvent(        // step 2: start bootloader (5x)
            m_worker->mainWindow(),
            new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_START, i));
        QThread::sleep(1);
    }

    int const cntLimit = 100;       // wait until its for sure that bl_startBL()
    int cnt = 0;                    // has been excuted
    while (++cnt < cntLimit &&
        m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_START) {
        // wait until all bl_startBL() are done
        QThread::msleep(200);
    }

    if (cnt == cntLimit) {
        // start events not received ???
        Utils::printCriticalErrorMsg("BL_START EVENT NOT RECEIVED AFTER 20 SECS");
        return false;
    }

    m_worker->mainWindow()->setUpdateStep(UpdateDcEvent::UpdateStep::BL_CHECK);

    for (int i=1; i <= MainWindow::BL_IS_UP_COUNT; ++i) {
        QApplication::postEvent(m_worker->mainWindow(), new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_CHECK, i));
        QThread::sleep(1);
        QApplication::postEvent(m_worker->mainWindow(), new UpdateDcEvent(m_worker, UpdateDcEvent::UpdateStep::BL_IS_UP, i));
        if (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_UP) {
            break;
        }
        QThread::sleep(1);
    }

    cnt = 0;
    while (++cnt < cntLimit &&
        m_worker->mainWindow()->updateStep() != UpdateDcEvent::UpdateStep::BL_IS_UP) {
        // wait until all bl_startBL() are done
        QThread::msleep(200);
    }

    if (cnt == cntLimit) {
        // really not up
        Utils::printCriticalErrorMsg("BL_IS_UP EVENT NOT RECEIVED AFTER 20 SECS");
        stopBootloader(); // try to stop bootloader whichhas been already started
        return false;
    }

    if (m_worker->mainWindow()->updateStep() == UpdateDcEvent::UpdateStep::BL_IS_UP) {
        // bootloader MUST be running to download device-controller
#if 0
        if (!downloadBinaryToDC(bFile)) {
            Utils::printCriticalErrorMsg(
                QString("UPDATING DC: ") + bFile + " ...DOWNLOAD FAILED");
        }
#endif

    } else {
        Utils::printCriticalErrorMsg(
            QString("UPDATING DC: ") + bFile + " BOOT LOADER NOT RUNNING -> NO DOWNLOAD ("
                  + QThread::currentThread()->objectName() + ")");
        return false;
    }

    // do this unconditionally, even if bootloader is not running at all ->
    // the controller possibly tells us nonsense.
    if (!stopBootloader()) {
        Utils::printCriticalErrorMsg(
            QString("UPDATING DC: ") + bFile + " BOOT LOADER STILL RUNNING ("
                  + QThread::currentThread()->objectName() + ")");
        return false;
    }

    Utils::printInfoMsg(QString("UPDATING DC: ") + bFile + " ...OK");
    return true;
}

QString Update::jsonType(enum FileTypeJson type) {
    switch (type) {
    case FileTypeJson::CASH: return "CASH";
    case FileTypeJson::CONFIG: return "CONFIG";
    case FileTypeJson::PRINTER: return "PRINTER";
    case FileTypeJson::SERIAL: return "SERIAL";
    case FileTypeJson::DEVICE: return "DEVICE";
    case FileTypeJson::TIME: return "TIME";
    }
    return "N/A";
}

bool Update::downloadJson(enum FileTypeJson type,
                          int templateIdx,
                          QString jsFileToSendToDC) const {

    qDebug() << "updating json-file:" << jsFileToSendToDC << "...";
    qDebug() << "    template-index:" << templateIdx;
    qDebug() << "         json-type:" << jsonType(type);

    m_hw->dc_autoRequest(true); // downloading Json needs the AutoEmission flag
    qDebug() << "SET AUTO-REQUEST=TRUE";
    QThread::sleep(1);  // make sure the auto-request flag is acknowledged

    bool ready = false;
    int nTry = 25;
    while ((ready = m_hw->sys_ready4sending()) == false) {
        QThread::msleep(200);
        if (--nTry <= 0) {
            qCritical() << "SYS NOT READY FOR SENDING AFTER 5 SECONDS";
            break;
        }
    }

    bool ret = false;
    if (ready) {
        QFile file(jsFileToSendToDC);
        QFileInfo fi(jsFileToSendToDC); // max. size of template file is 800 bytes
        if (file.exists()) {
            if (file.open(QIODevice::ReadOnly)) {
                if (fi.size() <= 800) {
                    QByteArray ba = file.readAll();
                    if (m_hw->sys_sendJsonFileToDc((uint8_t)(type),
                                                   templateIdx,
                                                   (uint8_t *)ba.data())) {
                        QThread::sleep(1);
                        qDebug() << "SENT" << jsFileToSendToDC;
                        ret = true;
                    }
                } else {
                    qCritical() << "SIZE OF" << jsFileToSendToDC
                                << "TOO BIG (" << fi.size() << "BYTES)";
                }
            } else {
                qCritical() << "CANNOT OPEN" << jsFileToSendToDC << "FOR READING";
            }
        } else {
            qCritical() << jsFileToSendToDC << "DOES NOT EXIST";
        }
    }

    m_hw->dc_autoRequest(false);
    qDebug() << "SET AUTO-REQUEST=FALSE";
    QThread::sleep(1);  // make sure the auto-request flag is acknowledged

    return ret;
}

bool Update::updatePrinterTemplate(int templateIdx, QString jsFile) const {
    return downloadJson(FileTypeJson::PRINTER, templateIdx, jsFile);
}

bool Update::updateConfig(QString jsFile) {
    return downloadJson(FileTypeJson::CONFIG, 0, jsFile);
}

bool Update::updateCashConf(QString jsFile) {
    return downloadJson(FileTypeJson::CASH, 0, jsFile);
}

bool Update::updateDeviceConf(QString jsFile) {
    return downloadJson(FileTypeJson::DEVICE, 0, jsFile);
}

QStringList Update::split(QString line, QChar sep) {
    QStringList lst;
    QString next;
    int start = 0, end;

    while ((end = line.indexOf(sep, start)) != -1) {
        next = line.mid(start, end - start).trimmed();
        lst << next;
        start = end + 1;
    }
    next = line.mid(start, end - start).trimmed();
    lst << next;

    return lst;
}

void Update::readyReadStandardOutput() {
    QProcess *p = (QProcess *)sender();
    QByteArray buf = p->readAllStandardOutput();
    qCritical() << buf;
}

void Update::readyReadStandardError() {
    QProcess *p = (QProcess *)sender();
    QByteArray buf = p->readAllStandardError();
    qCritical() << buf;
}

void Update::finished(int /*exitCode*/, QProcess::ExitStatus /*exitStatus*/) {
    QProcess *p = (QProcess *)sender();
    disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardOutput()));
    disconnect(p, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(readyReadStandardError()));
}

QStringList Update::getDcSoftAndHardWareVersion() {
    m_hw->dc_autoRequest(true);
    QThread::sleep(1); // make sure the timer-slots are active

    for (int i=0; i < 3; ++i) { // send explicit reuests to get
                                // current SW/HW-versions
        m_hw->request_DC2_SWversion();
        m_hw->request_DC2_HWversion();
        QThread::sleep(1);
    }

    QString const &hwVersion = m_hw->dc_getHWversion().toLower().trimmed();
    QString const &swVersion = m_hw->dc_getSWversion().toLower().trimmed();

    m_hw->dc_autoRequest(false);
    QThread::sleep(1); // make sure the timer-slots are inactive

    if (!hwVersion.isEmpty() && !swVersion.isEmpty()) {
        return QStringList() << hwVersion << swVersion;
    }

    return QStringList() << "DC HW-version not available"
                         << "DC SW-version not available";
}

bool Update::doUpdate(int &displayIndex, QStringList const &filesToWorkOn) {
    bool serialOpened = false;

    QString const &parentName = Utils::getParentName();

    Utils::printInfoMsg(
        QString("PARENT OF ATB-UPDATE-TOOL (ppid=%1) ").arg(getppid()) + parentName);

    if (parentName == "ATBQT" || parentName == "systemd") {
        // the tool was not called during 'service' ot during an automatic
        // update procedure. and it was called explicitly with libCAmaster.so
        if (m_pluginName.contains("master", Qt::CaseInsensitive)) {
            Utils::printCriticalErrorMsg(parentName
                + " IS MASTER, BUT ATB-UPDATE-TOOL CALLED WITH libCAmaster.so");
            return false;
        }
        Utils::printInfoMsg(
            QString("ATB-UPDATE-TOOL STARTED AS SLAVE OF ") + parentName);
    } else
    if (Utils::isATBQTRunning()) {  // manual testing
        if (m_pluginName.contains("master", Qt::CaseInsensitive)) {
            Utils::printCriticalErrorMsg(
                "ATBQT IS MASTER, BUT ATB-UPDATE-TOOL CALLED WITH libCAmaster.so");
            return false;
        }
        Utils::printInfoMsg(
            "ATB-UPDATE-TOOL STARTED AS SLAVE-SIBLING OF ATBQT-MASTER");
    } else {
        if (m_pluginName.contains("slave", Qt::CaseInsensitive)) {
            Utils::printCriticalErrorMsg(
                "ATB-UPDATE-TOOL CALLED WITH libCAslave.so ALTHOUGH MASTER");
            return false;
        }
        Utils::printInfoMsg("ATB-UPDATE-TOOL STARTED AS MASTER");

        if ((serialOpened = openSerial(baudrateMap.value(m_baudrate),
                                       m_baudrate,
                                       m_serialInterface)) == false) {
                Utils::printCriticalErrorMsg(
                    QString("CANNOT OPEN ")
                          + m_serialInterface
                          + "( BAUDRATE=" + m_baudrate + ")");
                return false;
        }

        m_hw->dc_autoRequest(false);

        Utils::printInfoMsg(
            QString("SERIAL OPEN ") + m_serialInterface
                + " (BAUDRATE=" + m_baudrate + ")");
    }

    bool res = false;
    QList<QString>::const_iterator it;
    for (it = filesToWorkOn.cbegin(); it != filesToWorkOn.cend(); ++it) {
        m_worker->startProgressLoop();
        QString fToWorkOn = (*it).trimmed();
        fToWorkOn = QDir::cleanPath(m_customerRepository + QDir::separator() + fToWorkOn);

        static const QRegularExpression version("^.*dc2c[.][0-9]{1,2}[.][0-9]{1,2}[.]bin.*$");
        if (fToWorkOn.contains(version)) {
            Utils::printInfoMsg("DO-UPDATE FILE-TO-WORK-ON " + fToWorkOn);

            QFile fn(fToWorkOn);
            QFileInfo finfo(fn);
            if (!fn.exists()) { // check for broken link
                Utils::printCriticalErrorMsg("DO-UPDATE FILE-TO-WORK-ON "
                    + fToWorkOn + " DOES NOT EXIST");
                res = false;
            } else {
                bool updateBinaryRes = true;

                qInfo() << "DOWNLOADING" << finfo.completeBaseName() << "TO DC";
#if UPDATE_DC == 1
                m_hw->dc_autoRequest(false);// default: turn auto-request setting off
                QThread::sleep(1);          // wait to be sure that there are no more
                                            // commands sent to dc-hardware
                qInfo() << "SET AUTO-REQUEST=FALSE";

                if ((updateBinaryRes = updateBinary(fToWorkOn.toStdString().c_str())) == true) {
                    qCritical() << "downloaded binary" << fToWorkOn;
                    ++displayIndex;
                    emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")")
                        + QString(" Update ") + QFileInfo(fToWorkOn).fileName(),
                        Worker::UPDATE_STEP_DONE);
                }

                m_hw->dc_autoRequest(true); // turn auto-request setting on
                qInfo() << "SET AUTO-REQUEST=TRUE";

                QStringList const &versions = Update::getDcSoftAndHardWareVersion();
                if (versions.size() >= 2) {
                    if (updateBinaryRes == true) {
                        qInfo() << "dc-hardware-version (UPDATED)" << versions[0];
                        qInfo() << "dc-firmware-version (UPDATED)" << versions[1];
                    } else {
                        qInfo() << "dc-hardware-version (NOT UPDATED)" << versions[0];
                        qInfo() << "dc-firmware-version (NOT UPDATED)" << versions[1];
                    }
                }
#endif
                res = updateBinaryRes;
            }
        } else if (fToWorkOn.contains("DC2C_print", Qt::CaseInsensitive)
                && fToWorkOn.endsWith(".json", Qt::CaseInsensitive)) {
            res = true;
#if UPDATE_PRINTER_TEMPLATES == 1
            int i = fToWorkOn.indexOf("DC2C_print", Qt::CaseInsensitive);
            int const templateIdx = fToWorkOn.mid(i).midRef(10, 2).toInt();
            if ((templateIdx < 1) || (templateIdx > 32)) {
                qCritical() << "WRONG TEMPLATE INDEX" << templateIdx;
                res = false;
            } else {
                if ((res = updatePrinterTemplate(templateIdx, fToWorkOn))) {
                    qInfo() << "downloaded printer template"<< fToWorkOn;
                    ++displayIndex;
                    emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")")
                          + QString(" Update ") + QFileInfo(fToWorkOn).fileName(),
                            Worker::UPDATE_STEP_DONE);
                }
            }
#endif
        } else if (fToWorkOn.contains("DC2C_cash", Qt::CaseInsensitive)
                && fToWorkOn.endsWith(".json", Qt::CaseInsensitive)) {
            res = true;
#if UPDATE_CASH_TEMPLATE == 1
            if ((res = updateCashConf(fToWorkOn))) {
                qInfo() << "downloaded cash template"<< fToWorkOn;
                ++displayIndex;
                emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")")
                      + QString(" Update ") + QFileInfo(fToWorkOn).fileName(),
                        Worker::UPDATE_STEP_DONE);
            }
#endif
        } else if (fToWorkOn.contains("DC2C_conf", Qt::CaseInsensitive)
                && fToWorkOn.endsWith(".json", Qt::CaseInsensitive)) {
            res = true;
#if UPDATE_CONF_TEMPLATE == 1
            if ((res= updateConfig(fToWorkOn))) {
                qInfo() << "downloaded config template"<< fToWorkOn;
                ++displayIndex;
                emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")")
                      + QString(" Update ") + QFileInfo(fToWorkOn).fileName(),
                        Worker::UPDATE_STEP_DONE);
            }
#endif
        } else if (fToWorkOn.contains("DC2C_device", Qt::CaseInsensitive)
                && fToWorkOn.endsWith(".json", Qt::CaseInsensitive)) {
            res = true;
#if UPDATE_DEVICE_TEMPLATE == 1
            if ((res = updateDeviceConf(fToWorkOn))) {
                qInfo() << "downloaded device template"<< fToWorkOn;
                ++displayIndex;
                emit m_worker->appendText(QString("\n(") + QString("%1").arg(displayIndex).rightJustified(2, ' ') + QString(")")
                      + QString(" Update ") + QFileInfo(fToWorkOn).fileName(),
                        Worker::UPDATE_STEP_DONE);
            }
#endif
        } else {
            qCritical() << "UNKNOWN JSON FILE NAME" << fToWorkOn;
            res = false;
        }
        m_worker->stopProgressLoop();
        m_worker->setProgress(100);

        if (res == false) {
            break;
        }
    } // for (it = openLines.cbegin(); it != openLines.end(); ++it) {

    //m_hw->dc_autoRequest(true);  // ALWAYS turn autoRequest ON
    //qDebug() << "SET AUTO-REQUEST=TRUE";

    if (serialOpened) {
        m_hw->dc_closeSerial();
    }

    return res;
}
