
// cmdline.cpp - implementation for hairline command line processing

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <GL/glew.h>

#include "parse_fms_mode.hpp"
#include "polar/global_polar_settings.hpp"
#include "cmdline.hpp"
#include "decoration.hpp"
#include "showfps.h"
#include "hairline.hpp"
#include "textures.hpp"

#ifdef _WIN32
# if !defined(stricmp) && !defined(__CYGWIN__)
#  define stricmp _stricmp  // avoid deprecation warning
# endif
# if !defined(strnicmp) && !defined(__CYGWIN__)
#  define strnicmp _strnicmp  // avoid deprecation warning
# endif
#else
# include <strings.h>
# define stricmp strcasecmp
# define strnicmp strncasecmp
#endif

extern const char *program_name;

bool registerDebugCallback = false;
bool enable_GL_DEBUG_OUTPUT_SYNCHRONOUS = false;

void parseCommandLine(int argc, char **argv)
{
    for (int i=1; i<argc; i++) {
        if (!stricmp("-debugcallback", argv[i])) {
            registerDebugCallback = 1;
            continue;
        }
        if (!stricmp("-debugcallback2", argv[i])) {
            registerDebugCallback = 1;
            enable_GL_DEBUG_OUTPUT_SYNCHRONOUS = true;
            continue;
        }
        if (!strcmp(argv[i], "-vsync")) {
            frame_sync = true;
            continue;
        }
        if (!strcmp(argv[i], "-white")) {
            background = 1;
            continue;
        }
        if (!strcmp(argv[i], "-black")) {
            background = 0;
            continue;
        }
        if (!strcmp(argv[i], "-nooutline")) {
            overlay_outline = 0;
            continue;
        }
        if (!strcmp(argv[i], "-glsync")) {
            enable_gl_debug_output_synchronous = true;
        }
        if (!strcmp(argv[i], "-animate")) {
            animating = 1;
            continue;
        }
        if (!strcmp(argv[i], "-v")) {
            verbose = true;
            continue;
        }
        if (!strcmp(argv[i], "-miter") || !stricmp(argv[i], "-miterlimit")) {
            i++;
            if (!argv[i]) {
                printf("no miter limit specified after -miter\n");
                exit(1);
            }
            sscanf(argv[i], "%g", &initial_miter_limit);
            printf("initializing miter limit to %g\n", initial_miter_limit);
            continue;
        }
        if (!strcmp(argv[i], "-extrusion") || !stricmp(argv[i], "-arcLengthExtrusion")) {
            i++;
            if (!argv[i]) {
                printf("no miter limit specified after -miter\n");
                exit(1);
            }
            sscanf(argv[i], "%g", &initial_miter_limit);
            printf("initializing miter limit to %g\n", initial_miter_limit);
            continue;
        }
        if (!stricmp(argv[i], "-noHullLines")) {
            decoration = decoration & ~DRAW_HULL_LINES;
            continue;
        }
        if (!stricmp(argv[i], "-noCaps")) {
            initially_no_caps = true;
            continue;
        }
        if (!stricmp(argv[i], "-noJoins")) {
            initially_no_joins = true;
            continue;
        }
        if (!stricmp(argv[i], "-noOverlay")) {
            show_status_text = false;
            continue;
        }
        if (!stricmp(argv[i], "-texture")) {
            i++;
            if (!argv[i]) {
                printf("no filename specified after -texture\n");
                exit(1);
            }
            setInitialTexture(argv[i]);
            continue;
        }
        if (!stricmp(argv[i], "-noCull")) {
            stroke_tessellator_settings.disableTrivialCulling();
            continue;
        }
        if (!strcmp(argv[i], "-mode")) {
            i++;
            if (i < argc) {
                bool ok = parseMixedSamplesModeString(argv[i],
                    initial_stencil_samples, initial_color_samples, use_supersample);
                if (ok) {
                    continue;
                }
            }
            modeOptionHelp(program_name);
            exit(1);
        }
        if (!strcmp(argv[i], "-scale")) {
            i++;
            if (i < argc) {
                float f = 1;
                int rc = sscanf(argv[i], "%g", &f);
                if (rc == 1) {
                    extern float global_line_width_scale;
                    extern float global_point_size_scale;
                    global_line_width_scale = f;
                    global_point_size_scale = 0.5f*f;
                } else {
                   printf("warning: could not parse %s to a float\n", argv[i]);
                }
                continue;
            } else {
                printf("-scale needs to be followed by a number\n");
                exit(1);
            }
        }
        if (!stricmp("-reportMS", argv[i]) || !stricmp("-ms", argv[i])) {
            printf("report animating frame rate in milliseconds per frame\n");
            reportFPSinMS();
            continue;
        }
        if (!stricmp("-reportFPS", argv[i]) || !stricmp("-fps", argv[i])) {
            printf("report animating frame rate in frames per second\n");
            reportFPSinFPS();
            continue;
        }
        fprintf(stderr, "usage: %s [-fillonly]\n",
            program_name);
        exit(1);
    }
}
