#include "common.h"
#include <stdio.h>

#if defined(HAVE_MIGEMO)
extern "C"
{
#include "oniguruma.h"
#include "migemo.h"
}

migemo* gMigemo;
#endif

void regex_init()
{
#if defined(HAVE_MIGEMO)
    char buf[PATH_MAX];
    gMigemo = migemo_open(NULL);

    if(gKanjiCode == kUtf) {
        sprintf(buf, "%s/share/migemo-dic/utf-8/migemo-dict", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_MIGEMO, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/utf-8/roma2hira.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_ROMA2HIRA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/utf-8/hira2kata.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HIRA2KATA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/utf-8/han2zen.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HAN2ZEN, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
    }
    else if(gKanjiCode == kEuc) {
        sprintf(buf, "%s/share/migemo-dic/euc-jp/migemo-dict", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_MIGEMO, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/euc-jp/roma2hira.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_ROMA2HIRA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/euc-jp/hira2kata.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HIRA2KATA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/euc-jp/han2zen.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HAN2ZEN, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
    }
    else {
        sprintf(buf, "%s/share/migemo-dic/cp932/migemo-dict", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_MIGEMO, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/cp932/roma2hira.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_ROMA2HIRA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/cp932/hira2kata.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HIRA2KATA, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
        sprintf(buf, "%s/share/migemo-dic/cp932/han2zen.dat", PREFIX_PATH);
        if(migemo_load(gMigemo, MIGEMO_DICTID_HAN2ZEN, buf) == MIGEMO_DICTID_INVALID) {
            fprintf(stderr, "%s is not found\n", buf);
            exit(1);
        }
    }
#endif
}

void regex_final()
{
#if defined(HAVE_MIGEMO)
    migemo_close(gMigemo);
#endif
}

int regcmp(char* reg_str, char* str)
{
#if defined(HAVE_MIGEMO)
    int result = 1;

FILE* f = fopen("aaa", "w");

    const OnigUChar* p = migemo_query(gMigemo, (const unsigned char*)reg_str);
    if(p == NULL) {
fclose(f);
        return 0;
    }
fprintf(f, "migemo %s", p);

    int r;
    regex_t* reg;
    OnigErrorInfo err_info;

    if(gKanjiCode == kUtf) {
        r = onig_new(&reg, p, p + strlen((char*)p), ONIG_OPTION_DEFAULT, ONIG_ENCODING_UTF8, ONIG_SYNTAX_PERL, &err_info);
    }
    else if(gKanjiCode == kEuc) {
        r = onig_new(&reg, p, p + strlen((char*)p), ONIG_OPTION_DEFAULT, ONIG_ENCODING_EUC_JP, ONIG_SYNTAX_PERL, &err_info);
    }
    else {
        r = onig_new(&reg, p, p + strlen((char*)p), ONIG_OPTION_DEFAULT, ONIG_ENCODING_SJIS, ONIG_SYNTAX_PERL, &err_info);
    }

    if(r != ONIG_NORMAL) {
fprintf(f, "aaa");
        migemo_release(gMigemo, (unsigned char*)p);
fclose(f);
        return 0;
    }

    OnigRegion* region;
    region = onig_region_new();
    
    OnigUChar* str2 = (OnigUChar*)str;

    int r2 = onig_search(reg, str2, str2 + strlen((char*)str2), str2, str2 + strlen((char*)str2), region, ONIG_OPTION_NONE);

    if(r2 != 0 || r2 == ONIG_MISMATCH) {
fprintf(f, "bbb");
        result = 0;
    }

    onig_region_free(region, 1);
    onig_free(reg);
    onig_end();

    migemo_release(gMigemo, (unsigned char*) p);


fclose(f);

    return result;
#endif
}
