/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define CHAZ_USE_SHORT_NAMES
#define CHAZ_CONFWRITER_INTERNAL

#include "Charmonizer/Core/Util.h"
#include "Charmonizer/Core/ConfWriter.h"
#include "Charmonizer/Core/ConfWriterRuby.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Static vars. */
static FILE *config_rb_fh  = NULL;
static ConfWriter CWRuby_conf_writer;

/* Open the Charmony.rb file handle.
 */
static void
S_open_config_rb(void);

static void
S_ConfWriterRuby_clean_up(void);
static void
S_ConfWriterRuby_vappend_conf(const char *fmt, va_list args);
static void
S_ConfWriterRuby_add_def(const char *sym, const char *value);
static void
S_ConfWriterRuby_add_typedef(const char *type, const char *alias);
static void
S_ConfWriterRuby_add_sys_include(const char *header);
static void
S_ConfWriterRuby_add_local_include(const char *header);
static void
S_ConfWriterRuby_start_module(const char *module_name);
static void
S_ConfWriterRuby_end_module(void);

void
ConfWriterRuby_enable(void) {
    CWRuby_conf_writer.clean_up          = S_ConfWriterRuby_clean_up;
    CWRuby_conf_writer.vappend_conf      = S_ConfWriterRuby_vappend_conf;
    CWRuby_conf_writer.add_def           = S_ConfWriterRuby_add_def;
    CWRuby_conf_writer.add_typedef       = S_ConfWriterRuby_add_typedef;
    CWRuby_conf_writer.add_sys_include   = S_ConfWriterRuby_add_sys_include;
    CWRuby_conf_writer.add_local_include = S_ConfWriterRuby_add_local_include;
    CWRuby_conf_writer.start_module      = S_ConfWriterRuby_start_module;
    CWRuby_conf_writer.end_module        = S_ConfWriterRuby_end_module;
    S_open_config_rb();
    ConfWriter_add_writer(&CWRuby_conf_writer);
    return;
}

static void
S_open_config_rb(void) {
    /* Open the filehandle. */
    config_rb_fh = fopen("Charmony.rb", "w+");
    if (config_rb_fh == NULL) {
        Util_die("Can't open 'Charmony.rb': %s", strerror(errno));
    }

    /* Start the module. */
    fprintf(config_rb_fh,
            "# Auto-generated by Charmonizer. \n"
            "# DO NOT EDIT THIS FILE!!\n"
            "\n"
            "module Charmony\n"
            "\n"
            "defs = {}\n"
            "\n"
            "def config\n\defs\nend\n"
            "\n"
           );
}

static void
S_ConfWriterRuby_clean_up(void) {
    /* Write the last bit of Charmony.rb and close. */
    fprintf(config_rb_fh, "\nend\n\n");
    if (fclose(config_rb_fh)) {
        Util_die("Couldn't close 'Charmony.rb': %s", strerror(errno));
    }
}

static void
S_ConfWriterRuby_vappend_conf(const char *fmt, va_list args) {
    (void)fmt;
    (void)args;
}

static char*
S_ConfWriterRuby_quotify(const char *string, char *buf, size_t buf_size) {
    char *quoted = buf;

    /* Don't bother with undef values here. */
    if (!string) {
        return NULL;
    }

    /* Allocate memory if necessary. */
    {
        const char *ptr;
        size_t space = 3; /* Quotes plus NUL termination. */
        for (ptr = string; *ptr; ptr++) {
            if (*ptr == '\'' || *ptr == '\\') {
                space += 2;
            }
            else {
                space += 1;
            }
        }
        if (space > buf_size) {
            quoted = (char*)malloc(space);
        }
    }

    /* Perform copying and escaping */
    {
        const char *ptr;
        size_t pos = 0;
        quoted[pos++] = '\'';
        for (ptr = string; *ptr; ptr++) {
            if (*ptr == '\'' || *ptr == '\\') {
                quoted[pos++] = '\\';
            }
            quoted[pos++] = *ptr;
        }
        quoted[pos++] = '\'';
        quoted[pos++] = '\0';
    }

    return quoted;
}

#define CFRUBY_MAX_BUF 100
static void
S_ConfWriterRuby_add_def(const char *sym, const char *value) {
    char sym_buf[CFRUBY_MAX_BUF + 1];
    char value_buf[CFRUBY_MAX_BUF + 1];
    char *quoted_sym;
    char *quoted_value;

    /* Quote key. */
    if (!sym) {
        Util_die("Can't handle NULL key");
    }
    quoted_sym = S_ConfWriterRuby_quotify(sym, sym_buf, CFRUBY_MAX_BUF);

    /* Quote value or use "nil". */
    if (!value) {
        strcpy(value_buf, "nil");
        quoted_value = value_buf;
    }
    else {
        quoted_value = S_ConfWriterRuby_quotify(value, value_buf,
                                                CFRUBY_MAX_BUF);
    }

    fprintf(config_rb_fh, "defs[%s] = %s\n", quoted_sym, quoted_value);

    if (quoted_sym   != sym_buf)   { free(quoted_sym);   }
    if (quoted_value != value_buf) { free(quoted_value); }
}

static void
S_ConfWriterRuby_add_typedef(const char *type, const char *alias) {
    (void)type;
    (void)alias;
}

static void
S_ConfWriterRuby_add_sys_include(const char *header) {
    (void)header;
}

static void
S_ConfWriterRuby_add_local_include(const char *header) {
    (void)header;
}

static void
S_ConfWriterRuby_start_module(const char *module_name) {
    fprintf(config_rb_fh, "# %s\n", module_name);
}

static void
S_ConfWriterRuby_end_module(void) {
    fprintf(config_rb_fh, "\n");
}

