//    This file is part of Motion Watch.

//    Motion Watch is free software: you can redistribute it and/or modify
//    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.

//    Motion Watch is distributed in the hope that it will be useful,
//    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.

#include "common.h"
#include "camera.h"

void showHelp()
{
    QTextStream(stdout) << APP_NAME << " " << APP_VER << Qt::endl << Qt::endl;
    QTextStream(stdout) << "Usage: mow <argument>" << Qt::endl << Qt::endl;
    QTextStream(stdout) << "-h : display usage information about this application." << Qt::endl;
    QTextStream(stdout) << "-c : path to the config file used to run a single camera instance." << Qt::endl;
    QTextStream(stdout) << "-d : path to a directory that can contain multiple config files." << Qt::endl;
    QTextStream(stdout) << "     each file found in the directory will be used to create a " << Qt::endl;
    QTextStream(stdout) << "     systemd service for the camera. already existing camera" << Qt::endl;
    QTextStream(stdout) << "     services will be reloaded and restarted. any services without" << Qt::endl;
    QTextStream(stdout) << "     a config file will be removed." << Qt::endl;
    QTextStream(stdout) << "-i : this is the same as -d except a directory does not need to be" << Qt::endl;
    QTextStream(stdout) << "     provided. the default directory /etc/" << APP_BIN << " will be used." << Qt::endl;
    QTextStream(stdout) << "-v : display the current version." << Qt::endl;
    QTextStream(stdout) << "-u : uninstall the entire app from your system, including all" << Qt::endl;
    QTextStream(stdout) << "     camera services." << Qt::endl;
    QTextStream(stdout) << "-f : force an action without pausing for user confirmation." << Qt::endl;
    QTextStream(stdout) << "-l : list all camera services along with statuses." << Qt::endl;
    QTextStream(stdout) << "-r : remove all camera services." << Qt::endl;
 }

int main(int argc, char** argv)
{   
    QCoreApplication app(argc, argv);

    QCoreApplication::setApplicationName(APP_NAME);
    QCoreApplication::setApplicationVersion(APP_VER);

    auto args = QCoreApplication::arguments();
    auto ret  = 0;

    if (args.contains("-h"))
    {
        showHelp();
    }
    else if (args.contains("-v"))
    {
        QTextStream(stdout) << APP_VER << Qt::endl;
    }
    else if (args.contains("-d"))
    {
        rmServices(); ret = loadServices(args);
    }
    else if (args.contains("-l"))
    {
        listServices();
    }
    else if (args.contains("-i"))
    {
        args = {"-d", "/etc/" + QString(APP_BIN)};

        rmServices(); ret = loadServices(args);
    }
    else if (args.contains("-c"))
    {
        auto *cam = new Camera(&app);

        ret = cam->start(args);

        if (ret == 0)
        {
            ret = QCoreApplication::exec();
        }
    }
    else if (args.contains("-u"))
    {
        if (args.contains("-f"))
        {
            rmServices(); QProcess::execute("/opt/mow/uninst");
        }
        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')
            {
                rmServices(); QProcess::execute("/opt/mow/uninst");
            }
        }
    }
    else if (args.contains("-r"))
    {
        rmServices();
    }
    else
    {
        showHelp();
    }

    return ret;
}
