
/*---------------------------------------------------------------------------*/
/* apparguments.cpp                                                               */
/* copyright (c) innovaphone 2015                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

#include "platform/platform.h"

#include "apparguments.h"

static app_arguments_t appArguments;


app_arguments_t * ParseArguments(int argc, char *argv[])
{
    memset(&appArguments, 0, sizeof(app_arguments_t));

    // Set the default ports for http and https...
    appArguments.httpPort = 80;
    appArguments.httpsPort = 443;
    appArguments.reuseAddr = true;

    // If argc = 0, the function will be called just to get access to the internal
    // structure. This will be used by the innovaphone tester, but can also be used
    // by an application to get access to the structure at every point of the application.
    if (argc == 0)
        return &appArguments;
        
    appArguments.appCommand = argv[0];

    for (int i = 1; i < argc; i++) {
        bool error = false;
        char * curArg = argv[i];
        char * tok;
        size_t nameLen;

        if (*curArg != '-' || *(curArg + 1) != '-') {
            error = true;
        }
        else {
            curArg += 2;
            tok = strchr(curArg, '=');
            if (tok != NULL) {
                tok++;
                nameLen = tok - curArg;
            }
            else {
                nameLen = 1000;
            }

            if (strncmp(curArg, "app-password=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.appPassword = tok;
            }
            else if (strncmp(curArg, "manager=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.managerLocalPath = tok;
            }
            else if (strncmp(curArg, "service-id=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.serviceID = tok;
            }
            else if (strncmp(curArg, "webserver=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.webserverLocalPath = tok;
            }
            else if (strncmp(curArg, "default-path=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.defaultPath = tok;
            }
            else if (strcmp(curArg, "tls-upgrade") == 0) {
                appArguments.redirectToTLS = true;
            }
            else if(strcmp(curArg, "do-not-reuse-addr") == 0) {
                appArguments.reuseAddr = false;
            }
            else if(strncmp(curArg, "log-flags=", nameLen) == 0) {
                appArguments.logFlags = strtoull(tok, NULL, 0);
            }
            else if (strncmp(curArg, "log-size=", nameLen) == 0) {
                if (tok == NULL || *(tok + 1) == '\0') error = true;
                else appArguments.logFileSize = strtoul(tok, NULL, 10);
            }
            else if (strncmp(curArg, "http=", nameLen) == 0) {
                if (tok == NULL || *tok == '\0') error = true;
                else appArguments.httpPort = atoi(tok);
            }
            else if (strncmp(curArg, "https=", nameLen) == 0) {
                if (tok == NULL || *tok == '\0') error = true;
                else appArguments.httpsPort = atoi(tok);
            }
            else if (strcmp(curArg, "segfaulted") == 0) {
                appArguments.segfaulted = true;
            }
            else if (strcmp(curArg, "disable-hexdumps") == 0) {
                appArguments.disableHexdumps = true;
            }
            else {
                error = true;
            }
        }

        if (error) {
            debug->printf("Webserver::ParseArguments unknown argument %s", argv[i]);
        }
    }

    // Check if all neccesary parameters had been given...
    return &appArguments;
}
