#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

## 	$Id$	

# Copyright (C) 1999,2000 by Peter J. Gawthrop

# Tell user
Sys=$1
lang=$2
date=`date`
outfile=$1_input.$2

Nu=`mtt_getsize ${Sys} u` # Inputs 
echo  "Creating $outfile for standard input"

## Pascal code
make_p()
{
cat <<EOF > $outfile
PROCEDURE ${Sys}_input(VAR mttu : InputVector;
                       mttx : StateVector;
                       mtty : OutputVector;
                       mttt : REAL;
                       par  : ParameterVector);

{Created by MTT on $date for standard input to simulation}

VAR t : REAL; Start:BOOLEAN;

BEGIN{${Sys}_input}
  {Zap comments}
  IF (input^ = chr("#")) THEN
  BEGIN
    readln;
    WHILE (input^ = chr("#")) DO readln;
  END;

  read(t); { Read, and discard, first column (time) }
  FOR mtti:=1 TO $Nu DO {Read the inputs}
    read(mttu[mtti]);
  readln; {Next line}


END{${Sys}_input};
EOF
}

make_m() {
mtt_header ${Sys} input m stdin > $outfile
cat >> $outfile <<EOF
## The "standard input"
global MTT_input MTT_input_index MTT_input_last
  
  MTT_input_index = min(++MTT_input_index,MTT_input_last);
  mttu = MTT_input(MTT_input_index,:);
## END Code
endfunction
EOF

}


make_cc() {
cat > $outfile <<EOF
// -*-c++-*- Put emacs into c++-mode

// 
// System ${Sys}, representation input, language oct; 
// File ${Sys}_input.oct; 
// Generated by MTT on `date`; 

#ifdef STANDALONE
#include <octave/oct.h>
#include "${Sys}_def.h"

void strip_comments (istream &str)
{
  char c;
  c = str.peek ();
  while (c == '#')
    {
      str.unsetf(ios::skipws);
      while (c != '\n')
	{
	  str >> c;
	}
      str.setf(ios::skipws);
      c = str.peek ();
    }
}

ColumnVector F${Sys}_input (
	ColumnVector	&mttx,
	ColumnVector	&mtty,
	const double	&mttt,
	ColumnVector	&mttpar
)
{
#endif // STANDALONE
#ifndef STANDALONE
#include <octave/oct.h>
#include <variables.h>
#include "${Sys}_def.h"

DEFUN_DLD (${Sys}_input, args, ,
"Usage: [mttu] = ${Sys}_input()
Octave input (-stdin) representation of system ${Sys}
Generated by MTT on `date`")
{

  octave_value_list retval;
#endif // ! STANDALONE

  ColumnVector	mttu	(MTTNU);

  // Set up the mttu vector
#ifndef STANDALONE
  int MTT_input_index	= static_cast<int>(get_global_value ("MTT_input_index").double_value ());
  int MTT_input_last	= static_cast<int>(get_global_value ("MTT_input_last").double_value ());
  Matrix MTT_input 	= get_global_value ("MTT_input").matrix_value();

  mttu = MTT_input.row (MTT_input_index);

  if (MTT_input_index < (MTT_input_last - 1))
    {
      set_global_value ("MTT_input_index", static_cast<double>(++MTT_input_index));
    }
  
#else
  double t, u;
  strip_comments (cin);
  cin >> t;
  for (register int i = 0; i < MTTNU; i++)
    {      
      cin >> u;
      mttu(i) = u;
    }
#endif


#ifndef STANDALONE
  retval (0)	= octave_value (mttu);
  return (retval);
}
#endif // ! STANDALONE
#ifdef STANDALONE
  return mttu;
}
#endif // STANDALONE
EOF
}

case $lang in
    p)
	make_p 
	;;
    m)
	make_m
	;;
    cc)
	make_cc
	;;
    *)
	echo language $lang not supported - sorry
esac
