#!/bin/sh
     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: mtt_header
# Headings for functions
# Copyright (C) 2000 by Peter J. Gawthrop

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.3  2000/10/11 08:59:15  peterg
## Added csex rep
##
## Revision 1.2  2000/10/11 08:01:42  peterg
## Added noglobal fudge
##
## Revision 1.1  2000/10/10 21:02:27  peterg
## Initial revision
##
###############################################################

# Arguments
system=$1
rep=$2
language=$3
args=$4
output=$5
extras=$6

# Find system constants
Nx=`mtt_getsize $system x` # States
Nxx=`mtt_getsize $system xx` # States x States
Nu=`mtt_getsize $system u` # Inputs 
Ny=`mtt_getsize $system y` # Outputs 
Nyz=`mtt_getsize $system yz` # Zero outputs
Npar=`wc -l $system\_sympar.txt | awk '{print $1}'`

# Representation-specific stuff
case $rep in
    cse)
	states=yes;
        inputs=yes;
	parameters=yes;
	;;
    csex)
	states=yes;
        inputs=yes;
	parameters=yes;
	;;
    cseo)
	states=yes;
        inputs=yes;
	parameters=yes;
	;;
    ode)
	states=yes;
        inputs=yes;
	parameters=yes;
	;;
    numpar)
	states=no;
	inputs=no;
	parameters=no;
	;;
    odeo)
	states=yes;
        inputs=yes;
	parameters=yes;
	;;
    sm)
	states=no;
	inputs=no;
	parameters=yes;
	;;
    *)
	
esac
# Lanuage specific stuff
case $language in
    m)
	modeline='% -*-octave-*- Put Emacs into octave-mode%';
	ext='m';
	Lc='##';
	Rc='';
	Lb='(';
	Rb=')';
	function="function"
	declaration="[$output] = $1_$rep($args);"
	noglobals=true; # Fudge to make mtt_m2p work
	;;
    *)
	echo Language $language not supported - sorry; exit 1
esac


# Header information
cat<<EOF
$modeline
$function $declaration
$Lc $declaration
$Lc System $system, representation $rep, language $language; $Rc
$Lc File $1_$rep.$ext; $Rc
$Lc Generated by MTT on `date`; $Rc
EOF

if [ -n "$noglobals" ]; then
cat<<EOF

## Horrible fudge to make mtt_m2p work
global ...
mtt_no_globals ;
EOF
fi

# Parameters
if [ "$parameters" = "yes" ]; then
cat <<EOF

$Lc Parameters
EOF
sympar2par_txt2m $1 
fi
# States
if [ "$states" = "yes" ]; then
cat <<EOF

$Lc States
EOF
N=`n2m 1 $Nx`
for i in $N; do
    echo '  mttx'$i' = mttx('$i');'
done
fi

# Inputs
if [ "$inputs" = "yes" ]; then
cat <<EOF

$Lc Inputs
EOF
N=`n2m 1 $Nu`
for i in $N; do
    echo '  mttu'$i' = mttu('$i');'
done
fi
