/* TCHCLI.C - The Cat Helpfile CLI (Win32 Final) */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "TCHELP.H"

#ifndef INVALID_FILE_ATTRIBUTES
#define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
#endif

#define MAX_PATHS 20
#define MAX_FILES 100

char g_SearchPaths[MAX_PATHS][MAX_PATH];
int g_PathCount = 0;
char g_FoundFiles[MAX_FILES][MAX_PATH];
int g_FoundCount = 0;

void LoadPathFile() {
    char exePath[MAX_PATH], pathFile[MAX_PATH], line[MAX_PATH];
    FILE* f; char* p;
    g_PathCount = 0;
    GetCurrentDirectory(MAX_PATH, g_SearchPaths[0]); g_PathCount++;
    GetModuleFileName(NULL, exePath, MAX_PATH);
    p = strrchr(exePath, '\\'); if(p) *p=0;
    wsprintf(pathFile, "%s\\Path", exePath);
    f = fopen(pathFile, "r");
    if (f) {
        while (fgets(line, MAX_PATH, f)) {
            line[strcspn(line, "\r\n")] = 0;
            if (strlen(line) > 0 && g_PathCount < 50) strcpy(g_SearchPaths[g_PathCount++], line);
        }
        fclose(f);
    }
}

/* UPDATE: Unterstützt jetzt tchelp und tch */
int ResolvePath(char* input, char* outPath) {
    int i, e;
    char testPath[MAX_PATH];
    char candidate[MAX_PATH];
    /* Reihenfolge: Exakt, dann .tchelp, dann .tch */
    char* extensions[] = { "", ".tchelp", ".TCH", ".tch" };
    
    for(e=0; e<4; e++) {
        wsprintf(candidate, "%s%s", input, extensions[e]);

        /* 1. Direkt */
        if (GetFileAttributes(candidate) != INVALID_FILE_ATTRIBUTES) {
            strcpy(outPath, candidate);
            return 1;
        }
        /* 2. Pfade */
        for(i=0; i<g_PathCount; i++) {
            wsprintf(testPath, "%s\\%s", g_SearchPaths[i], candidate);
            if (GetFileAttributes(testPath) != INVALID_FILE_ATTRIBUTES) {
                strcpy(outPath, testPath);
                return 1;
            }
        }
    }
    return 0;
}

void ListPaths() {
    int i;
    for(i=0; i<g_PathCount; i++) printf("%s\n", g_SearchPaths[i]);
}

void SearchFiles(char* pattern) {
    int i; WIN32_FIND_DATA fd; HANDLE hFind; char search[MAX_PATH];
    g_FoundCount = 0;
    for(i=0; i<g_PathCount; i++) {
        /* Suche nach *.* und filtere dann, um tch und tchelp zu finden */
        wsprintf(search, "%s\\*.*", g_SearchPaths[i]);
        hFind = FindFirstFile(search, &fd);
        if (hFind != INVALID_HANDLE_VALUE) {
            do {
                /* Filter auf Endung und Suchbegriff */
                if ((strstr(fd.cFileName, ".tchelp") || strstr(fd.cFileName, ".TCH")) && strstr(fd.cFileName, pattern)) {
                    if (g_FoundCount < MAX_FILES) wsprintf(g_FoundFiles[g_FoundCount++], "%s\\%s", g_SearchPaths[i], fd.cFileName);
                }
            } while (FindNextFile(hFind, &fd));
            FindClose(hFind);
        }
    }
}

void ShowHelp(char* file, char* lang) {
    HelpData d; char fullPath[MAX_PATH]; int i, j;
    if (!ResolvePath(file, fullPath)) { printf("Fehler: Datei '%s' nicht gefunden.\n", file); return; }
    if (TC_LoadXML(fullPath, &d)) {
        printf("\nDatei: %s\nAutor: %s (v%s)\n", fullPath, d.auth, d.ver);
        printf("--------------------------------------------------\n");
        for(i=0; i<d.lang_cnt; i++) {
            if (lang && stricmp(d.lang[i].key, lang) != 0) continue;
            printf("[%s] %s\n", d.lang[i].key, d.lang[i].desc);
            for(j=0; j<d.lang[i].sec_cnt; j++) printf("  %s\n  %s\n\n", d.lang[i].sec[j].name, d.lang[i].sec[j].text);
        }
        if (d.link_cnt > 0) { printf("Verweise: "); for(i=0; i<d.link_cnt; i++) printf("%s ", d.links[i]); printf("\n"); }
    } else printf("Fehler beim Parsen.\n");
}

int main(int argc, char* argv[]) {
    HelpData data; char fullPath[MAX_PATH]; int i;
    LoadPathFile();
    if (argc < 2) {
        printf("The Cat Helpfile CLI\nUsage: TCHCLI <cmd> [arg]\n");
        printf("  -lsp           Zeige Pfade\n  -lsf <such>    Suche Dateien\n  <file> [lang]  Zeige Hilfe\n");
        return 0;
    }
    if (strcmp(argv[1], "-lsp") == 0) { ListPaths(); return 0; }
    if (strcmp(argv[1], "-lsf") == 0) { if (argc > 2) { SearchFiles(argv[2]); for(i=0; i<g_FoundCount; i++) printf("%s\n", g_FoundFiles[i]); } return 0; }
    /* Viewer starten (optional) */
    if (strcmp(argv[1], "-sh") == 0) { 
        /* Interaktive Auswahl Code hier einfügen falls gewünscht */
        return 0; 
    }

    /* Standard */
    ShowHelp(argv[1], (argc > 2) ? argv[2] : NULL);
    return 0;
}