// Copyright (C) 2016 BlackBerry Limited. All rights reserved.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qnxdevice.h"

#include "qnxconstants.h"
#include "qnxdeployqtlibrariesdialog.h"
#include "qnxdevicetester.h"
#include "qnxtr.h"

#include <projectexplorer/devicesupport/idevicefactory.h>
#include <projectexplorer/devicesupport/sshparameters.h>

#include <remote/linuxdevice.h>
#include <remote/remotelinux_constants.h>
#include <remote/sshdevicewizard.h>

using namespace ProjectExplorer;
using namespace Remote;
using namespace Utils;

namespace Qnx::Internal {

static QString signalProcessByNameQnxCommandLine(const QString &filePath, int sig)
{
    QString executable = filePath;
    return QString::fromLatin1("for PID in $(ps -f -o pid,comm | grep %1 | awk '/%1/ {print $1}'); "
        "do "
            "kill -%2 $PID; "
        "done").arg(executable.replace(QLatin1String("/"), QLatin1String("\\/"))).arg(sig);
}

static QString killCommandForPath(const FilePath &filePath)
{
    return QString::fromLatin1("%1; %2").arg(signalProcessByNameQnxCommandLine(filePath.path(), 15),
                                             signalProcessByNameQnxCommandLine(filePath.path(), 9));
}
class QnxDevice final : public LinuxDevice
{
public:
    QnxDevice()
    {
        setDisplayType(Tr::tr("QNX"));
        setDefaultDisplayName(Tr::tr("QNX Device"));
        setOsType(OsTypeOtherUnix);
        setupId(IDevice::ManuallyAdded);
        setType(Constants::QNX_QNX_OS_TYPE);
        setMachineType(IDevice::Hardware);
        SshParameters sshParams;
        sshParams.setTimeout(10);
        setDefaultSshParameters(sshParams);
        setFreePorts(PortList::fromString("10000-10100"));
        sourceProfile.setDefaultValue(true);
        setKillCommandForPathFunction(killCommandForPath);

        addDeviceAction({Tr::tr("Deploy Qt libraries..."), [](const IDevice::Ptr &device) {
                             executeQnxDeployQtLibrariesDialog(device);
        }});
    }

    DeviceTester *createDeviceTester() final { return new QnxDeviceTester(shared_from_this()); }
};

class QnxDeviceFactory final : public IDeviceFactory
{
public:
    QnxDeviceFactory() : IDeviceFactory(Constants::QNX_QNX_OS_TYPE)
    {
        setDisplayName(Tr::tr("QNX Device"));
        setCombinedIcon(":/qnx/images/qnxdevicesmall.png",
                        ":/qnx/images/qnxdevice.png");
        setQuickCreationAllowed(true);
        setConstructionFunction([] { return IDevice::Ptr(new QnxDevice); });
        setCreator([]() -> IDevice::Ptr {
            const IDevice::Ptr device = IDevice::Ptr(new QnxDevice);
            SshDeviceWizard wizard(Tr::tr("New QNX Device Configuration Setup"), device);
            if (wizard.exec() != QDialog::Accepted)
                return {};
            return device;
        });
        setExecutionTypeId(Remote::Constants::ExecutionType);
    }
};

void setupQnxDevice()
{
    static QnxDeviceFactory theQnxDeviceFactory;
}

} // Qnx::Internal
