#!/bin/sh

# Generates function header for a computer language
# PJ Gawthrop May 1998

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.10  2000/04/18 11:14:32  peterg
## - parameters option
##
## Revision 1.9  2000/04/08 10:23:38  peterg
## Added -noglobal option
##
## Revision 1.8  1999/02/16 04:14:53  peterg
## Small change to modeline generation
##
## Revision 1.7  1999/01/20 22:19:21  peterg
## Puts emacs mode line at top of .m files
##
## Revision 1.6  1998/07/27 20:30:39  peterg
## Mods for dm rep - no globals ...
##
## Revision 1.5  1998/07/25 20:42:30  peterg
## No assignements for switch either
##
## Revision 1.4  1998/07/25 10:43:10  peterg
## No assignments of x or u if sm rep.
##
## Revision 1.3  1998/07/19 15:52:15  peterg
## Don't call the _input function - make it external from now on ...
##
## Revision 1.2  1998/05/24 12:47:39  peterg
## This still needs worjk doing for c generation!!
##
## Revision 1.1  1998/05/23 16:17:40  peterg
## Initial revision
##
###############################################################

# Args
while [ -n "`echo $1 | grep '^-'`" ]; do
  case $1 in
    -noglobals)
	noglobals=noglobals;
	;;
    -parameters)
	parameters=parameters;
	;;
    *)
	echo $1 is an unknown option
        exit;;
  esac
  shift
done

# Set up the language specific stuff
system=$1
rep=$2
language=$3
args=$4
output=$5
extras=$6

case $language in
    m)
	modeline='% -*-octave-*- Put Emacs into octave-mode%';
	ext='m';
	Lc='#';
	Rc='#';
	Lb='(';
	Rb=')';
	;;
    p)
	modeline='% -*-octave-*- Put Emacs into octave-mode%';
	ext='m';
	Lc='#';
	Rc='#';
	Lb='[';
	Rb=']';
	;;
    c)
	ext='c';
	Lc='/*';
	Rc='*/';
	Lb='[';
	Rb=']';
	;;
    java)
	ext='java';
	Lc='/*';
	Rc='*/';
	Lb='[';
	Rb=']';
	;;
    *)
	echo Language $language not supported - sorry; exit 1
esac


######### Octave (matlab) code
if [ "$language" = m ]; then
cat <<EOF
function $output = $1_$rep($args);
% $output = $1_$rep($args);
%System $system, representation $rep, language $language;
%File $1_$rep.$ext;
%Generated by MTT on `date`;
%
EOF
fi


######### c code
if [ "$language" = c ]; then
cat <<EOF
void $1_$rep() {
/*
System $system, representation $rep, language $language;
File $1_$rep.$ext;
Generated by MTT on `date`;

NB Arrays are be defined to be one larger than expected
 - the 0 element is not used.
*/

EOF

# Appropriate emacs mode line
cat <<EOF
$modeline
EOF

# Declarations
$MATRIX -q  <<EOF 
  %System structure
  [nx,ny,nu,nz,nyz] = $1_def;

  printf("$Lc Declare standard arrays $Rc\n");
  printf("  extern double mtty[%i]; \t $Lc $1_ode output $Rc\n", ny+1);
  printf("  extern double mttdx[%i]; \t $Lc $1_ode state derivative $Rc\n", nx+1);
  printf("  extern double mttx[%i]; \t $Lc $1_ode state $Rc\n", nx+1);
  printf("  extern double mttu[%i]; \t $Lc $1_ode input $Rc\n\n", nu+1);

  printf("$Lc Dummy variable list $Rc\n");
  printf("  double t0;\n\n");


  printf("$Lc State variable list $Rc\n");
  for i = 1:nx
    printf("  double mttx%i;\n",i);
  end;
printf("\n");

printf("$Lc State variable input list $Rc\n");
for i = 1:nu
  printf("  double mttu%i;\n",i);
end;
printf("\n");

printf("$Lc Counter $Rc\n");
printf("  int i;\n");
printf("\n");

printf("$Lc Time $Rc\n");
printf("  double t;\n");
printf("\n");

EOF

fi

if [ "$language" = "java" ]; then
cat <<EOF
public class $1_ode{
/*
System $system, representation $rep, language $language;
File $1_$rep.$ext;
Generated by MTT on `date`;
*/

EOF
fi

# Any extra bits ..
if [ -n "$extras" ]; then
cat <<EOF
$Lc Extra bits here $Rc
$extras
EOF
fi

# Globals
if [ -z "$noglobals" ]; then
    sympar2global_txt2lang $1 $language
#else
#cat<<EOF
#global ...
#mtt_no_globals ;
#EOF
fi

# Explicit parameters
if [ -n "$parameters" ]; then
    sympar2par_txt2m $1 
fi

## if [ "$language" = "c" ] || [ "$language" = "m" ]; then
##   if [ "$rep" != "sm" ]&&[ "$rep" != "dm" ]&&[ "$rep" != "switch" ]; then
##   # Common part - ode (c and m)
##   # Use octave for this bit - needs the definition file
##   $MATRIX -q  <<EOF 
##     %System structure
##     [nx,ny,nu,nz,nyz] = $1_def;
## 
##   printf("$Lc====== Set up the state variables ======$Rc\n");
##   for i = 1:nx
##     printf("  mttx%i = mttx$Lb%i$Rb;\n",i,i);
##   end;
##   printf("\n");
## 
##   printf("$Lc====== Set up the input variables ======$Rc\n");
##   for i = 1:nu
##   printf("  mttu%i = mttu$Lb%i$Rb;\n",i,i);
##   end;
##   printf("\n");
## 
## EOF
##  fi
## fi
## #if [ "$language" = "java" ]; then

#$MATRIX -q <<EOF   
#    printf("JAVA\n");
#EOF

#fi

