#! /bin/sh

# This program is granted to the public domain

# 2003-01-03 Paul Kienzle <pkienzle@users.sf.net>
# * eliminate sed --- use direct string interpolation for variables
# * define both the C and fortran names for mexFunction in the oct-file.
# 2001-06-20 Paul Kienzle <pkienzle@users.sf.net>
# * eliminate $(arg:0:1) since it is not available in all sh versions
# 2001-09-20 Paul Kienzle <pkienzle@users.sf.net>
# * use config-like syntax to set the name of mkoctfile and the path to mex

test $# -lt 1 && echo usage: mex2oct -options file.c && exit 1

first=""
for arg in $*; do
    case "$arg" in -c) compileonly=1 ;; -*) ;; *) first="$arg"; break ;; esac
done

if test -z "$first" ; then
   mkoctfile -Doctave_idx_type=int -DHAVE_OCTAVE_21 -v $*
   exit
fi

if test -n "$compileonly" ; then
   set -x
   mkoctfile -Doctave_idx_type=int -DHAVE_OCTAVE_21 -v -I/usr/lib/octave/2.1.73/site/oct/i486-pc-linux-gnu/octave-forge $*
   exit
else
   echo building $first
fi

# default the name of the octave function from the first filename
dir=`dirname $first`
first=`basename $first`
#echo "first= $first"
ext=`echo $first | sed 's;.*\.;.;g'`
#echo "ext= $ext"
name=`basename $first $ext`
#echo "name=$name ext=$ext"

case "$ext" in
  f*|F*)
    invoke=Fortran_mex
    otherfn=mexFunction
  ;;
  *)
    invoke=C_mex
    otherfn="F77_FUNC(mexfunction,MEXFUNCTION)"
  ;;
esac

# search for a .m file which will be used for the help string
# in the mex function.
if test -f $dir/$name.m ; then
	mfile=$dir/$name.m
elif test -f ./$name.m ; then
	mfile=./$name.m
elif test -f $dir/../../../Psychtoolbox/PsychBasic/$name.m ; then
	mfile=$dir/../../../Psychtoolbox/PsychBasic/$name.m
else
	mfile=""
fi

cat <<EOF > Octave/$name.cc
// This C++ file is the interface between GNU/Octave and Psychtoolbox
// module $name. It defines the entry-point function F$name and the online
// help for $name. The function itself (see bottom of file) is just a
// wrapper around octFunction(), the real command dispatcher defined in
// the PsychScriptingGlue.cc file.
// This file is autogenerated, please do not edit!
//

#ifdef PTBOCTAVE
#define PTBMODULE_$name
#include <octave/oct.h>

extern "C" {
  // mex.cc names both mexFunction (c) and MEXFUNCTION (Fortran)
  // but the mex file only defines one of them, so define the other
  // here just to keep the linker happy, but don't ever call it.
  void $otherfn() {}
  const char *mexFunctionName = "$name";
} ;

DEFUN_DLD($name, args, nargout,
EOF

if test "X$mfile" = "X" ; then
	cat <<EOF >> Octave/$name.cc
"\
$name not directly documented. Try the following:\n\
   type(file_in_loadpath('$name.m'))\n\
")
#endif

EOF
else
	gawk 'BEGIN{print "\"\\";printing=0;} 
		/^[ \t]*[%#]/ {printing=1;
		gsub(/^[ \t]*[%#]*/,""); 
		gsub(/\\/,"\\\\"); 
		gsub(/"/,"\\\""); 
		print $0 "\\n\\"; next}
		{if (printing) exit;}
		END{print "\")"}' \
		$mfile >> Octave/$name.cc

EOF

fi

cat <<EOF >> Octave/$name.cc
{
  octave_value_list octFunction(const octave_value_list &, const int);
  return octFunction(args, nargout);
}
#endif

EOF

if test -f "Octave/mex.o" ; then
  MEXPATH="Octave"
else
  MEXPATH="/usr/lib/octave/2.1.73/site/oct/i486-pc-linux-gnu/octave-forge"
fi


set -x
./mkoctfile -Doctave_idx_type=int -DHAVE_OCTAVE_21 -DPTBOCTAVE -v -o $name.oct -I$MEXPATH $*
