#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include <libgen.h>

#define BUFFER_SIZE 1024

void convert_slashes(char* path) {
    for (char *p = path; *p != '\0'; p++) {
        if (*p == '/') {
            *p = '\\';
        }
    }
}

void remove_tb_textures_line(const char* file_path) {
    FILE *input_file, *output_file;
    char buffer[BUFFER_SIZE];
    char output_file_path[BUFFER_SIZE];
    char parent_folder[BUFFER_SIZE];
    char resolved_file_path[BUFFER_SIZE];

    // Convert forward slashes to backslashes
    strcpy(resolved_file_path, file_path);
    convert_slashes(resolved_file_path);

    // Resolve file path if no drive letter is included
    if (!(isalpha(resolved_file_path[0]) && resolved_file_path[1] == ':')) {
        char executable_path[BUFFER_SIZE];
        GetModuleFileName(NULL, executable_path, BUFFER_SIZE);
        char* exe_dir = dirname(executable_path);
        snprintf(resolved_file_path, BUFFER_SIZE, "%s\\%s", exe_dir, file_path);
        convert_slashes(resolved_file_path);
    }

    // Open the input file for reading
    input_file = fopen(resolved_file_path, "r");
    if (input_file == NULL) {
        perror("Error opening input file");
        exit(EXIT_FAILURE);
    }

    // Copy the file path to modify and get the parent folder
    strcpy(output_file_path, resolved_file_path);
    char *last_backslash = strrchr(output_file_path, '\\');
    if (last_backslash != NULL) {
        *last_backslash = '\0'; // Terminate the string to get the parent directory path
        strcpy(parent_folder, output_file_path);

        // Find the next to last backslash to move up one directory
        last_backslash = strrchr(parent_folder, '\\');
        if (last_backslash != NULL) {
            *(last_backslash + 1) = '\0'; // Terminate to get the grandparent directory path
        } else {
            perror("Invalid file path");
            fclose(input_file);
            exit(EXIT_FAILURE);
        }
    } else {
        perror("Invalid file path");
        fclose(input_file);
        exit(EXIT_FAILURE);
    }

    // Generate the output file path by concatenating the grandparent directory path and the file name
    strcat(parent_folder, strrchr(resolved_file_path, '\\') + 1);
    strcpy(output_file_path, parent_folder);

    // Open the output file for writing
    output_file = fopen(output_file_path, "w");
    if (output_file == NULL) {
        perror("Error opening output file");
        fclose(input_file);
        exit(EXIT_FAILURE);
    }

    // Read each line from the input file and write to output file, except the line that starts with "_tb_textures"
    while (fgets(buffer, BUFFER_SIZE, input_file) != NULL) {
        if (strncmp(buffer, "\"_tb_textures\"", 14) != 0) {
            fputs(buffer, output_file);
        }
    }

    // Close the files
    fclose(input_file);
    fclose(output_file);

    printf("File processed successfully. Saved to: %s\n", output_file_path);
}

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <file_path>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    remove_tb_textures_line(argv[1]);

    return 0;
}
