#include "SingleApplication.h"
#include <iostream>

SingleApplication::SingleApplication(const std::string& name)
    : m_name(name)
{
    m_lockFile = open(("/tmp/" + m_name + "-" + std::to_string(getuid())).c_str(), O_CREAT | O_RDWR, 0666);
    if (m_lockFile != -1 && flock(m_lockFile, LOCK_EX | LOCK_NB) == -1) {
        close(m_lockFile);
        m_lockFile = -1;
    }
}

SingleApplication::~SingleApplication()
{
    if (m_lockFile != -1) {
        flock(m_lockFile, LOCK_UN);
        close(m_lockFile);
    }
}

bool SingleApplication::isAlreadyRuning()
{
    return m_lockFile == -1;
}
