// Mosaic

// Copyright (C) 2009,2025 Quirky <quirky.quippy@gmail.com>

// This file is part of Mosaic

// Mosaic is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Mosaic 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 Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with Mosaic.  If not, see <https://www.gnu.org/licenses/>.

#include <assert.h>
#include <stdio.h>
#include <getopt.h>
#include <time.h>
#include <stdlib.h>

#include "itty/media.hh"
#include "itty/paths.hh"

#include "conf.hh"
#include "game.hh"
#include "strings.hh"

std::filesystem::path parse_options(int argc, char *argv[])
{
        static char str_usage[] =
                "Usage: %s [-h] [-v] [PATH]\n"
                "\n";

        static char str_options[] =
                "Options:\n"
                "  -h    Display this information and exit.\n"
                "  -v    Display version string and exit.\n"
                "\n";

        int c;

        while ((c = getopt(argc, argv, "hv")) != -1)
                switch (c) {
                 case 0:
                         break; // If this option set a flag do nothing.
                 case 'h':
                         printf(str_usage, argv[0]);
                         printf(str_options);
                         exit(0);
                 case 'v':
                         printf("%s\n", str_version);
                         exit(0);
                 default:
                         fprintf(stderr, str_usage);
                         exit(-1);
                }

        SDL_Log("Mosaic %s\n", str_revision);

        switch (argc - optind) {
         case 0:
                return itty::paths.data.r(basedir / "level/princess.tmj");
         case 1:
                return argv[optind];
         default:
                fprintf(stderr, str_usage, argv[0]);
                exit(-1);
        }
}

int main(int argc, char *argv[])
{
        assert(sizeof(float) == sizeof(GLfloat));
        auto path = parse_options(argc, argv);
        conf_load();
        if (itty::Media_lock _{"Mosaic"})
                play(path);
        conf_save();
        return 0;
}
