/* Copyright (C) 2022 igoz <igoz@igoz.net>
 * See end of file for extended copyright and license notice. */

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

#define SETTINGS_PATH "settings.json"
#define SWAP_PATH     ".settings.json.ereacb-swp"
#define COPY_TO_BUF_SIZE 64
#define ENABLED_STR_SIZE (sizeof(json) - 1)

char const json[] = 
    "{\r\n"
    "  \"title\": \"ELDEN RING™\",\r\n"
    "  \"executable\": \"eldenring.exe\",\r\n"
    "  \"productid\": \"0a0\",\r\n"
    "  \"sandboxid\": \"0a0\",\r\n"
    "  \"deploymentid\": \"0a0\",\r\n"
    "  \"requested_splash\": \"./EasyAntiCheat/SplashScreen.png\",\r\n"
    "  \"wait_for_game_process_exit\": \"false\",\r\n"
    "  \"logo_position\": \"bottom-left\"\r\n"
    "}\r\n"
    ;

void copy_to(char const* dst_path, char const* src_path) {
    FILE* src = fopen(src_path, "r");
    if (!src) {
        fprintf(stderr, "Failed to open '%s': %s\n"
                , src_path, strerror(errno));
        exit(EXIT_FAILURE);
    }
    FILE* dst = fopen(dst_path, "w");
    if (!dst) {
        fprintf(stderr, "Failed to open or create '%s': %s\n"
                , dst_path, strerror(errno));
        exit(EXIT_FAILURE);
    }
    char buf[COPY_TO_BUF_SIZE];
    for (size_t read; (read = fread(buf, 1, COPY_TO_BUF_SIZE, src)) > 0;) {
        fwrite(buf, 1, read, dst);
    }
    fclose(src);
    fclose(dst);
}

void write_patch() {
    FILE* settings = fopen(SETTINGS_PATH, "w");
    if (!settings) {
        fprintf(stderr, "Failed to open '%s': %s\n"
                , SETTINGS_PATH, strerror(errno));
        exit(EXIT_FAILURE);
    }
    fwrite(json, 1, sizeof(json) - 1, settings);
    fclose(settings);
}

int enabled() {
    FILE* settings = fopen(SETTINGS_PATH, "r");
    if (!settings) {
        fprintf(stderr, "Failed to open '%s': %s\n"
                , SETTINGS_PATH, strerror(errno));
        exit(EXIT_FAILURE);
    }
    char str[ENABLED_STR_SIZE];
    memset(str, 0, ENABLED_STR_SIZE);
    fread(str, 1, ENABLED_STR_SIZE, settings);
    fclose(settings);
    return !strcmp(str, json);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s [option]\n"
                        "See \"%s --help\" for all options.\n"
                , argv[0], argv[0]);
        exit(EXIT_FAILURE);
    }

    if (!strcmp(argv[1], "--help")) {
        fprintf(stdout, "Usage: %s [option]\n"
                        "Options:\n"
                        "  -e, --enable   Enable the EAC bypass patch.\n"
                        "  -d, --disable  Disable the EAC bypass patch.\n"
                        "      --help     Display this message.\n"
                        "      --version  Display version information.\n"
                , argv[0]);
    } else if (!strcmp(argv[1], "-e") || !strcmp(argv[1], "--enable")) {
        if (!enabled()) {
            copy_to(SWAP_PATH, SETTINGS_PATH);
            write_patch();
        }
    } else if (!strcmp(argv[1], "-d") || !strcmp(argv[1], "--disable")) {
        if (enabled()) {
            copy_to(SETTINGS_PATH, SWAP_PATH);
        }
    } else if (!strcmp(argv[1], "--version")) {
        fprintf(stdout, "er-eac-bypass 1.0\n"
                        "Copyright (C) 2022 igoz <igoz@igoz.net>\n"
                        "License GPLv3+: GNU GPL version 3 or later "
                          "<https://gnu.org/licenses/gpl.html>.\n"
                        );
    } else {
        fprintf(stderr, "Unrecognized option '%s'\n"
                        "Usage: %s [option]\n"
                        "See \"%s --help\" for all options.\n"
                , argv[1], argv[0], argv[0]);
        exit(EXIT_FAILURE);
    }

    exit(EXIT_SUCCESS);
}

/* Copyright (C) 2022 igoz <igoz@igoz.net>
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */
