#!/bin/sh
#
# Copyright (C) 2004-2009 Yves Renard, Julien Pommier.
#                                                       
# This file is a part of GETFEM++                                         
#                                                                         
# GetFEM++  is  free software;  you  can  redistribute  it  and/or modify it
# under  the  terms  of the  GNU  Lesser General Public License as published
# by  the  Free Software Foundation;  either version 2.1 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 Lesser General Public
# License for more details.
# You  should  have received a copy of the GNU Lesser General Public License
# along  with  this program;  if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
#

CF=$1
MF=`basename $CF .cc`.m
src_dir=`dirname $CF`
funname=`echo $MF | sed -e 's/\.m$//'`
shortfunname=`echo $funname | sed -e 's/^gf_//'`

echo "Extracting doc from $CF to $MF"
#cat $CF | sed -n -e '/\*MLABCOM/,/MLABCOM\*/s/^/%/p' | sed -e '/MLABCOM/d' > $MF

$src_dir/../bin/extract_pydoc $MF $src_dir | sed -e 's/^/% /' > $MF 
echo "function [varargout]=${funname}(varargin)" >> $MF
cat $CF | sed -n -e '/\*MLABEXT/,/MLABEXT\*/p' | sed -e '/MLABEXT/d' >> $MF

echo "  if (nargout)," >> $MF
echo "    [varargout{1:nargout}]=gf_matlab('${shortfunname}',varargin{:});" >> $MF
echo "  else" >> $MF
echo "    gf_matlab('${shortfunname}',varargin{:});" >> $MF
echo "    if (exist('ans','var') == 1), varargout{1}=ans; end;" >> $MF
echo "  end;" >> $MF
