"""Generate Matlab interface for C code to form a condensed problem with vector
parameters.
"""

import os

from muaompc._ldt.codegen.common.codegen import CCodeGenerator as CCG


class MatlabCodeGenerator(CCG, object):

    """Generate Matlab code common to code generation classes.

    This class overrides the following methods:
    generate_code: generates Matlab specific code on its own directory
    _make_destination_dir_tree: creates Matlab own directory
    """

    def __init__(self, ccg):
        """Assign to self cvp, path attributes from the given C code.

        This depends on C-code already being generated by C-code
        generation class. This creates the interface to that specific code.

        :param ccg: an instance of the C code generator class for which
        a Matlab interface is to be created
        :type ccg: muaompc._ldt.codegen.common.codegen.CCodeGenerator
        """
        self.prefix = ccg.prefix
        self.path = ccg.path

    def generate_code(self):
        self._make_destination_dir_tree()
        self._generate_matlab_code()

    def _make_destination_dir_tree(self):
        try:
            os.mkdir(os.path.join(self.path['dest'], 'matlab'))
        except OSError:
            pass
        try:
            os.mkdir(os.path.join(self.path['dest'], 'matlab', 'include'))
        except OSError:
            pass

    def _generate_matlab_code(self):
        self._replace_prefix('matlab.c', srcdir='matlab', destdir='matlab')
        self._replace_prefix('matlab.h', srcdir='matlab',
                destdir=os.path.join('matlab', 'include'))
