2024-04-10 20:51:56 -04:00
|
|
|
// This file is part of JustMotion.
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2024-04-10 20:51:56 -04:00
|
|
|
// JustMotion is free software: you can redistribute it and/or modify
|
2022-09-22 20:57:46 -04:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2024-04-10 20:51:56 -04:00
|
|
|
// JustMotion is distributed in the hope that it will be useful,
|
2022-09-22 20:57:46 -04:00
|
|
|
// 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.
|
2022-04-14 09:45:54 -04:00
|
|
|
|
2023-05-17 15:06:58 -04:00
|
|
|
#include "common.h"
|
2023-05-15 15:29:47 -04:00
|
|
|
#include "camera.h"
|
2024-04-21 08:03:30 -04:00
|
|
|
#include "proc_control.h"
|
2022-08-12 21:46:36 -04:00
|
|
|
|
2024-04-10 20:51:56 -04:00
|
|
|
void showHelp(const QString etcDir)
|
2023-07-31 11:16:07 -04:00
|
|
|
{
|
2024-04-10 20:51:56 -04:00
|
|
|
QTextStream(stdout) << APP_NAME << " " << APP_VERSION << Qt::endl << Qt::endl;
|
|
|
|
QTextStream(stdout) << "Usage: " << APP_TARGET << " <argument>" << Qt::endl << Qt::endl;
|
2023-07-31 11:16:07 -04:00
|
|
|
QTextStream(stdout) << "-h : display usage information about this application." << Qt::endl;
|
2024-04-21 08:03:30 -04:00
|
|
|
QTextStream(stdout) << "-d : all valid config files found in " << etcDir << " will be used to" << Qt::endl;
|
|
|
|
QTextStream(stdout) << " create camera instances. (this is blocking, meant to run with systemd)" << Qt::endl;
|
2023-07-31 11:16:07 -04:00
|
|
|
QTextStream(stdout) << "-v : display the current version." << Qt::endl;
|
2024-04-21 08:03:30 -04:00
|
|
|
QTextStream(stdout) << "-u : uninstall the entire app from your system, including the service." << Qt::endl;
|
2023-07-31 11:16:07 -04:00
|
|
|
QTextStream(stdout) << "-f : force an action without pausing for user confirmation." << Qt::endl;
|
2024-04-21 08:03:30 -04:00
|
|
|
QTextStream(stdout) << "-s : view the status of all camera instances." << Qt::endl;
|
|
|
|
QTextStream(stdout) << "-q : kill all camera instances." << Qt::endl;
|
|
|
|
QTextStream(stdout) << "-r : same as -d except it is non-blocking by starting all camera instances" << Qt::endl;
|
|
|
|
QTextStream(stdout) << " via systemd. same as 'systemctl start " << APP_TARGET << "'" << Qt::endl;
|
2023-07-31 11:16:07 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
2023-04-20 14:52:59 -04:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
QCoreApplication::setApplicationName(APP_NAME);
|
2024-04-10 20:51:56 -04:00
|
|
|
QCoreApplication::setApplicationVersion(APP_VERSION);
|
2023-04-20 14:52:59 -04:00
|
|
|
|
2024-04-21 08:03:30 -04:00
|
|
|
auto args = QCoreApplication::arguments();
|
|
|
|
auto etcDir = "/etc/" + QString(APP_TARGET);
|
|
|
|
auto staDir = QDir::tempPath() + "/" + APP_TARGET + "-stats";
|
|
|
|
auto procFile = QDir::tempPath() + "/" + APP_TARGET + "-proc";
|
|
|
|
auto ret = 0;
|
|
|
|
|
|
|
|
if (!QFileInfo::exists(staDir))
|
|
|
|
{
|
|
|
|
mkPath(staDir);
|
|
|
|
}
|
2022-12-11 10:25:22 -05:00
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
if (args.contains("-h"))
|
2023-02-18 21:20:24 -05:00
|
|
|
{
|
2024-04-10 20:51:56 -04:00
|
|
|
showHelp(etcDir);
|
2023-02-07 23:19:41 -05:00
|
|
|
}
|
2023-05-15 15:29:47 -04:00
|
|
|
else if (args.contains("-v"))
|
2022-08-12 21:46:36 -04:00
|
|
|
{
|
2024-04-10 20:51:56 -04:00
|
|
|
QTextStream(stdout) << APP_VERSION << Qt::endl;
|
2023-03-05 16:07:07 -05:00
|
|
|
}
|
2024-04-21 08:03:30 -04:00
|
|
|
else if (args.contains("-d"))
|
2023-07-31 11:16:07 -04:00
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
auto confs = lsFilesInDir(etcDir);
|
|
|
|
auto proc = new ProcControl(procFile, staDir, &app);
|
|
|
|
|
|
|
|
if (!proc->init())
|
2023-10-27 15:43:17 -04:00
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
ret = EACCES;
|
2023-10-27 15:43:17 -04:00
|
|
|
}
|
2024-04-21 08:03:30 -04:00
|
|
|
else
|
2023-10-27 15:43:17 -04:00
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
for (auto &&conf : confs)
|
|
|
|
{
|
|
|
|
new Camera(etcDir + "/" + conf, staDir, proc, &app);
|
|
|
|
}
|
2023-05-17 15:06:58 -04:00
|
|
|
|
2024-04-21 08:03:30 -04:00
|
|
|
ret = app.exec();
|
2022-12-11 10:25:22 -05:00
|
|
|
}
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
2023-07-31 11:16:07 -04:00
|
|
|
else if (args.contains("-u"))
|
|
|
|
{
|
|
|
|
if (args.contains("-f"))
|
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
ret = QProcess::execute("/opt/" + QString(APP_TARGET) + "/uninstall.sh", QStringList());
|
2023-07-31 11:16:07 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char ans;
|
|
|
|
|
|
|
|
std::cout << "This will completely uninstall " << APP_NAME << " from your system. continue y/n? ";
|
|
|
|
std::cin >> ans;
|
|
|
|
|
|
|
|
if (ans == 'y' || ans == 'Y')
|
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
ret = QProcess::execute("/opt/" + QString(APP_TARGET) + "/uninstall.sh", QStringList());
|
2023-07-31 11:16:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 08:03:30 -04:00
|
|
|
else if (args.contains("-s"))
|
|
|
|
{
|
|
|
|
auto statFiles = lsFilesInDir(staDir);
|
|
|
|
|
|
|
|
for (auto &&statFile: statFiles)
|
|
|
|
{
|
|
|
|
QFile file(staDir + "/" + statFile);
|
|
|
|
|
|
|
|
file.open(QFile::ReadOnly);
|
|
|
|
|
|
|
|
std::cout << file.readAll().data();
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (args.contains("-q"))
|
|
|
|
{
|
|
|
|
QFile::remove(procFile);
|
|
|
|
QThread::currentThread()->sleep(5);
|
|
|
|
}
|
2023-07-31 11:16:07 -04:00
|
|
|
else if (args.contains("-r"))
|
|
|
|
{
|
2024-04-21 08:03:30 -04:00
|
|
|
QProcess::execute("systemctl start " + QString(APP_TARGET));
|
2023-07-31 11:16:07 -04:00
|
|
|
}
|
2022-04-14 09:45:54 -04:00
|
|
|
else
|
|
|
|
{
|
2024-04-10 20:51:56 -04:00
|
|
|
showHelp(etcDir);
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|
|
|
|
|
2023-05-15 15:29:47 -04:00
|
|
|
return ret;
|
2022-04-14 09:45:54 -04:00
|
|
|
}
|