#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "hamming_gen.h"

int main(int argc, char **argv)
{
    // get k
    int k;
    if (argc >= 2) {
        char *last_char;
        k = strtol(argv[1], &last_char, 10);
        if (*last_char != '\0') {
            fprintf(stderr, "\"k\" not a number\n");
            return EXIT_FAILURE;
        }
        if (k < 3) {
            fprintf(stderr, "\"k\" too small\n");
            return EXIT_FAILURE;
        }
    } else {
        fprintf(stderr, "\"k\" not specified\n");
        return EXIT_FAILURE;
    }

    // get output filename prefix
    const char *prefix = argv[2];

    // calculate parity bit count
    int r = get_parity_bits(k);

    // generate matrix
    int matrix[(k + r) * r];
    generate_opt_matrix(matrix, k, r);

    // write to files
    if (write_vhdl_pkg(prefix, 0, matrix, k, r) < 0) {
        fprintf(stderr, "Error writing file\n");
        return EXIT_FAILURE;
    }
    if (write_vhdl_pkg(prefix, 1, matrix, k, r) < 0) {
        fprintf(stderr, "Error writing file\n");
        return EXIT_FAILURE;
    }
    
    if (write_sim_script(k, r) < 0) {
        fprintf(stderr, "Error writing file\n");
        return EXIT_FAILURE;
    }


    return EXIT_SUCCESS;
}
