Prevent adding block with *sim* word in name

function mdaqBlockAdd(block_def)
    // Check version compatibility
    [is_supp vers] = mdaq_is_working('mdaqBlockAdd');
    if is_supp == %F then
        error('ERROR: ' + vers)
        return;
    end

    if type(block_def) <> 17 then
        disp("ERROR: Wrong type of input argument!");
        if type(block_def) == 13 then
            disp("Input argument type is function - use ''block = mdaqBlock()'' instead of ''block = mdaqBlock''")
        else
            disp("Use mdaqBlock() function to create initialized block sctructure!")
        end
        return;
    end

    if size(block_def.in, 'r' ) > 1 then
        if size(block_def.in, 'c' ) > 1 then
            disp("ERROR: Wrong block ''in'' parameter - one dimentional array expected!");
            return;
        end
        block_def.in = block_def.in';
    end

    if size(block_def.out, 'r' ) > 1 then
        if size(block_def.out, 'c' ) > 1 then
            disp("ERROR: Wrong block ''out'' parameter - one dimentional array expected!")
            return;
        end
        block_def.out = block_def.out';
    end

    if length(block_def.name) > 20 then
        disp("ERROR: Block name too long!");
        return;
    end

    FORCE_SIM = %F;
    if haveacompiler() == %F & block_def.use_sim_script == %F then
        warning("Compiler not found.");
        warning("This block will use simulation script instead of C code during simulation mode.");
        FORCE_SIM = %T;
    end

    path = dirname(get_function_path('mdaqBlockAdd')) + filesep();
    module_path = part(path,1:length(path)-length("macros") - 1 );
    SCRIPT_FILE_ROOT = path + 'user_blocks' + filesep();
    C_FILE_ROOT = module_path+'src'+filesep()+'c'+ filesep()+'userlib'+filesep();
    IMAGE_FILE_ROOT = module_path + filesep() + 'images' + filesep();

    // =============================PREPARE INPUT=====================================
    params_size = max(size(block_def.param_name));
    params_size_size = max(size(block_def.param_size));
    params_def_val_size = max(size(block_def.param_def_val));
    in_size = max(size(block_def.in));
    out_size = max(size(block_def.out));

    if block_def.in(1) < 1 then
        in_size = 0;
    end

    if block_def.out(1) < 1 then
        out_size = 0;
    end

    //If user did not enter enough information about size of parameters then
    //we set it to default 1.
    if params_size > params_size_size then
        sizes = ones(1:params_size)';
        sizes(1:params_size_size) = block_def.param_size;
        block_def.param_size = sizes;
    end

    //If user did not enter enough information about default parameters value then
    //we set it to default 0.
    def_val = list();
    for i=1:params_size
        def_val(i) = zeros(1:block_def.param_size(i))';

        if i <= params_def_val_size then
            i_params_def_val_size = max(size(block_def.param_def_val(i)));
            def_val(i)(1:i_params_def_val_size) = block_def.param_def_val(i);
        end
    end

    block_def.param_def_val = def_val;

    // if block name changed without desc - change block name in desc
    if block_def.desc == "Set new_block parameters" then
        block_def.desc = strsubst(block_def.desc, "new_block", block_def.name);
    end

    // =============================GENERATE STRINGS=====================================
    // converted params
    params_converted = '';

    // script file
    params_string1 = '';
    params_string2 = '';
    params_string3 = '';
    params_string4 = '';
    params_string5 = '';

    //default values string
    params_string6 = '';

    // C file
    in_ports_string = '';
    out_ports_string = '';
    params_c_string = '';
    params_sci_string = '';

    name_converted = convstr(block_def.name,'l');
    name_converted = strsubst(name_converted, ' ', '_');
    if strstr(name_converted, 'sim') <> "" then
        error("Cannot create block with '"*sim*'" word (known issue). Choose another block name.");
    end
    name_converted = 'mdaq_' + name_converted;

    for i = 1:params_size
        // Remove spaces and convert to lower case
        params_converted(i) = convstr(block_def.param_name(i),'l')
        params_converted(i) = strsubst(params_converted(i), ' ', '_')
        params_converted(i) = strsubst(params_converted(i), ';', '_')
        params_converted(i) = strsubst(params_converted(i), ':', '_')
        params_converted(i) = strsubst(params_converted(i), ',', '_')
        params_converted(i) = strsubst(params_converted(i), '.', '_')

        params_string1 = params_string1 + params_converted(i)+',';
        params_string2 = params_string2 + ''''+block_def.param_name(i)+':'';';
        disp(params_string2)
        params_string3 = params_string3 + '''vec'','+ string(block_def.param_size(i)) + ',';
        params_string4 = params_string4 + 'sci2exp(' + params_converted(i) + ');';
        params_string5 = params_string5 + params_converted(i)+';';

        i_def_param_size = max(size(block_def.param_def_val(i)));
        i_param_def_string = '';

        for j = 1:block_def.param_size(i)
            i_param_def_string = i_param_def_string + string(block_def.param_def_val(i)(j))+';';
        end

        //default values string
        params_string6(i) = '       '+params_converted(i)+' = ['+i_param_def_string+'];';

    end

    //delete last character ','
    params_string3 = part(params_string3, 1:(length(params_string3)-1) );

    in_string = '';
    out_string = '';

    if  in_size > 0 then
        for i = 1:(in_size-1)
            in_string = in_string + string(block_def.in(i)) + ';';
        end
        in_string = in_string + string(block_def.in(in_size));
    end

    if  out_size > 0 then
        for i = 1:(out_size-1)
            out_string = out_string + string(block_def.out(i)) + ';';
        end
        out_string = out_string + string(block_def.out(out_size));
    end

    block_v_size = 3;
    if max([out_size in_size]) > 2 then
        block_v_size = block_v_size + (max([out_size in_size]) - 2);
    end

    block_h_size = 4;
    if length(block_def.name) > 8 then
        block_h_size = block_h_size + ((length(block_def.name) - 8) * 0.2);
    end

    // =============================GENERATE SCRIPT=====================================
    // TODO define default params value
    //  (block_def.name).sci script generator
    block_script = [
    '// Generated with MicroDAQ toolbox ver: ' + mdaq_version() + '';
    'function [x,y,typ] = '+ name_converted + '(job,arg1,arg2)';
    '   block_desc = [''' + block_def.desc + ''';';
    '   '''';]';
    '';
    '   x=[];y=[];typ=[];';
    '   select job';
    '   case ''set'' then';
    '       x=arg1;';];

    if params_size > 0 then
        block_script = [block_script;
        '       model=arg1.model;';
        '       graphics=arg1.graphics;';
        '       exprs=graphics.exprs;';
        '';
        '       while %t do';
        '           try';
        '               getversion(''scilab'');';
        '               [ok,'+params_string1+'exprs]=..'
        '               scicos_getvalue( block_desc,..';
        '               ['+params_string2+'],..';
        '               list('+params_string3+'), exprs)';
        '           catch';
        '               [ok,'+params_string1+'exprs]=..'
        '               getvalue(block_desc,..';
        '               ['+params_string2+'],..';
        '               list('+params_string3+'), exprs);';
        '           end;';
        '';
        '       if ~ok then';
        '              break';
        '       end';
        '';
        '       if ok then';
        '           [model,graphics,ok] = check_io(model,graphics, ['+in_string+'], ['+out_string+'], 1, []);';
        '           graphics.exprs = exprs;';
        '           model.rpar = ['+params_string5+'];';
        '           model.ipar = [];';
        '           model.dstate = [];';
        '           x.graphics = graphics;';

        '           x.model = model;';
        '           break';
        '       end';
        '';
        '   end'];
    end

     use_sim_string = [
            '       if c_link('''+name_converted+''') then';
            '           model.sim=list('''+name_converted+''',4);';
            '       else';
            '           model.sim=list('''+name_converted+'_sim'',5);';
            '           warning(''Cannot link '''''+name_converted+''''' C function. Script '''''+name_converted+'_sim.sci'''' will be used instead.'');';
            '       end';
    ];

    if block_def.use_sim_script == %T then
       use_sim_string = '       model.sim=list('''+name_converted+'_sim'',5);';
    end

    block_script = [block_script; '   case ''define'' then';
    params_string6;
    '       model=scicos_model();';
    use_sim_string;
    '       model.in=['+in_string+'];';
    '       model.in2=1;';
    '       model.out=['+out_string+'];';
    '       model.out2=1;';
    '       model.outtyp=1;';
    '       model.intyp=1;';
    '       model.evtin=1;';
    '       model.rpar=['+params_string5+'];';
    '       model.ipar=[];';
    '       model.dstate=[];';
    '       model.blocktype=''d'';';
    '       model.dep_ut=[%t %f];';
    '       exprs=['+params_string4+'];';
    '       gr_i=[''xstringb(orig(1),orig(2),['''''''' ; ],sz(1),sz(2),''''fill'''');''];';
    '       x=standard_define([' + string(block_h_size) + ' '+string(block_v_size)+'],model,exprs,gr_i);';
    '       x.graphics.in_implicit=[];';
    '       x.graphics.exprs=exprs;';
    '       x.graphics.style=[""blockWithLabel;verticalLabelPosition=center;displayedLabel=' + block_def.name + '""]';
    '   end';
    'endfunction';
    ];

    // =============================GENERATE SIM SCRIPT=====================================

    index = 0;
    n_lines = 1;
    l = 1;
    for i = 1:n_lines:(params_size*n_lines)
        params_sci_string(i+1) =  '    '+params_converted(l)+' = block.rpar('+string(index + 1)+');';
        index = index + block_def.param_size(l);
        l = l + 1;
    end
    //  (block_def.name)_sim.sci script generator
    init_string = '';
    if FORCE_SIM == %T then
        init_string = [
'           mprintf('"\nWARNING: The '''''+name_converted+''''' block uses '''''+name_converted+'_sim.sci'''' script instead of C code during\n\t simulation mode. '');';
'           mprintf('"Make sure that the valid compiler is installed. More information is available at:\n'');';
'           mprintf('"\t https://help.scilab.org/doc/5.5.2/en_US/supported_compilers.html'');';
        ];
    end

    block_script_sim = [
    '// Generated with MicroDAQ toolbox ver: ' + mdaq_version() + '';
    'function block='+name_converted+'_sim(block,flag)';
    '';
    '    global %microdaq';
    '    if %microdaq.dsp_loaded == %F then';
    params_sci_string;
    '    select flag';
    '       case -5 // Error';
    '       case 0 // Derivative State Update';
    '       case 1 // Output Update';
    '       case 2 // State Update';
    '       case 3 // OutputEventTiming';
    '       case 4 // Initialization';
    init_string;
    '       case 5 // Ending';
    '       case 6 // Re-Initialisation';
    '       case 9 // ZeroCrossing';
    '       else // Unknown flag';
    '       break';
    '    end';
    'end';
    'endfunction';
    ];

    // save scripts
    file_name = SCRIPT_FILE_ROOT+name_converted+'.sci';
    save_string(file_name, block_script);

    file_name = SCRIPT_FILE_ROOT+name_converted+'_sim.sci';
    save_string(file_name, block_script_sim);

    // =============================GENERATE C FILE =====================================
    l = 1;
    n_lines = 3;
    index = 0;

    if in_size > 0 then
        in_ports_string(1) = '    /* Block input ports */';
        for i = 2:n_lines:(in_size*n_lines+1)
            in_ports_string(i) = '    double *u'+string(l)+' = GetRealInPortPtrs(block,'+string(l)+');';
            in_ports_string(i+1) = '    int u' +string(l)+ '_size = GetInPortRows(block,'+ string(l)+');'+...
            '    /* u' + string(l)+ '_size = '+string(block_def.in(l))+' */';
            in_ports_string(i+2) = '';
            l = l + 1;
        end
    end

    l = 1;

    if out_size > 0 then
        out_ports_string(1) = '    /* Block output ports */';
        for i = 2:n_lines:(out_size*n_lines+1)
            out_ports_string(i) = '    double *y'+string(l)+' = GetRealOutPortPtrs(block,'+string(l)+');';
            out_ports_string(i+1) = '    int y'+string(l)+'_size = GetOutPortRows(block,'+string(l)+');'+....
            '    /* y' + string(l)+ '_size = '+string(block_def.out(l))+' */';
            out_ports_string(i+2) = '';
            l = l + 1;
        end
    end

    index = 0;
    n_lines = 2;
    l = 1;
    for i = 1:n_lines:(params_size*n_lines)
        if  block_def.param_size(l) == 1 then
            params_c_string(i) = '    /* param size = 1 */';
            params_c_string(i+1) =  '    double '+params_converted(l)+' = params['+string(index)+'];';
        else
            params_c_string(i) = '    int '+params_converted(l)+'_size = '+string(block_def.param_size(l))+';';
            params_c_string(i+1) =  '    double *'+params_converted(l)+' = &params['+string(index)+'];';
        end

        index = index + block_def.param_size(l);
        l = l + 1;
    end

    block_c_file = [
    '/* Generated with MicroDAQ toolbox ver: ' + mdaq_version() + ' */';
    '#include '"scicos_block4.h'"';
    '';
    '';
    'extern double get_scicos_time( void );';
    '';
    '/* This function will executed once at the beginning of model execution */'
    'static void init(scicos_block *block)';
    '{';
    '    /* Block parameters */';
    '    double *params = GetRparPtrs(block);';
    '';
    params_c_string;
    '';
    '    /* Add block init code here */';
    '}';
    '';
    '/* This function will be executed on every model step */';
    'static void inout(scicos_block *block)';
    '{';
    '    /* Block parameters */';
    '    double *params = GetRparPtrs(block);';
    params_c_string;
    '';
    in_ports_string;
    out_ports_string;
    '';
    '    /* Add block code here (executed every model step) */';
    '';
    '}';
    '';
    '/* This function will be executed once at the end of model execution (only in Ext mode) */'
    'static void end(scicos_block *block)';
    '{';
    '    /* Prameters */';
    '    double *params = GetRparPtrs(block);';
    '';
    params_c_string;
    '';
    '    /* Add block end code here */';
    '}';
    '';
    'void '+name_converted+'(scicos_block *block,int flag)';
    '{';
    '    if (flag == 1){            /* set output */';
    '        inout(block);';
    '    }';
    '    else if (flag == 5){       /* termination */';
    '        end(block);';
    '    }';
    '    else if (flag == 4){       /* initialisation */';
    '        init(block);';
    '    }';
    '}';
    ]

    file_name = C_FILE_ROOT+name_converted+'.c';
    save_string(file_name, block_c_file);

    svg_path = IMAGE_FILE_ROOT + 'svg' + filesep();
    gen_svg(svg_path, name_converted, block_def.name);

    // build macros and compile C code
    mdaqBlockBuild(%F, ~block_def.use_sim_script);
    mprintf("\tRestart Scilab to use new block\n");
endfunction

function res = save_string(filename, content)
    [f,res] = mopen(filename,'w');
    if res == 0 then
        mprintf(" ### Generating %s\n", filename);
        mputl(content,f)
        mclose(f)
    end
endfunction

function gen_svg(path,filename,label);
    doc = xmlRead(path+'mdaq_template.svg');

    // Label handle
    node = doc.root.children(13).children(1);
    node.content = "";
    xmlDump(node);
    xmlWrite(doc,path+filename+'.svg');
    xmlDelete(doc);
endfunction

function mdaqBlockAdd(block_def)
    // Check version compatibility
    [is_supp vers] = mdaq_is_working('mdaqBlockAdd');
    if is_supp == %F then
        error('ERROR: ' + vers)
        return;
    end

    if type(block_def) <> 17 then
        disp("ERROR: Wrong type of input argument!");
        if type(block_def) == 13 then
            disp("Input argument type is function - use ''block = mdaqBlock()'' instead of ''block = mdaqBlock''")
        else
            disp("Use mdaqBlock() function to create initialized block sctructure!")
        end
        return;
    end

    if size(block_def.in, 'r' ) > 1 then
        if size(block_def.in, 'c' ) > 1 then
            disp("ERROR: Wrong block ''in'' parameter - one dimentional array expected!");
            return;
        end
        block_def.in = block_def.in';
    end

    if size(block_def.out, 'r' ) > 1 then
        if size(block_def.out, 'c' ) > 1 then
            disp("ERROR: Wrong block ''out'' parameter - one dimentional array expected!")
            return;
        end
        block_def.out = block_def.out';
    end

    if length(block_def.name) > 20 then
        disp("ERROR: Block name too long!");
        return;
    end

    FORCE_SIM = %F;
    if haveacompiler() == %F & block_def.use_sim_script == %F then
        warning("Compiler not found.");
        warning("This block will use simulation script instead of C code during simulation mode.");
        FORCE_SIM = %T;
    end

    path = dirname(get_function_path('mdaqBlockAdd')) + filesep();
    module_path = part(path,1:length(path)-length("macros") - 1 );
    SCRIPT_FILE_ROOT = path + 'user_blocks' + filesep();
    C_FILE_ROOT = module_path+'src'+filesep()+'c'+ filesep()+'userlib'+filesep();
    IMAGE_FILE_ROOT = module_path + filesep() + 'images' + filesep();

    // =============================PREPARE INPUT=====================================
    params_size = max(size(block_def.param_name));
    params_size_size = max(size(block_def.param_size));
    params_def_val_size = max(size(block_def.param_def_val));
    in_size = max(size(block_def.in));
    out_size = max(size(block_def.out));

    if block_def.in(1) < 1 then
        in_size = 0;
    end

    if block_def.out(1) < 1 then
        out_size = 0;
    end

    //If user did not enter enough information about size of parameters then
    //we set it to default 1.
    if params_size > params_size_size then
        sizes = ones(1:params_size)';
        sizes(1:params_size_size) = block_def.param_size;
        block_def.param_size = sizes;
    end

    //If user did not enter enough information about default parameters value then
    //we set it to default 0.
    def_val = list();
    for i=1:params_size
        def_val(i) = zeros(1:block_def.param_size(i))';

        if i <= params_def_val_size then
            i_params_def_val_size = max(size(block_def.param_def_val(i)));
            def_val(i)(1:i_params_def_val_size) = block_def.param_def_val(i);
        end
    end

    block_def.param_def_val = def_val;

    // if block name changed without desc - change block name in desc
    if block_def.desc == "Set new_block parameters" then
        block_def.desc = strsubst(block_def.desc, "new_block", block_def.name);
    end

    // =============================GENERATE STRINGS=====================================
    // converted params
    params_converted = '';

    // script file
    params_string1 = '';
    params_string2 = '';
    params_string3 = '';
    params_string4 = '';
    params_string5 = '';

    //default values string
    params_string6 = '';

    // C file
    in_ports_string = '';
    out_ports_string = '';
    params_c_string = '';
    params_sci_string = '';

    name_converted = convstr(block_def.name,'l');
    name_converted = strsubst(name_converted, ' ', '_');
    if name_converted == 'sim' then
        name_converted = name_converted + '1';
        error("Block name '"sim'" is reserved.");
    end
    name_converted = 'mdaq_' + name_converted;

    for i = 1:params_size
        // Remove spaces and convert to lower case
        params_converted(i) = convstr(block_def.param_name(i),'l')
        params_converted(i) = strsubst(params_converted(i), ' ', '_')
        params_converted(i) = strsubst(params_converted(i), ';', '_')
        params_converted(i) = strsubst(params_converted(i), ':', '_')
        params_converted(i) = strsubst(params_converted(i), ',', '_')
        params_converted(i) = strsubst(params_converted(i), '.', '_')

        params_string1 = params_string1 + params_converted(i)+',';
        params_string2 = params_string2 + ''''+block_def.param_name(i)+':'';';
        disp(params_string2)
        params_string3 = params_string3 + '''vec'','+ string(block_def.param_size(i)) + ',';
        params_string4 = params_string4 + 'sci2exp(' + params_converted(i) + ');';
        params_string5 = params_string5 + params_converted(i)+';';

        i_def_param_size = max(size(block_def.param_def_val(i)));
        i_param_def_string = '';

        for j = 1:block_def.param_size(i)
            i_param_def_string = i_param_def_string + string(block_def.param_def_val(i)(j))+';';
        end

        //default values string
        params_string6(i) = '       '+params_converted(i)+' = ['+i_param_def_string+'];';

    end

    //delete last character ','
    params_string3 = part(params_string3, 1:(length(params_string3)-1) );

    in_string = '';
    out_string = '';

    if  in_size > 0 then
        for i = 1:(in_size-1)
            in_string = in_string + string(block_def.in(i)) + ';';
        end
        in_string = in_string + string(block_def.in(in_size));
    end

    if  out_size > 0 then
        for i = 1:(out_size-1)
            out_string = out_string + string(block_def.out(i)) + ';';
        end
        out_string = out_string + string(block_def.out(out_size));
    end

    block_v_size = 3;
    if max([out_size in_size]) > 2 then
        block_v_size = block_v_size + (max([out_size in_size]) - 2);
    end

    block_h_size = 4;
    if length(block_def.name) > 8 then
        block_h_size = block_h_size + ((length(block_def.name) - 8) * 0.2);
    end

    // =============================GENERATE SCRIPT=====================================
    // TODO define default params value
    //  (block_def.name).sci script generator
    block_script = [
    '// Generated with MicroDAQ toolbox ver: ' + mdaq_version() + '';
    'function [x,y,typ] = '+ name_converted + '(job,arg1,arg2)';
    '   block_desc = [''' + block_def.desc + ''';';
    '   '''';]';
    '';
    '   x=[];y=[];typ=[];';
    '   select job';
    '   case ''set'' then';
    '       x=arg1;';];

    if params_size > 0 then
        block_script = [block_script;
        '       model=arg1.model;';
        '       graphics=arg1.graphics;';
        '       exprs=graphics.exprs;';
        '';
        '       while %t do';
        '           try';
        '               getversion(''scilab'');';
        '               [ok,'+params_string1+'exprs]=..'
        '               scicos_getvalue( block_desc,..';
        '               ['+params_string2+'],..';
        '               list('+params_string3+'), exprs)';
        '           catch';
        '               [ok,'+params_string1+'exprs]=..'
        '               getvalue(block_desc,..';
        '               ['+params_string2+'],..';
        '               list('+params_string3+'), exprs);';
        '           end;';
        '';
        '       if ~ok then';
        '              break';
        '       end';
        '';
        '       if ok then';
        '           [model,graphics,ok] = check_io(model,graphics, ['+in_string+'], ['+out_string+'], 1, []);';
        '           graphics.exprs = exprs;';
        '           model.rpar = ['+params_string5+'];';
        '           model.ipar = [];';
        '           model.dstate = [];';
        '           x.graphics = graphics;';

        '           x.model = model;';
        '           break';
        '       end';
        '';
        '   end'];
    end

     use_sim_string = [
            '       if c_link('''+name_converted+''') then';
            '           model.sim=list('''+name_converted+''',4);';
            '       else';
            '           model.sim=list('''+name_converted+'_sim'',5);';
            '           warning(''Cannot link '''''+name_converted+''''' C function. Script '''''+name_converted+'_sim.sci'''' will be used instead.'');';
            '       end';
    ];

    if block_def.use_sim_script == %T then
       use_sim_string = '       model.sim=list('''+name_converted+'_sim'',5);';
    end

    block_script = [block_script; '   case ''define'' then';
    params_string6;
    '       model=scicos_model();';
    use_sim_string;
    '       model.in=['+in_string+'];';
    '       model.in2=1;';
    '       model.out=['+out_string+'];';
    '       model.out2=1;';
    '       model.outtyp=1;';
    '       model.intyp=1;';
    '       model.evtin=1;';
    '       model.rpar=['+params_string5+'];';
    '       model.ipar=[];';
    '       model.dstate=[];';
    '       model.blocktype=''d'';';
    '       model.dep_ut=[%t %f];';
    '       exprs=['+params_string4+'];';
    '       gr_i=[''xstringb(orig(1),orig(2),['''''''' ; ],sz(1),sz(2),''''fill'''');''];';
    '       x=standard_define([' + string(block_h_size) + ' '+string(block_v_size)+'],model,exprs,gr_i);';
    '       x.graphics.in_implicit=[];';
    '       x.graphics.exprs=exprs;';
    '       x.graphics.style=[""blockWithLabel;verticalLabelPosition=center;displayedLabel=' + block_def.name + '""]';
    '   end';
    'endfunction';
    ];

    // =============================GENERATE SIM SCRIPT=====================================

    index = 0;
    n_lines = 1;
    l = 1;
    for i = 1:n_lines:(params_size*n_lines)
        params_sci_string(i+1) =  '    '+params_converted(l)+' = block.rpar('+string(index + 1)+');';
        index = index + block_def.param_size(l);
        l = l + 1;
    end
    //  (block_def.name)_sim.sci script generator
    init_string = '';
    if FORCE_SIM == %T then
        init_string = [
'           mprintf('"\nWARNING: The '''''+name_converted+''''' block uses '''''+name_converted+'_sim.sci'''' script instead of C code during\n\t simulation mode. '');';
'           mprintf('"Make sure that the valid compiler is installed. More information is available at:\n'');';
'           mprintf('"\t https://help.scilab.org/doc/5.5.2/en_US/supported_compilers.html'');';
        ];
    end

    block_script_sim = [
    '// Generated with MicroDAQ toolbox ver: ' + mdaq_version() + '';
    'function block='+name_converted+'_sim(block,flag)';
    '';
    '    global %microdaq';
    '    if %microdaq.dsp_loaded == %F then';
    params_sci_string;
    '    select flag';
    '       case -5 // Error';
    '       case 0 // Derivative State Update';
    '       case 1 // Output Update';
    '       case 2 // State Update';
    '       case 3 // OutputEventTiming';
    '       case 4 // Initialization';
    init_string;
    '       case 5 // Ending';
    '       case 6 // Re-Initialisation';
    '       case 9 // ZeroCrossing';
    '       else // Unknown flag';
    '       break';
    '    end';
    'end';
    'endfunction';
    ];

    // save scripts
    file_name = SCRIPT_FILE_ROOT+name_converted+'.sci';
    save_string(file_name, block_script);

    file_name = SCRIPT_FILE_ROOT+name_converted+'_sim.sci';
    save_string(file_name, block_script_sim);

    // =============================GENERATE C FILE =====================================
    l = 1;
    n_lines = 3;
    index = 0;

    if in_size > 0 then
        in_ports_string(1) = '    /* Block input ports */';
        for i = 2:n_lines:(in_size*n_lines+1)
            in_ports_string(i) = '    double *u'+string(l)+' = GetRealInPortPtrs(block,'+string(l)+');';
            in_ports_string(i+1) = '    int u' +string(l)+ '_size = GetInPortRows(block,'+ string(l)+');'+...
            '    /* u' + string(l)+ '_size = '+string(block_def.in(l))+' */';
            in_ports_string(i+2) = '';
            l = l + 1;
        end
    end

    l = 1;

    if out_size > 0 then
        out_ports_string(1) = '    /* Block output ports */';
        for i = 2:n_lines:(out_size*n_lines+1)
            out_ports_string(i) = '    double *y'+string(l)+' = GetRealOutPortPtrs(block,'+string(l)+');';
            out_ports_string(i+1) = '    int y'+string(l)+'_size = GetOutPortRows(block,'+string(l)+');'+....
            '    /* y' + string(l)+ '_size = '+string(block_def.out(l))+' */';
            out_ports_string(i+2) = '';
            l = l + 1;
        end
    end

    index = 0;
    n_lines = 2;
    l = 1;
    for i = 1:n_lines:(params_size*n_lines)
        if  block_def.param_size(l) == 1 then
            params_c_string(i) = '    /* param size = 1 */';
            params_c_string(i+1) =  '    double '+params_converted(l)+' = params['+string(index)+'];';
        else
            params_c_string(i) = '    int '+params_converted(l)+'_size = '+string(block_def.param_size(l))+';';
            params_c_string(i+1) =  '    double *'+params_converted(l)+' = &params['+string(index)+'];';
        end

        index = index + block_def.param_size(l);
        l = l + 1;
    end

    block_c_file = [
    '/* Generated with MicroDAQ toolbox ver: ' + mdaq_version() + ' */';
    '#include '"scicos_block4.h'"';
    '';
    '';
    'extern double get_scicos_time( void );';
    '';
    '/* This function will executed once at the beginning of model execution */'
    'static void init(scicos_block *block)';
    '{';
    '    /* Block parameters */';
    '    double *params = GetRparPtrs(block);';
    '';
    params_c_string;
    '';
    '    /* Add block init code here */';
    '}';
    '';
    '/* This function will be executed on every model step */';
    'static void inout(scicos_block *block)';
    '{';
    '    /* Block parameters */';
    '    double *params = GetRparPtrs(block);';
    params_c_string;
    '';
    in_ports_string;
    out_ports_string;
    '';
    '    /* Add block code here (executed every model step) */';
    '';
    '}';
    '';
    '/* This function will be executed once at the end of model execution (only in Ext mode) */'
    'static void end(scicos_block *block)';
    '{';
    '    /* Prameters */';
    '    double *params = GetRparPtrs(block);';
    '';
    params_c_string;
    '';
    '    /* Add block end code here */';
    '}';
    '';
    'void '+name_converted+'(scicos_block *block,int flag)';
    '{';
    '    if (flag == 1){            /* set output */';
    '        inout(block);';
    '    }';
    '    else if (flag == 5){       /* termination */';
    '        end(block);';
    '    }';
    '    else if (flag == 4){       /* initialisation */';
    '        init(block);';
    '    }';
    '}';
    ]

    file_name = C_FILE_ROOT+name_converted+'.c';
    save_string(file_name, block_c_file);

    svg_path = IMAGE_FILE_ROOT + 'svg' + filesep();
    gen_svg(svg_path, name_converted, block_def.name);

    // build macros and compile C code
    mdaqBlockBuild(%F, ~block_def.use_sim_script);
    mprintf("\tRestart Scilab to use new block\n");
endfunction

function res = save_string(filename, content)
    [f,res] = mopen(filename,'w');
    if res == 0 then
        mprintf(" ### Generating %s\n", filename);
        mputl(content,f)
        mclose(f)
    end
endfunction

function gen_svg(path,filename,label);
    doc = xmlRead(path+'mdaq_template.svg');

    // Label handle
    node = doc.root.children(13).children(1);
    node.content = "";
    xmlDump(node);
    xmlWrite(doc,path+filename+'.svg');
    xmlDelete(doc);
endfunction


sb120: #i111224# disabled failing tests for now

#111102# -o xmloff.Chart.XMLContentExporter
#111102# -o xmloff.Chart.XMLContentImporter
#111102# -o xmloff.Chart.XMLExporter
#111102# -o xmloff.Chart.XMLImporter
#111102# -o xmloff.Chart.XMLStylesExporter
#111102# -o xmloff.Chart.XMLStylesImporter
-o xmloff.Draw.XMLContentExporter
-o xmloff.Draw.XMLContentImporter
-o xmloff.Draw.XMLExporter
-o xmloff.Draw.XMLImporter
-o xmloff.Draw.XMLMetaExporter
#i111200 -o xmloff.Draw.XMLMetaImporter
-o xmloff.Draw.XMLSettingsExporter
-o xmloff.Draw.XMLSettingsImporter
#i87695 -o xmloff.Draw.XMLStylesExporter
-o xmloff.Draw.XMLStylesImporter
#i111224 -o xmloff.Impress.XMLContentExporter
-o xmloff.Impress.XMLContentImporter
-o xmloff.Impress.XMLExporter
#i111111# -o xmloff.Impress.XMLImporter
-o xmloff.Impress.XMLMetaExporter
-o xmloff.Impress.XMLMetaImporter
-o xmloff.Impress.XMLSettingsExporter
-o xmloff.Impress.XMLSettingsImporter
#i87695 -o xmloff.Impress.XMLStylesExporter
-o xmloff.Impress.XMLStylesImporter

#111102# -o xmloff.Chart.XMLContentExporter
#111102# -o xmloff.Chart.XMLContentImporter
#111102# -o xmloff.Chart.XMLExporter
#111102# -o xmloff.Chart.XMLImporter
#111102# -o xmloff.Chart.XMLStylesExporter
#111102# -o xmloff.Chart.XMLStylesImporter
-o xmloff.Draw.XMLContentExporter
-o xmloff.Draw.XMLContentImporter
-o xmloff.Draw.XMLExporter
-o xmloff.Draw.XMLImporter
-o xmloff.Draw.XMLMetaExporter
#i111200 -o xmloff.Draw.XMLMetaImporter
-o xmloff.Draw.XMLSettingsExporter
-o xmloff.Draw.XMLSettingsImporter
#i87695 -o xmloff.Draw.XMLStylesExporter
-o xmloff.Draw.XMLStylesImporter
-o xmloff.Impress.XMLContentExporter
-o xmloff.Impress.XMLContentImporter
-o xmloff.Impress.XMLExporter
#i111111# -o xmloff.Impress.XMLImporter
-o xmloff.Impress.XMLMetaExporter
-o xmloff.Impress.XMLMetaImporter
-o xmloff.Impress.XMLSettingsExporter
-o xmloff.Impress.XMLSettingsImporter
#i87695 -o xmloff.Impress.XMLStylesExporter
-o xmloff.Impress.XMLStylesImporter


Fix for building toolbox without user_blocks dir

function buildmacros()
    script_path = get_absolute_file_path("buildmacros.sce");
    module_path = part(script_path,1:length(script_path)-length("macros") - 1 )

    tbx_build_macros(TOOLBOX_NAME, script_path);
    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "microdaq_blocks");
    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "microdaq_macros");

    // check user_blocks beafore build
    if isdir(script_path + filesep() + "user_blocks") == %F then
        mkdir(script_path + filesep() + "user_blocks");
    end

    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "user_blocks");

    // Build MicroDAQ blocks
    microdaq_blocks = mgetl( script_path + filesep() + "microdaq_blocks" + filesep() + "names");
    microdaq_blocks = microdaq_blocks';

    blocks = [];
    for i=1:size(microdaq_blocks, "*")
        if strstr(microdaq_blocks(i), "_sim") == ""
            blocks = [blocks, microdaq_blocks(i)];
        end
    end

    tbx_build_blocks(module_path, blocks, "macros" + filesep() + "microdaq_blocks");

    // Build MicroDAQ User blocks
    if isfile(script_path + filesep() + "user_blocks" + filesep() + "names")  == %T then

        microdaq_blocks = mgetl( script_path + filesep() + "user_blocks" + filesep() + "names");
        microdaq_blocks = microdaq_blocks';

        blocks = [];
        for i=1:size(microdaq_blocks, "*")
            if strstr(microdaq_blocks(i), "_sim") == ""
                if isfile(script_path + filesep() + "user_blocks" + filesep() + microdaq_blocks(i) + '.sci')  == %T then
                    blocks = [blocks, microdaq_blocks(i)];
                end
            end
        end

        if blocks <> [] then
            tbx_build_blocks(module_path, blocks, "macros" + filesep() + "user_blocks");
        end
    end
endfunction

buildmacros();
clear buildmacros; // remove buildmacros on stack

function buildmacros()
    script_path = get_absolute_file_path("buildmacros.sce");
    module_path = part(script_path,1:length(script_path)-length("macros") - 1 )

    tbx_build_macros(TOOLBOX_NAME, script_path);
    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "microdaq_blocks");
    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "microdaq_macros");
    tbx_build_macros(TOOLBOX_NAME, script_path + filesep() + "user_blocks");

    // Build MicroDAQ blocks
    microdaq_blocks = mgetl( script_path + filesep() + "microdaq_blocks" + filesep() + "names");
    microdaq_blocks = microdaq_blocks';

    blocks = [];
    for i=1:size(microdaq_blocks, "*")
        if strstr(microdaq_blocks(i), "_sim") == ""
            blocks = [blocks, microdaq_blocks(i)];
        end
    end

    tbx_build_blocks(module_path, blocks, "macros" + filesep() + "microdaq_blocks");

    // Build MicroDAQ User blocks
    if isfile(script_path + filesep() + "user_blocks" + filesep() + "names")  == %T then

        microdaq_blocks = mgetl( script_path + filesep() + "user_blocks" + filesep() + "names");
        microdaq_blocks = microdaq_blocks';

        blocks = [];
        for i=1:size(microdaq_blocks, "*")
            if strstr(microdaq_blocks(i), "_sim") == ""
                if isfile(script_path + filesep() + "user_blocks" + filesep() + microdaq_blocks(i) + '.sci')  == %T then
                    blocks = [blocks, microdaq_blocks(i)];
                end
            end
        end

        if blocks <> [] then
            tbx_build_blocks(module_path, blocks, "macros" + filesep() + "user_blocks");
        end
    end
endfunction

buildmacros();
clear buildmacros; // remove buildmacros on stack


Update firmware  link in mdaq_latest_fw

Update firmware  link in mdaq_latest_fw

function fw_ver = mdaq_latest_fw()
    fw_ver = [];
    try
       [a fw_ver] = getURL("https://raw.githubusercontent.com/microdaq/Firmware/master/LATEST", TMPDIR + filesep() + "LATEST");
       fw_ver = strtod( strsplit(fw_ver, ',') );
    catch
      warning("Unable to connect to MicroDAQ firmware server.")
    end

endfunction

function fw_ver = mdaq_latest_fw()
    fw_ver = [];
    try
       [a fw_ver] = getURL("raw.githubusercontent.com/microdaq/Firmware/test/LATEST", TMPDIR + filesep() + "LATEST");
       fw_ver = strtod( strsplit(fw_ver, ',') );
    catch
      warning("Unable to connect to MicroDAQ firmware server.")
    end

endfunction


Force to compile model before code generation

function mdaq_code_gen(load_dsp_app)

    if  check_mdaq_compiler() == %F then
        messagebox("Can''t locate compiler for compiling generated code. To solve this problem run microdaq_setup() function", "Settings error", "error");
        return;
    end

    standalone = %f;
    debug_build = %t;
    profiling = %f;
    solver_type = 0;
    continue_code_gen = %f;

    scs_m;

    // TODO: make general solution for scilab 5/6
    try
        size(scs_m.objs)
    catch
        messagebox("Can''t generate code from inside of superblock.", "Code generation problem", "error")
        return
    end

    if argn(2)<2 then
        k=1;
        for i=1:(size(scs_m.objs)-1)
            sciVersion = getversion('scilab');
            if sciVersion(1) == 6 then
                if typeof(scs_m.objs(i)) <> "Block" then
                    continue;
                end
            end

            // in case of superblock set k
            if scs_m.objs(i).model.sim(1)=="super" then
                k=i;
            end

            // in case of mdaq_setup get code gen parameters
            if scs_m.objs(i).model.sim(1)=="mdaq_setup" then

                // get build mode from model setup block
                if scs_m.objs(i).model.ipar(1) == 1 then
                    debug_build = %f;
                else
                    debug_build = %t;
                end

                // get build type from model setup block
                if scs_m.objs(i).model.ipar(2) == 1 then
                    standalone = %f;
                else
                    standalone = %t;
                end

                // check if profiling is enabled
                if scs_m.objs(i).model.ipar(3) == 1 then
                    profiling = %t;
                else
                    profiling = %f;
                end
                solver_type = scs_m.objs(i).model.ipar(4);

                // mdaq_setup block found so code gen can be executed
                continue_code_gen = %t;
            end
        end
    end

    if continue_code_gen == %f then
        messagebox("Can''t determine model settings - use SETUP block from MicroDAQ palette.", "SETUP missing", "error");
        return;
    end

    if scs_m.objs(k).model.sim(1)=="super" then
        XX = scs_m.objs(k); //** isolate the super block to use

        // Save format
        oldFormat = format();
        format('v',10);

        xcos_compile(scs_m)

        [ok, XX, alreadyran, flgcdgen, szclkINTemp, freof] =  do_compile_superblock_rt(XX, scs_m, k, %f, standalone, debug_build, profiling, solver_type, load_dsp_app);

        // Restore format
        format(oldFormat(2), oldFormat(1));
        if ok == %f then
            messagebox("Can''t generate code from model. Check model for errors", "Model error", "error");
        end
    else
        messagebox("Can''t find superblock for code generation",  "Model structure problem", "error");
    end

endfunction

function mdaq_code_gen(load_dsp_app)

    if  check_mdaq_compiler() == %F then
        messagebox("Can''t locate compiler for compiling generated code. To solve this problem run microdaq_setup() function", "Settings error", "error");
        return;
    end

    standalone = %f;
    debug_build = %t;
    profiling = %f;
    solver_type = 0;
    continue_code_gen = %f;

    scs_m;

    // TODO: make general solution for scilab 5/6
    try
        size(scs_m.objs)
    catch
        messagebox("Can''t generate code from inside of superblock.", "Code generation problem", "error")
        return
    end

    if argn(2)<2 then
        k=1;
        for i=1:(size(scs_m.objs)-1)
            sciVersion = getversion('scilab');
            if sciVersion(1) == 6 then
                if typeof(scs_m.objs(i)) <> "Block" then
                    continue;
                end
            end

            // in case of superblock set k
            if scs_m.objs(i).model.sim(1)=="super" then
                k=i;
            end

            // in case of mdaq_setup get code gen parameters
            if scs_m.objs(i).model.sim(1)=="mdaq_setup" then

                // get build mode from model setup block
                if scs_m.objs(i).model.ipar(1) == 1 then
                    debug_build = %f;
                else
                    debug_build = %t;
                end

                // get build type from model setup block
                if scs_m.objs(i).model.ipar(2) == 1 then
                    standalone = %f;
                else
                    standalone = %t;
                end

                // check if profiling is enabled
                if scs_m.objs(i).model.ipar(3) == 1 then
                    profiling = %t;
                else
                    profiling = %f;
                end
                solver_type = scs_m.objs(i).model.ipar(4);

                // mdaq_setup block found so code gen can be executed
                continue_code_gen = %t;
            end
        end
    end

    if continue_code_gen == %f then
        messagebox("Can''t determine model settings - use SETUP block from MicroDAQ palette.", "SETUP missing", "error");
        return;
    end

    if scs_m.objs(k).model.sim(1)=="super" then
        XX = scs_m.objs(k); //** isolate the super block to use

        // Save format
        oldFormat = format();
        format('v',10);

        [ok, XX, alreadyran, flgcdgen, szclkINTemp, freof] =  do_compile_superblock_rt(XX, scs_m, k, %f, standalone, debug_build, profiling, solver_type, load_dsp_app);

        // Restore format
        format(oldFormat(2), oldFormat(1));
        if ok == %f then
            messagebox("Can''t generate code from model. Check model for errors", "Model error", "error");
        end
    else
        messagebox("Can''t find superblock for code generation",  "Model structure problem", "error");
    end

endfunction