%% Building callCddMex
%
% This file tries to build the interface to cddlib that is provided under
%   GNU-GPL by Komei Fukuda. The mex interface itself is provided by the 
%   mpt-toolbox (M. Baotic, F. Torrisi, M. Kvasnica) under GNU-GPL. See
%   licencing information in the subfolders of the directory where you 
%   found this file.
%
% This file is automatically called by configElk and must be run in the
%   directory where this file is found. According to mex there are 
%   relatively strict requirements on the installed MATLAB version and 
%   C-compiler version. If your compiled version crashes - use a C-compiler
%   with an appropriate version to compile cddlib and cddmex.

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

%% Setup of subfolders
cddlibLocation = 'cddlib-094g';
cddmexLocation = 'mpt-cddmex';
compiler = 'gcc-4.4';
zerotol = 1e-7;
% choice of zerotol: generic_paracetamol_19 in cs-decomposition causes
%   cddlib errors for zerotol <= 1e-8

%% Caller and m-file location
callLocation = pwd;
myLocation = which('buildCddlib');
myLocation = myLocation(1:find(myLocation == '/', 1, 'last'));
logfile = [myLocation filesep 'buildCddlib.log'];

%% Write zerotolCddlib.m
cd(myLocation)
fileId = fopen('zerotolCddlib.m', 'w+');
fprintf(fileId, 'function zerotol = zerotolCddlib\n');
fprintf(fileId, '%% return zerotol that is used for compilation of cddlib\n');
fprintf(fileId, ['zerotol = ' num2str(zerotol) ';']);
fclose(fileId);

%% Precompile cddlib
type legalNoticeCddlib

cd(cddlibLocation);
callSystemFunction(['./configure CC=' compiler ' CFLAGS=-fPIC'], ...
    'logfile', logfile, ...
    'reset logfile', 1, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');

cd('lib-src');

% Apply zerotol option
command = ['sed ''s/\(#define dd_almostzero  \)\([0-9]*.[0-9]*E*-*[0-9]*\)/' ...
    '\1' num2str(zerotol, '%G') '/g'' cdd.h.original > cdd.h'];
% system(command)
callSystemFunction(command, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. This error cannot be found in the logfile.');

callSystemFunction('make', ...
    'logfile', logfile, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');

%% Compile cddmex
cd(myLocation)
type legalNoticeMpt

cd(fullfile(myLocation, cddlibLocation, 'lib-src'));
callSystemFunction(['cp ' ...
    myLocation filesep cddmexLocation filesep 'cddmex.c .' filesep], ...
    'logfile', logfile, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');

try
    % libcdd.a is in .libs only in version 94g
    eval(['mex cddmex.c .libs/libcdd.a CC=' compiler]);
catch thisError
    type(fullfile(myLocation, 'buildCddlib.log'));
    disp('Mex execution failed - see details far above the added log')
    throw(thisError);
end
% 94e:
% gcc -O -pthread -shared -m32 -Wl,--version-script,/usr/local/MATLAB/R2011b/extern/lib/glnx86/mexFunction.map -Wl,--no-undefined -o  "cddmex.mexglx"  cddmex.o  libcdd.a -Wl,-rpath-link,/usr/local/MATLAB/R2011b/bin/glnx86 -L/usr/local/MATLAB/R2011b/bin/glnx86 -lmx -lmex -lmat -lm -lstdc++
% 94f:
% gcc -O -pthread -shared -m32 -Wl,--version-script,/usr/local/MATLAB/R2011b/extern/lib/glnx86/mexFunction.map -Wl,--no-undefined -o  "cddmex.mexglx"  cddmex.o  libcdd.a -Wl,-rpath-link,/usr/local/MATLAB/R2011b/bin/glnx86 -L/usr/local/MATLAB/R2011b/bin/glnx86 -lmx -lmex -lmat -lm -lstdc++
callSystemFunction('rm cddmex.c', ...
    'logfile', logfile, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');
callSystemFunction(['mv cddmex.' mexext ' ../../callCddLib.' mexext], ...
    'logfile', logfile, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');

%% Copy help file
cd(myLocation);
callSystemFunction(['cp ' cddmexLocation '/cddmex.m ' './callCddLib.m'], ...
    'logfile', logfile, ...
    'error type', 'elk:setup', ...
    'error message', 'Build of cddlib failed. See logfile above.');

disp('The log file of this process is stored in: ')
disp(fullfile(myLocation, 'buildCddlib.log'));

cd(callLocation);
