#!/bin/sh

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

# Bourne shell script: abg2tex
# Creates a tex file describing the system abg
# P.J.Gawthrop May 1997
# Copyright (c) P.J.Gawthrop, 1997.

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.6  2004/02/11 10:10:04  gawthrop
## Includes a _pic.fig file (schematic) if such exists
##
## Revision 1.5  2004/02/10 16:12:21  gawthrop
## Don't give details of Se De Sf Df INTF
##
## Revision 1.4  2002/04/28 18:41:26  geraint
## Fixed [ 549658 ] awk should be gawk.
## Replaced calls to awk with call to gawk.
##
## Revision 1.3  2001/07/12 04:02:53  gawthrop
## Now fixes multiports for input and output as well as state
##
## Revision 1.2  2000/11/27 17:45:00  peterg
## Removed "System" from  heading
##
## Revision 1.1  2000/11/27 15:11:15  peterg
## Initial revision
##
## Revision 1.7  1997/12/06 16:20:37  peterg
## Added another argument - the label
##
## Revision 1.6  1997/12/06 15:49:35  peterg
## Changed order of fig arguments to put filename first.
##
## Revision 1.5  1997/08/15 11:32:05  peterg
## Removed an echo left in by mistake
##
# Revision 1.4  1997/08/15  11:19:25  peterg
# Minto cosmetics.
#
## Revision 1.3  1997/05/21 12:07:22  peterg
## Replaced echo by cat -- much simpler.
##
## Revision 1.2  1997/05/19 16:25:53  peterg
## Explicit include of tex files .. latex2html seems to prefer this!
##
# Revision 1.1  1997/05/19  11:30:00  peterg
# Initial revision
#
###############################################################

## Subsystems that don't need documentation
ignore(){ 
   gawk '
BEGIN{
ignored = " Se De Sf Df INTF ";
}
{
     subsys = sprintf(" %s ", $1);
     if (match(ignored,subsys)==0)
       print $1 
  }'
}

#Look for a command line argument
while [ -n "`echo $1 | grep '^-'`" ]; do
  case $1 in
	-pdf )  pdf='-pdf'
                ;;
	-s )  sensitivity='-s'
                ;;

	*)
		echo "$1 is an invalid argument - ignoring" ;;
  esac
  shift
done


if [ -z "$2" ]; then
  filename="$1_abg.tex";
  echo > $filename
  echo Creating $filename
  subsection='section'
else
  filename=$2
  subsection='subsection'
fi


#Section heading (if not at top level)
if [ -n "$2" ]; then
cat<<EOF >> $filename

%% Subsystem $1
  \subsection{\textbf{$1}}
    \label{sec:$1}
    \index{\textbf{$1} -- abg} 

EOF
else
mtt -q -u ${sensitivity} $1 abg m # Make sure all the subsystems exist -- nasty kludge
cat<<EOF > $filename
   % abg report for system $1 ($filename)
   % Generated by MTT at `date`
EOF
fi

# Figure
##echo mtt -q -u $pdf $1 abg ps
mtt -q -u  $pdf $1 abg ps
cat <<EOF  >> $filename

   \fig{`pwd`/$1_abg}{$1_abg.ps}{0.9}{System \textbf{$1}: acausal bond graph}

EOF

## Put a schematic if exists (_pic.fig)
if [ -e "$1_pic.fig" ]; then
  echo Inserting $1_pic.fig
  fig2dev -L eps $1_pic.fig  > $1_pic.ps
cat <<EOF  >> $filename

   \fig{`pwd`/$1_pic}{$1_pic.ps}{0.9}{System \textbf{$1}: schematic diagram}

EOF
fi

# Description
mtt -q -u $1 desc tex
# Put in absolute pathnames in figures -- and fix bug in old desc files
sed "s;fig{;fig{`pwd`/;g" < $1_desc.tex |\
sed "s/Ref{$1_abg}/Ref{fig:$1_abg.ps}/" >> $filename

# Label file
mtt -q -u $1 lbl tex
cat<<EOF  >> $filename
 \sub$subsection{Summary information} 
 \label{sec:$1_lbl} 
 \index{\textbf{$1} -- lbl} 
EOF
cat $1_lbl.tex   >> $filename


# CRs
#mtt -q -u $1 cr tex
#cat<<EOF  >> $filename
# \sub$subsection{Constitutive relationships} 
# \label{sec:$1_cr} 
# \index{\textbf{$1} -- cr} 
#EOF
#cat $1_cr.tex   >> $filename

# Subsystems
mtt -q -u $1 sub tex 
cat<<EOF  >> $filename
 \sub$subsection{Subsystems} 
 \label{sec:$1_sub} 
 \index{\textbf{$1} -- subsystems} 
EOF
cat $1_sub.tex   >> $filename

#Now do the subsystems (if at top level)
if [ -z "$2" ]; then
    sub2subs $1 | sort -u | ignore |\
  gawk '{print "abg2tex", pdf, $1, filename}' pdf=$pdf filename=$filename | sh
fi
